Skip to content
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

st.select_slider resets its state upon interaction(v0.84) #3600

Closed
asaini opened this issue Jul 23, 2021 · 2 comments
Closed

st.select_slider resets its state upon interaction(v0.84) #3600

asaini opened this issue Jul 23, 2021 · 2 comments
Assignees
Labels
type:bug Something isn't working type:regression This bug is a regression

Comments

@asaini
Copy link

asaini commented Jul 23, 2021

Summary

This came up in a forum issue, but it seems like we've broken something in st.select_slider: https://discuss.streamlit.io/t/streamlit-resetting-the-slider-for-v0-84-1/15125/2

Steps to reproduce

Code snippet:

import streamlit as st

color = st.select_slider(
     'Select a color of the rainbow',
     options=[['red', 'orange'], ['yellow', 'green'], ['blue', 'indigo'], ['violet']])

st.write(type(color))

# This line below seems to be the culprit. Commenting this seems to fix the issue
color.append('purple')


st.write(color)

Expected behavior:

Moving the slider should retain its widget state

Actual behavior:

Moving the slider resets its widget state right away.

I sometimes get this error message

Exception in thread ScriptRunner.scriptThread:
Traceback (most recent call last):
  File "/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/abhi/.local/share/virtualenvs/state-v2-tVNwr_p_/lib/python3.8/site-packages/streamlit/script_runner.py", line 182, in _process_request_queue
    widget_states = self._session_state.as_widget_states()
  File "/Users/abhi/.local/share/virtualenvs/state-v2-tVNwr_p_/lib/python3.8/site-packages/streamlit/state/session_state.py", line 471, in as_widget_states
    return self._new_widget_state.as_widget_states()
  File "/Users/abhi/.local/share/virtualenvs/state-v2-tVNwr_p_/lib/python3.8/site-packages/streamlit/state/session_state.py", line 197, in as_widget_states
    states = [
  File "/Users/abhi/.local/share/virtualenvs/state-v2-tVNwr_p_/lib/python3.8/site-packages/streamlit/state/session_state.py", line 200, in <listcomp>
    if self.get_serialized(widget_id)
  File "/Users/abhi/.local/share/virtualenvs/state-v2-tVNwr_p_/lib/python3.8/site-packages/streamlit/state/session_state.py", line 178, in get_serialized
    serialized = metadata.serializer(item.value)
  File "/Users/abhi/.local/share/virtualenvs/state-v2-tVNwr_p_/lib/python3.8/site-packages/streamlit/elements/select_slider.py", line 157, in serialize_select_slider
    return [index_(opt, u) for u in to_serialize]
  File "/Users/abhi/.local/share/virtualenvs/state-v2-tVNwr_p_/lib/python3.8/site-packages/streamlit/elements/select_slider.py", line 157, in <listcomp>
    return [index_(opt, u) for u in to_serialize]
  File "/Users/abhi/.local/share/virtualenvs/state-v2-tVNwr_p_/lib/python3.8/site-packages/streamlit/util.py", line 129, in index_
    raise ValueError("{} is not in iterable".format(str(x)))
ValueError: ['yellow', 'green', 'purple'] is not in iterable

Is this a regression?

Yes, verified that this works fine in versions prior to 0.84

Debug info

  • Streamlit version: 0.84.0
  • Python version: 3.8.7
@asaini asaini added type:bug Something isn't working status:needs-triage Has not been triaged by the Streamlit team labels Jul 23, 2021
@asaini asaini changed the title st.select_slider is resets its state upon interaction(v0.84) st.select_slider resets its state upon interaction(v0.84) Jul 23, 2021
@AnOctopus AnOctopus self-assigned this Jul 23, 2021
@vdonato vdonato added type:regression This bug is a regression and removed status:needs-triage Has not been triaged by the Streamlit team labels Jul 23, 2021
AnOctopus added a commit to AnOctopus/streamlit that referenced this issue Jul 26, 2021
streamlit#3600 is caused by the initial value of a widget, which is stored and
compared to internally, being returned directly as the widget's return
value. This is not a problem for value types, but if we return a mutable
reference type and the user mutates it later in the script, it changes
our stored copy, so we incorrectly think the initial value has changed
on the next run.

This is also a potential issue for values that are passed in, in various
ways, but handling them systematically will require some careful
checking. The reference type sharing introduces a potential for bugs
akin to "shotgun parsing", and Python is not very well equiped for
dealing with this kind of issue.

The unit test for this might make more sense elsewhere, but I couldn't
decide where.
AnOctopus added a commit that referenced this issue Jul 27, 2021
* Make copies of returned reference types stored in widget state

#3600 is caused by the initial value of a widget, which is stored and
compared to internally, being returned directly as the widget's return
value. This is not a problem for value types, but if we return a mutable
reference type and the user mutates it later in the script, it changes
our stored copy, so we incorrectly think the initial value has changed
on the next run.

This is also a potential issue for values that are passed in, in various
ways, but handling them systematically will require some careful
checking. The reference type sharing introduces a potential for bugs
akin to "shotgun parsing", and Python is not very well equiped for
dealing with this kind of issue.

The unit test for this might make more sense elsewhere, but I couldn't
decide where.
@vdonato
Copy link
Collaborator

vdonato commented Jul 28, 2021

Closing this since it was fixed in #3615, but the description for that PR didn't have the right keyword to link this issue

@vdonato vdonato closed this as completed Jul 28, 2021
@asaini
Copy link
Author

asaini commented Aug 5, 2021

Fixed in v0.86.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:bug Something isn't working type:regression This bug is a regression
Projects
None yet
Development

No branches or pull requests

3 participants