From 3a744cef9f115808adf72bed278257ea6a7c9c38 Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Thu, 22 Aug 2024 09:32:16 -0400 Subject: [PATCH 1/3] docs: Add modal_show/remove examples --- shiny/api-examples/modal_remove/app-core.py | 45 +++++++++++++++++++ .../api-examples/modal_remove/app-express.py | 40 +++++++++++++++++ shiny/ui/_modal.py | 4 +- 3 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 shiny/api-examples/modal_remove/app-core.py create mode 100644 shiny/api-examples/modal_remove/app-express.py diff --git a/shiny/api-examples/modal_remove/app-core.py b/shiny/api-examples/modal_remove/app-core.py new file mode 100644 index 000000000..08e47b8a5 --- /dev/null +++ b/shiny/api-examples/modal_remove/app-core.py @@ -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) diff --git a/shiny/api-examples/modal_remove/app-express.py b/shiny/api-examples/modal_remove/app-express.py new file mode 100644 index 000000000..a79dbd0a5 --- /dev/null +++ b/shiny/api-examples/modal_remove/app-express.py @@ -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=2) + + # Now that we have model results, remove the modal + # and update the model result reactive value + ui.modal_remove() + model_result.set(result) diff --git a/shiny/ui/_modal.py b/shiny/ui/_modal.py index cdfc76f76..3e3923aac 100644 --- a/shiny/ui/_modal.py +++ b/shiny/ui/_modal.py @@ -158,7 +158,7 @@ def modal( ) -@add_example(ex_dir="../api-examples/modal") +@add_example(ex_dir="../api-examples/modal_remove") def modal_show(modal: Tag, session: Optional[Session] = None) -> None: """ Show a modal dialog. @@ -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. From 4832f8829df4e30eba9bf8a6fa325dea0b6af9d3 Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Thu, 22 Aug 2024 09:45:45 -0400 Subject: [PATCH 2/3] keep delays the same --- shiny/api-examples/modal_remove/app-express.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shiny/api-examples/modal_remove/app-express.py b/shiny/api-examples/modal_remove/app-express.py index a79dbd0a5..c1389e778 100644 --- a/shiny/api-examples/modal_remove/app-express.py +++ b/shiny/api-examples/modal_remove/app-express.py @@ -32,7 +32,7 @@ def do_run_model(): # Show the modal, blocking interaction with the UI ui.modal_show(the_modal()) - result = run_model(delay=2) + result = run_model(delay=4) # Now that we have model results, remove the modal # and update the model result reactive value From dfe8475f86b7e40510a6b385ef57fd72db4eadc9 Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Wed, 28 Aug 2024 16:13:59 -0400 Subject: [PATCH 3/3] Update shiny/ui/_modal.py --- shiny/ui/_modal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shiny/ui/_modal.py b/shiny/ui/_modal.py index 3e3923aac..a7dc57807 100644 --- a/shiny/ui/_modal.py +++ b/shiny/ui/_modal.py @@ -158,7 +158,7 @@ def modal( ) -@add_example(ex_dir="../api-examples/modal_remove") +@add_example(ex_dir="../api-examples/modal") def modal_show(modal: Tag, session: Optional[Session] = None) -> None: """ Show a modal dialog.