Skip to content

Commit 0f9ec9e

Browse files
authored
docs: Add modal_show/remove examples (#1628)
1 parent 52e60e8 commit 0f9ec9e

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from shiny import App, Inputs, Outputs, Session, reactive, ui
2+
3+
4+
def run_model(delay=10.0):
5+
import time
6+
7+
# Pretend to run a model for `delay` seconds
8+
start_time = time.time()
9+
while time.time() - start_time < delay:
10+
pass
11+
return time.time()
12+
13+
14+
def the_modal():
15+
return ui.modal(
16+
"The model is running, please wait.",
17+
title="Running model",
18+
easy_close=False,
19+
footer=None,
20+
)
21+
22+
23+
app_ui = ui.page_fluid(
24+
ui.input_action_button("run", "Run Model"),
25+
)
26+
27+
28+
def server(input: Inputs, output: Outputs, session: Session):
29+
model_result = reactive.value()
30+
31+
@reactive.effect
32+
@reactive.event(input.run)
33+
def do_run_model():
34+
# Show the modal, blocking interaction with the UI
35+
ui.modal_show(the_modal())
36+
37+
result = run_model(delay=4)
38+
39+
# Now that we have model results, remove the modal
40+
# and update the model result reactive value
41+
ui.modal_remove()
42+
model_result.set(result)
43+
44+
45+
app = App(app_ui, server)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from shiny import reactive
2+
from shiny.express import input, ui
3+
4+
5+
def run_model(delay=10.0):
6+
import time
7+
8+
# Pretend to run a model for `delay` seconds
9+
start_time = time.time()
10+
while time.time() - start_time < delay:
11+
pass
12+
return time.time()
13+
14+
15+
ui.input_action_button("run", "Run Model")
16+
17+
model_result = reactive.value()
18+
19+
20+
def the_modal():
21+
return ui.modal(
22+
"The model is running, please wait.",
23+
title="Running model",
24+
easy_close=False,
25+
footer=None,
26+
)
27+
28+
29+
@reactive.effect
30+
@reactive.event(input.run)
31+
def do_run_model():
32+
# Show the modal, blocking interaction with the UI
33+
ui.modal_show(the_modal())
34+
35+
result = run_model(delay=4)
36+
37+
# Now that we have model results, remove the modal
38+
# and update the model result reactive value
39+
ui.modal_remove()
40+
model_result.set(result)

shiny/ui/_modal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def modal_show(modal: Tag, session: Optional[Session] = None) -> None:
184184
session._send_message_sync({"modal": {"type": "show", "message": msg}})
185185

186186

187-
@add_example(ex_dir="../api-examples/modal")
187+
@add_example()
188188
def modal_remove(session: Optional[Session] = None) -> None:
189189
"""
190190
Remove a modal dialog box.

0 commit comments

Comments
 (0)