-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
79 lines (61 loc) · 1.57 KB
/
example.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
import typing as t
from brutalpywebui import BrutalPyWebUI
example_html = """
<input id="inp_regular_txt" value="Updated value" />
<button onclick="_wuiEvent('btn_press', _wuiVal('#inp_regular_txt'))">{{ button_text }}</button>
<div>Result: <span id="txt_result">Old value</span></div>
<div>Ticker: <span id="txt_ticker">{{ ticker_text }}</span></div>
"""
example_css = """
body {
display: flex;
flex-direction: column;
align-items: center;
padding: 30px 20px;
}
body > * {
width: 100%;
height: 24px;
max-width: 300px;
margin-bottom: 10px;
padding: 0;
border: none;
outline: none;
box-sizing: border-box;
}
body > input {
border: 1px solid black;
padding: 0 3px;
}
"""
wui: BrutalPyWebUI | None = None
ticker = 0
async def on_background():
global ticker
ticker += 1
await wui.el_set_text(["#txt_ticker"], str(ticker))
async def on_event(event: str, data: t.Any):
match event:
case "btn_press":
await wui.el_set_text(["#txt_result"], data)
case _:
pass
async def on_init():
# this uses Jinja2 templates
await wui.el_set_html_templ(
["body"],
example_html,
button_text="Press me!",
ticker_text=str(ticker),
)
wui = BrutalPyWebUI(
page_title="MyApp",
init_handler=on_init,
event_handler=on_event,
background_handler=on_background,
background_interval=5.2, # seconds
base_css=example_css,
)
# these are the default host and port
# specified here for the sake of the example
wui.run(host="localhost", port=7865)