-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweb_app.py
85 lines (70 loc) · 2.59 KB
/
web_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# -*- coding: utf-8 -*-
import re
from fasthtml.common import *
from fasthtml.fastapp import *
from beancount import Beancount
title = 'Bean-Add'
app, rt = fast_app(
static_path='./icons',
htmlkw={'data_theme': 'light'}
)
bean = Beancount()
bean.bean_cache()
def new_input():
return Input(
id='raw_input', placeholder='add new', required=True,
autocapitalize='off', autocomplete='off', autocorrect='off',
hx_post='/complete', target_id='current',
hx_swap='innerHTML transition:true',
hx_trigger='keyup changed delay:300ms'
)
@app.get('/')
def index():
favicon = Link(rel='icon', type='image/png', href='/Assets.png')
input_box = new_input()
input_button = Button(
'Enter', id='add_button',
hx_post='/enter', target_id='output', hx_swap='beforeend transition:true',
hx_trigger='click', hx_include='#raw_input'
)
box_enter = Div(
hx_post='/enter', target_id='output', hx_swap='beforeend transition:true',
hx_trigger="keyup[key=='Enter'] from:#raw_input changed", hx_include='#raw_input'
)
box_clear = Div(
hx_post='/clear', target_id='raw_input', hx_swap='outerHTML transition:true',
hx_trigger="keyup[key=='Enter'] from:#raw_input"
)
button_clear = Div(
hx_post='/clear', target_id='raw_input', hx_swap='outerHTML transition:true',
hx_trigger='click from:#add_button'
)
input_group = Group(input_box, input_button, box_enter, box_clear)
current = Div(P('...'), id='current')
output = Div(H3('History'), id='output')
card = Card(current, header=input_group, footer=output)
return Title(title), favicon, Main(H1(title), card, cls='container')
@app.post('/complete')
def complete(raw_input:str):
if not raw_input.strip():
return P('...')
output = bean.bean_add(raw_input.strip().split())[0]
if len(raw_input.split()) < 4:
return output['subtitle']
return output['arg'].replace('\n', '<br>')
@app.post('/enter')
def enter(raw_input:str):
if len(raw_input.split()) < 4:
return
output = bean.bean_add(raw_input.strip().split())[0]
with open(bean.settings['default_ledger'], 'r') as ledger_file:
content_lines = ledger_file.readlines()
content_lines.insert(len(content_lines)-3, '\n'+output['arg']+'\n')
with open(bean.settings['default_ledger'], 'w') as ledger_file:
ledger_file.write("".join(content_lines))
return Pre(re.sub(' +', ' ', output['arg']))
@app.post('/clear')
def clear():
return new_input()
if __name__ == '__main__':
serve(host='0.0.0.0', port=5001)