File: 25_state_update.py

package info (click to toggle)
python-trame 3.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 101,620 kB
  • sloc: python: 13,515; sh: 183; javascript: 93; makefile: 7
file content (73 lines) | stat: -rw-r--r-- 2,074 bytes parent folder | download
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
import asyncio
import time

from trame.app import get_server
from trame.ui.vuetify import SinglePageLayout
from trame.widgets import html, vuetify

# -----------------------------------------------------------------------------
# Trame app
# -----------------------------------------------------------------------------

server = get_server(client_type="vue2")
state = server.state

# -----------------------------------------------------------------------------
# State setup
# -----------------------------------------------------------------------------

state.a = 1
state.b = 0
state.c = 1
state.d = 0


@state.change("a")
async def change_a(a, c, **kwargs):
    print(f"(a) server state {a=} {c=}")
    await asyncio.sleep(2)  # async 2s wait
    with state:
        state.b = a * 2


@state.change("c")
def change_c(a, c, **kwargs):
    print(f"(c) server state {a=} {c=}")
    time.sleep(2)  # busy 2s wait
    state.d = c * 2


# -----------------------------------------------------------------------------
# UI setup
# -----------------------------------------------------------------------------

with SinglePageLayout(server) as layout:
    with layout.toolbar:
        vuetify.VSpacer()
        vuetify.VSlider(
            label="a",
            value=("get('a')",),
            hide_details=True,
            dense=True,
            disabled=True,
        )
        vuetify.VSlider(
            label="c",
            value=("get('c')",),
            hide_details=True,
            dense=True,
            disabled=True,
        )

    with layout.content:
        with vuetify.VContainer(fluid=True):
            html.Div("(Async) a={{a}} b={{b}} | (Busy) c={{c}} d={{d}}")
            vuetify.VSlider(v_model=("a", 0), label="Async server handling (a)")
            vuetify.VSlider(v_model=("c", 0), label="Busy server handling (c)")

# -----------------------------------------------------------------------------
# start server
# -----------------------------------------------------------------------------

if __name__ == "__main__":
    server.start()