Skip to content

Commit

Permalink
docs(example): collaboration state async/busy update
Browse files Browse the repository at this point in the history
  • Loading branch information
jourdain committed Oct 20, 2022
1 parent a6659c9 commit 792dd28
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions examples/validation/core/25_state_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import time
import asyncio
from trame.app import get_server
from trame.widgets import vuetify, html
from trame.ui.vuetify import SinglePageLayout

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

server = get_server()
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()

0 comments on commit 792dd28

Please sign in to comment.