Subject: Issue with Streamlit Selectbox State Persistence Across Pages
Hi everyone,
I’m facing an issue with state persistence in my Streamlit application. Below is a snippet of my code:
if 'Test' not in st.session_state:
st.session_state['Test'] = None
if 'Test1' not in st.session_state:
st.session_state['Test1'] = None
st.session_state['Test'] = col1_page1.selectbox('Test', options=['A', 'B', 'C'], index=0)
st.session_state['Test1'] = col1_page1.selectbox('Test1', options=['A', 'B', 'C'], index=['A', 'B', 'C'].index(st.session_state['Test1']) if st.session_state['Test1'] is not None else 0)
Problem Description:
When I enter my Streamlit application and choose an option in the first select box (‘Test’), then navigate to another page via the sidebar and return to the first page, the selected option is reset and not remembered.
For the second select box (‘Test1’), I have managed to fix this issue, and the selected option saved when navigating between pages. However, when I try to choose a different option, the select box requires two clicks to update, and st.session_state['Test1'] changes only on the second click.
Questions:
Why does the first select box (‘Test’) not remember the selected option when navigating between pages?
Why does the second select box (‘Test1’) require two clicks to update the selected option?
Do you have any suggestions or better methods to save the selected index across pages without initializing it?
I’ve included a reproducible example in the code snippet above. Any insights or suggestions would be greatly appreciated!
Use the key of the widget to get the value of widget. Use the on_change or on_click parameters to have an efficient streamlit app.
import streamlit as st
from streamlit import session_state as ss
TEST_OPTIONS = ['nan', 'A', 'B', 'C']
# user defined variable, this can persist in
# other page in a multipage app
if 'Test' not in ss:
ss['Test'] = TEST_OPTIONS[0]
def test_cb():
"""Called when there is a change in Test selectbox."""
# Store the value of the Test selectbox using its key, in the user
# defined variable in the session state with a key Test.
ss['Test'] = ss['testk']
def main():
# Widget
st.selectbox(
label='Test',
options=TEST_OPTIONS,
index=0,
key='testk', # used to get the value of widget
on_change=test_cb # called when there is change in the widget
)
# Shows the current value of Test selectbox. Notice how
# the value of widget is extracted.
st.text(f'test selectbox value is {ss["testk"]}')
if __name__ == '__main__':
main()
In the code above, if you change page and comeback, it will always start at index 0, showing the value nan. To solve this issue modify the index to start on the current value of the widget.
index=TEST_OPTIONS.index(ss['Test']),
Stop using the following.
st.session_state['Test'] = col1_page1.selectbox()
Practice using the key of the widget to get the value of widget (via the session state). And if you want to use the value of the widget elsewhere, create a session variable and save the value of the widget using the on_change or on_click for buttons parameters.
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.