-
Notifications
You must be signed in to change notification settings - Fork 100
docs: Add modal_show/remove examples #1628
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3a744ce
docs: Add modal_show/remove examples
gadenbuie 4832f88
keep delays the same
gadenbuie dfe8475
Update shiny/ui/_modal.py
gadenbuie 614233e
Merge branch 'main' into docs/ui-remove-modal
gadenbuie 3e86f50
Merge branch 'main' into docs/ui-remove-modal
gadenbuie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we're going to set a reactive val here, let's display it somewhere? I'd also be fine getting rid of it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should get rid of it; it's a core part of the pattern. On the other hand I don't think it's worth showing the value; that ends up just being something to delete if you use this example as a template.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a core part of a pattern, but not everyone coming to
modal_remove()
wants that pattern (e.g., maybe they just want to delay n seconds, or provide another UI control to close)? IMO introducing state that isn't actually used has potential to cause confusion. That said, I ultimately don't want to dwell on such a minor detail.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lol very true. I meant "the pattern being demonstrated", not the only pattern.
I probably wouldn't have included it either, but I've independently shown this pattern to 2-4 different people asking about how to remove the modal.