Issue with Streamlit Selectbox State Persistence Across Pages

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:

  1. 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.
  2. 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:

  1. Why does the first select box (‘Test’) not remember the selected option when navigating between pages?
  2. Why does the second select box (‘Test1’) require two clicks to update the selected option?
  3. 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!

Thank you!

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.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.