Saving data in a multi-page format

Dear community, I’m working on a multi-page Streamlit application, with different data entries as sliders, text and number inputs, selectbox,… I have a problem with my code.

values = list(range(0, 999))
cote = st.select_slider(t("cote"), options=values,                                   value=st.session_state.form_data["cote"])
st.session_state.form_data["cote"] = cote

First change on the slider is working (data is saved), second one is not taken into account (slider return to initial position), third is working, fourth not, and so on… Does anybody have an idea of what is the problem?

I save my data in session_state.form_data to be able to go and comeback on my Streamlit application’s different pages.

Thanks a lot in advance

Welcome to the community and thanks for your detailed question! :balloon: The issue you’re seeing—where the select_slider only updates every other time—is a classic Streamlit session state pitfall. This happens because you’re both reading and writing to st.session_state.form_data["cote"] in the same script run, which causes Streamlit to get out of sync with the widget’s state. This is a well-documented behavior in Streamlit.

To fix it, you should use the key parameter in your widget and let Streamlit manage the state, or use a callback function to update your session state. Here’s the recommended approach using a widget key:

values = list(range(0, 999))
if "form_data" not in st.session_state:
    st.session_state.form_data = {"cote": 0}

cote = st.select_slider(
    t("cote"),
    options=values,
    value=st.session_state.form_data["cote"],
    key="cote_slider"
)
st.session_state.form_data["cote"] = st.session_state["cote_slider"]

This ensures the widget and session state stay in sync on every interaction. For more details, see the official Streamlit widget updating/session state FAQ.

Sources:

1 Like

Thanks a lot for your answer!!