I am having some troubles with the initialization of widgets using the session state.
In the given example below, the first selectobox is properly initialised using the value stored in the session_state. However, if the toggle is toggled, the initial status of the second selectbox is incorrect.
How can I fix that?
import streamlit as st
# Initialize session state values if not already set
if 'value1' not in st.session_state:
st.session_state['value1'] = 'A'
if 'value2' not in st.session_state:
st.session_state['value2'] = 'A'
# Create the toggle button
toggle = st.toggle('Toggle')
# Render the first selectbox
st.selectbox('Selectbox 1', ['B', 'D', 'A', 'C'], key='value1')
# Render the second selectbox based on the toggle state
if toggle:
st.selectbox('Selectbox 2', ['B', 'D', 'A', 'C'], key='value2')
# Optional: Output the current session state for debugging
st.write(st.session_state)
Hi,
I thing there is a missunderstanding with how session state work.
The session state is initialized correctly, but the selectbox widgets are overwriting these values.
When you use a key parameter in a Streamlit widget that matches a session state key, the widget will both read from and write to that session state value.
In this case, the selectboxes with key='value1' and key='value2' are overwriting the initial session state values each time the script runs.
The selectboxes default to the first option (‘B’) if no index is specified, which is why you’re not seeing the initial ‘A’ values preserved.
To fix this, you should either:
Use the index parameter in the selectboxes to set their initial values based on the session state, or
Use different keys for the selectboxes that don’t conflict with your session state variables.
It will work :
# Initialize session state values if not already set
if 'value1' not in st.session_state:
st.session_state['value1'] = 2
if 'value2' not in st.session_state:
st.session_state['value2'] = 2
# Create the toggle button
toggle = st.toggle('Toggle')
# Render the first selectbox
st.selectbox('Selectbox 1', ['B', 'D', 'A', 'C'], key='1', index=st.session_state['value1'])
# Render the second selectbox based on the toggle state
if toggle:
st.selectbox('Selectbox 2', ['B', 'D', 'A', 'C'], key='2', index=st.session_state['value2'])
# Optional: Output the current session state for debugging
st.write(st.session_state)
Thanks for the reply!
What is confusing is that the first selectbox is properly initialised via the session state and does display ‘A’ on the first run which mean the widget is initialising itself using the value stored in the session state (?!). However, the second selectbox is not properly initialised like the first one while they have both been initialised exactly the same way
The key issue is that when the second selectbox appears for the first time (when you activate the toggle), Streamlit creates a new widget instance. At this point, it doesn’t use the value from the session state but instead uses the first value in the options list as the default value, which is ‘B’ in this case.
This ensures that even when the selectbox is rendered for the first time, it will use the correct value from the session state. (And it avoid any overwritting problem).
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.