-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
75 lines (56 loc) · 1.47 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
import asyncio
from pyreact import component, h, fragment, use_state, use_callback
from pyreact.web import App, use_url, link
@component
def hello_world():
name, set_name = use_state('World')
@use_callback(set_name)
def handle_input(e):
set_name(e.value)
@use_callback(set_name)
def handle_reset(e):
set_name('World')
return fragment(
h.div(f'Hello, {name}!'),
h.input(value=name, oninput=handle_input),
h.button(onclick=handle_reset)('reset'),
)
@component
def counter(init_count=0):
count, set_count = use_state(init_count)
@use_callback(set_count)
def increment(e):
set_count(lambda count: count + 1)
@use_callback(set_count)
def decrement(e):
set_count(lambda count: count - 1)
return h.div(
h.button(onclick=decrement)('-'),
f' count: {count} ',
h.button(onclick=increment)('+'),
count > 10 and h.p('That\'s high!'),
)
@component
def not_found():
url = use_url()
return f'url not found: {url}'
PAGES = {
'/': hello_world,
'/hello-world': hello_world,
'/counters': fragment(
counter,
counter(init_count=10),
),
}
@component
def router():
url = use_url()
page = PAGES.get(url, not_found)
return fragment(
h.ul(
h.li(link(href='/hello-world')('Hello, World!')),
h.li(link(href='/counters')('Counters')),
),
page,
)
app = App(router)