Skip to content

Commit c39de9b

Browse files
committed
move, edit, add examples for core module.py docs (with express example)
1 parent d8a9b94 commit c39de9b

File tree

3 files changed

+32
-90
lines changed

3 files changed

+32
-90
lines changed

shiny/api-examples/Module/app-core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def counter_ui(label: str = "Increment counter") -> ui.TagChild:
99
return ui.card(
1010
ui.h2("This is " + label),
1111
ui.input_action_button(id="button", label=label),
12-
ui.output_text_verbatim(id="out"),
12+
ui.output_text(id="out"),
1313
)
1414

1515

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from shiny import reactive
2+
from shiny.express import module, render, ui
3+
4+
5+
# ============================================================
6+
# Counter module
7+
# ============================================================
8+
@module
9+
def counter(input, output, session, label, starting_value: int = 0):
10+
count = reactive.value(starting_value)
11+
with ui.card():
12+
ui.h2(f"This is {label}")
13+
ui.input_action_button("button", f"{label}")
14+
15+
with ui.div():
16+
@render.text
17+
def out():
18+
return f"Click count is {count()}"
19+
20+
@reactive.effect
21+
@reactive.event(input.button)
22+
def _():
23+
count.set(count() + 1)
24+
25+
26+
# =============================================================================
27+
# App that uses module
28+
# =============================================================================
29+
counter("counter1", "Counter 1", starting_value=0)
30+
ui.hr()
31+
counter("counter2", "Counter 2", starting_value=0)

shiny/module.py

Lines changed: 0 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -47,51 +47,6 @@ def ui(fn: Callable[P, R]) -> Callable[Concatenate[str, P], R]:
4747
parameters accepted by `fn`. When called, it returns UI elements with input/output
4848
IDs automatically namespaced using the provided module `id`.
4949
50-
51-
Example
52-
-------
53-
54-
```python
55-
from shiny import App, module, reactive, render, ui
56-
57-
58-
@module.ui
59-
def counter_ui(label: str = "Increment counter") -> ui.TagChild:
60-
return ui.card(
61-
ui.h2("This is " + label),
62-
ui.input_action_button(id="button", label=label),
63-
ui.output_code(id="out"),
64-
)
65-
66-
67-
@module.server
68-
def counter_server(input, output, session, starting_value: int = 0):
69-
count: reactive.value[int] = reactive.value(starting_value)
70-
71-
@reactive.effect
72-
@reactive.event(input.button)
73-
def _():
74-
count.set(count() + 1)
75-
76-
@render.code
77-
def out() -> str:
78-
return f"Click count is {count()}"
79-
80-
81-
app_ui = ui.page_fluid(
82-
counter_ui("counter1", "Counter 1"),
83-
counter_ui("counter2", "Counter 2"),
84-
)
85-
86-
87-
def server(input, output, session):
88-
counter_server("counter1")
89-
counter_server("counter2")
90-
91-
92-
app = App(app_ui, server)
93-
```
94-
9550
See Also
9651
--------
9752
* Shiny Modules documentation: https://shiny.posit.co/py/docs/modules.html
@@ -131,50 +86,6 @@ def server(
13186
followed by any arguments expected by `fn`. When called, it will register
13287
the module's server logic in a namespaced context.
13388
134-
Example
135-
-------
136-
137-
```python
138-
from shiny import App, module, reactive, render, ui
139-
140-
141-
@module.ui
142-
def counter_ui(label: str = "Increment counter") -> ui.TagChild:
143-
return ui.card(
144-
ui.h2("This is " + label),
145-
ui.input_action_button(id="button", label=label),
146-
ui.output_code(id="out"),
147-
)
148-
149-
150-
@module.server
151-
def counter_server(input, output, session, starting_value: int = 0):
152-
count: reactive.value[int] = reactive.value(starting_value)
153-
154-
@reactive.effect
155-
@reactive.event(input.button)
156-
def _():
157-
count.set(count() + 1)
158-
159-
@render.code
160-
def out() -> str:
161-
return f"Click count is {count()}"
162-
163-
164-
app_ui = ui.page_fluid(
165-
counter_ui("counter1", "Counter 1"),
166-
counter_ui("counter2", "Counter 2"),
167-
)
168-
169-
170-
def server(input, output, session):
171-
counter_server("counter1")
172-
counter_server("counter2")
173-
174-
175-
app = App(app_ui, server)
176-
```
177-
17889
See Also
17990
--------
18091
* Shiny Modules documentation: https://shiny.posit.co/py/docs/modules.html

0 commit comments

Comments
 (0)