Skip to content

Commit

Permalink
docs: Add modal_show/remove examples (#1628)
Browse files Browse the repository at this point in the history
  • Loading branch information
gadenbuie authored Aug 29, 2024
1 parent 52e60e8 commit 0f9ec9e
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
45 changes: 45 additions & 0 deletions shiny/api-examples/modal_remove/app-core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from shiny import App, Inputs, Outputs, Session, reactive, ui


def run_model(delay=10.0):
import time

# Pretend to run a model for `delay` seconds
start_time = time.time()
while time.time() - start_time < delay:
pass
return time.time()


def the_modal():
return ui.modal(
"The model is running, please wait.",
title="Running model",
easy_close=False,
footer=None,
)


app_ui = ui.page_fluid(
ui.input_action_button("run", "Run Model"),
)


def server(input: Inputs, output: Outputs, session: Session):
model_result = reactive.value()

@reactive.effect
@reactive.event(input.run)
def do_run_model():
# Show the modal, blocking interaction with the UI
ui.modal_show(the_modal())

result = run_model(delay=4)

# Now that we have model results, remove the modal
# and update the model result reactive value
ui.modal_remove()
model_result.set(result)


app = App(app_ui, server)
40 changes: 40 additions & 0 deletions shiny/api-examples/modal_remove/app-express.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from shiny import reactive
from shiny.express import input, ui


def run_model(delay=10.0):
import time

# Pretend to run a model for `delay` seconds
start_time = time.time()
while time.time() - start_time < delay:
pass
return time.time()


ui.input_action_button("run", "Run Model")

model_result = reactive.value()


def the_modal():
return ui.modal(
"The model is running, please wait.",
title="Running model",
easy_close=False,
footer=None,
)


@reactive.effect
@reactive.event(input.run)
def do_run_model():
# Show the modal, blocking interaction with the UI
ui.modal_show(the_modal())

result = run_model(delay=4)

# Now that we have model results, remove the modal
# and update the model result reactive value
ui.modal_remove()
model_result.set(result)
2 changes: 1 addition & 1 deletion shiny/ui/_modal.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def modal_show(modal: Tag, session: Optional[Session] = None) -> None:
session._send_message_sync({"modal": {"type": "show", "message": msg}})


@add_example(ex_dir="../api-examples/modal")
@add_example()
def modal_remove(session: Optional[Session] = None) -> None:
"""
Remove a modal dialog box.
Expand Down

0 comments on commit 0f9ec9e

Please sign in to comment.