selectbox does not play well with session state persistence
See original GitHub issueSummary
When using selectbox together with session state persistence code found at https://gist.github.com/tvst/036da038ab3e999a64497f42de966a92, I need to select the same item two times in row for it to be actually selected. The first time I select it, the selectbox automatically restores the previous value.
Steps to reproduce
Code snippet:
import SessionState
import streamlit as st
options = ["option 1", "option 2"]
session_state = SessionState.get(option='-')
try:
index = options.index(session_state.option)
except ValueError:
index = 0
session_state.option = st.sidebar.selectbox('Options', options, index=index)
- At startup, the select box displays “option 1”.
- Select “option 2”, it works, the first selection after startup always works.
- Select “option 1”, the select box displays “option 1” during a very short period of time and then restores the select box content to “option 2”.
- Select “option 1” again, it works.
Expected behavior:
I expect step 3 to provide “option 1”
Actual behavior:
Step 3 provides “option 2”.
Is this a regression?
That is, did this use to work the way you expected in the past? I don’t know. I observed the same behavior in Streamlit version 0.81.1.
Debug info
- Streamlit version: version 0.82.0
- Python version: Python 3.8.9
- pyenv 1.2.27
- OS version: macOS Big Sur 11.3.1
- Browser version: Firefox 88.0.1 / Safari 14.1
Issue Analytics
- State:
- Created 2 years ago
- Comments:5
Top Results From Across the Web
State value doesn't seem to persist for a Select element ...
Hi I have a very simple example (in Next JS) that doesn't seem to work, where I have created a basic store: interface...
Read more >How to Maintain Persistence State of a Dropdown with ...
But for the drop-down I happen to be using, I cannot get it to persist. Well, I can, but then the default selection...
Read more >Multi-page app with session state - Streamlit
What I am trying to do is to not have state.input depend on state.selectbox if text is written into the text_input. If a...
Read more >Session State for Streamlit - Towards Data Science
But with Session State, it's possible to have values persist across reruns for those instances when you don't want your variables reinitialized.
Read more >DynamicModule persistent UI changes - Mathematica Stack ...
Lastly, be sure the checkbox is on, and close the notebook. Note that there's no external or internal clue about the notebook saved...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@vincbo
With the recent release of session state in core streamlit, the snippet you provided can now be written in a cleaner and more concise way that does not have this issue. Since widgets are integrated into the session state api, there is no longer a need for manually syncing the widget and session state values. This snippet works and I think does what the original wanted to do:
The selected value resetting used to be caused by the changing index argument giving the widget a new identity, but now we do it deliberately to facilitate live-editing scripts. I’m starting a discussion internally to evaluate that use case versus this being a potential footgun. The way to make it work correctly is to only ever set a
value
orindex
to a constant, and use the session state api to get/set the widget value instead of trying to manually sync them.Going to close this issue with a new, preferred, solution.