posit-dev / posit-dev/py-shiny
Trouble with reactive.Value containing a mutable object
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.8k
- Forks
- 135
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 21
Description
I've run into some odd behavior and I'm hoping it's user error. I've created a simple app with a reactive.Value() containing a list. Some events trigger reactives that mutate that list (because lists are mutable), and I can see that the list is getting modified, but a UI element that should show the current state of the list isn't being updated. Code below, and working example on shinylive.
from shiny import reactive, render, ui, App
app_ui = ui.page_fluid(
ui.row(
ui.column(
2,
ui.row(
ui.input_action_button("add_value", "Add to list"),
),
ui.row(
ui.input_action_button("remove_value", "Remove from list"),
),
),
ui.column(
10,
ui.tags.div(id="module_container"),
),
),
ui.output_text_verbatim("all_values")
)
def server(input, output, session):
value_list = reactive.Value([])
@reactive.Effect
@reactive.event(input.add_value)
def _():
id = "value_" + str(len(value_list.get()))
ui.insert_ui(
selector="#module_container", where="afterBegin", ui=ui.input_numeric(id, id, 0)
)
value_list.get().append(getattr(input, id))
@reactive.Effect
@reactive.event(input.remove_value)
def _():
ui.remove_ui(selector=f"#module_container .shiny-input-container:first-child")
value_list.get().pop()
@output
@render.text
def all_values():
return str(value_list())
app = App(app_ui, server)
all_values() depends on value_list(), and I'm changing the contents of value_list but all_values() isn't updating, even though I can see that the list inside is growing when I click "add to list" because the element label is a function of the length of the list.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the supplied app, focusing on reactive.Value([]), the add/remove event handlers, and the all_values() renderer. Trace how the reactive value is read by @render.text and how the in-place list mutations are observed; done when the expected update behavior is established and covered by a regression check.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100