-
Notifications
You must be signed in to change notification settings - Fork 100
[Bug]: use for loop to assign multiple modal to multiple button fail #1926
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
Comments
This is expected behavior, because of how Python captures the variable Each time you define the function in the loop, Python does not evaluate the value of Here's an example that doesn't involve Shiny at all. We'll define a function four times in a loop, and store it in a list of functions called fns = []
for i in range(0,4):
def f():
print(f"This is message {i}.")
fns.append(f)
# Now call each of the four functions:
fns[0]()
#> This is message 3.
fns[1]()
#> This is message 3.
fns[2]()
#> This is message 3.
fns[3]()
#> This is message 3. We called the four separate functions, and each one printed the value They really are different functions: fns
#> [<function f at 0x13ed720>, <function f at 0x129ba80>, <function f at 0x11ca2e0>, <function f at 0x1d7f710>]
fns[0] is fns[1]
#> False
fns[1] is fns[2]
#> False
fns[2] is fns[3]
#> False If we change the value of i = 100
fns[0]()
#> This is message 100.
fns[1]()
#> This is message 100.
fns[2]()
#> This is message 100.
fns[3]()
#> This is message 100. So that's what happened: the function looks at the value of To work around this, you can write a function that creates the from shiny import App, render, ui, reactive
app_ui = ui.page_fluid(
ui.input_action_button("show1", "Show doc1"),
ui.input_action_button("show2", "Show doc2"),
ui.input_action_button("show3", "Show doc3"),
)
def server(input, output, session):
def create_button_effect(n):
@reactive.effect
@reactive.event(input[f"show{n}"])
def _():
m = ui.modal(
f"This is a somewhat important message.{n}",
title="Somewhat important message",
easy_close=True,
)
ui.modal_show(m)
for i in range(1, 4):
create_button_effect(i)
app = App(app_ui, server) The loop calls In other words, in this example, each of the |
Component
UI (ui.*)
Severity
P0 - Critical (crash/unusable)
Shiny Version
1.3.0
Python Version
3.12
Minimal Reproducible Example
Behavior
Current: click on each button will display
for all modal.
Expect: want to display
for each button
Error Messages (if any)
Environment
The text was updated successfully, but these errors were encountered: