|
| 1 | +import asyncio |
| 2 | +import shiny.ui_toolkit as st |
1 | 3 | from shiny import * |
| 4 | +from htmltools import tags |
2 | 5 |
|
3 | | -ui = page_fluid( |
| 6 | +ui = st.page_fluid( |
4 | 7 | tags.p( |
5 | 8 | """ |
6 | 9 | The first time you click the button, you should see a 1 appear below the button, |
|
9 | 12 | print the number of clicks in the console twice. |
10 | 13 | """ |
11 | 14 | ), |
12 | | - navs_tab_card( |
13 | | - nav( |
| 15 | + st.navs_tab_card( |
| 16 | + st.nav( |
14 | 17 | "Sync", |
15 | | - input_action_button("btn", "Click me"), |
16 | | - output_ui("foo"), |
| 18 | + st.input_action_button("btn", "Click me"), |
| 19 | + st.output_ui("btn_value"), |
17 | 20 | ), |
18 | | - nav( |
| 21 | + st.nav( |
19 | 22 | "Async", |
20 | | - input_action_button("btn_async", "Click me"), |
21 | | - output_ui("foo_async"), |
| 23 | + st.input_action_button("btn_async", "Click me"), |
| 24 | + st.output_ui("btn_async_value"), |
22 | 25 | ), |
23 | 26 | ), |
24 | 27 | ) |
25 | 28 |
|
26 | 29 |
|
27 | | -def server(session: ShinySession): |
| 30 | +def server(input: Inputs, output: Outputs, session: Session): |
28 | 31 |
|
29 | 32 | # i.e., observeEvent(once=False) |
30 | | - @observe() |
31 | | - @event(lambda: session.input["btn"]) |
| 33 | + @reactive.effect() |
| 34 | + @event(input.btn) |
32 | 35 | def _(): |
33 | | - print("@observe() event: ", str(session.input["btn"])) |
| 36 | + print("@effect() event: ", str(input.btn())) |
34 | 37 |
|
35 | 38 | # i.e., eventReactive() |
36 | | - @reactive() |
37 | | - @event(lambda: session.input["btn"]) |
| 39 | + @reactive.calc() |
| 40 | + @event(input.btn) |
38 | 41 | def btn() -> int: |
39 | | - return session.input["btn"] |
| 42 | + return input.btn() |
40 | 43 |
|
41 | | - @observe() |
| 44 | + @reactive.effect() |
42 | 45 | def _(): |
43 | | - print("@reactive() event: ", str(btn())) |
| 46 | + print("@calc() event: ", str(btn())) |
44 | 47 |
|
45 | | - @session.output("foo") |
| 48 | + @output() |
46 | 49 | @render_ui() |
47 | | - @event(lambda: session.input["btn"]) |
48 | | - def _(): |
49 | | - return session.input["btn"] |
| 50 | + @event(input.btn) |
| 51 | + def btn_value(): |
| 52 | + return str(input.btn()) |
50 | 53 |
|
51 | 54 | # ----------------------------------------------------------------------------- |
52 | 55 | # Async |
53 | 56 | # ----------------------------------------------------------------------------- |
54 | | - @observe_async() |
55 | | - @event(lambda: session.input["btn_async"]) |
| 57 | + @reactive.effect_async() |
| 58 | + @event(input.btn_async) |
56 | 59 | async def _(): |
57 | | - print("@observe_async() event: ", str(session.input["btn_async"])) |
| 60 | + await asyncio.sleep(0) |
| 61 | + print("@effect_async() event: ", str(input.btn_async())) |
58 | 62 |
|
59 | | - @reactive_async() |
60 | | - @event(lambda: session.input["btn_async"]) |
61 | | - async def btn_async() -> int: |
62 | | - return session.input["btn_async"] |
| 63 | + @reactive.calc_async() |
| 64 | + @event(input.btn_async) |
| 65 | + async def btn_async_r() -> int: |
| 66 | + await asyncio.sleep(0) |
| 67 | + return input.btn_async() |
63 | 68 |
|
64 | | - @observe_async() |
| 69 | + @reactive.effect_async() |
65 | 70 | async def _(): |
66 | | - val = await btn_async() |
67 | | - print("@reactive_async() event: ", str(val)) |
| 71 | + val = await btn_async_r() |
| 72 | + print("@calc_async() event: ", str(val)) |
68 | 73 |
|
69 | | - @session.output("foo_async") |
| 74 | + @output() |
70 | 75 | @render_ui() |
71 | | - @event(lambda: session.input["btn_async"]) |
72 | | - async def _(): |
73 | | - return session.input["btn_async"] |
| 76 | + @event(btn_async_r) |
| 77 | + async def btn_async_value(): |
| 78 | + val = await btn_async_r() |
| 79 | + print("== " + str(val)) |
| 80 | + return str(val) |
74 | 81 |
|
75 | 82 |
|
76 | | -ShinyApp(ui, server).run() |
| 83 | +app = App(ui, server, debug=True) |
0 commit comments