I’m using a Streamlit App called Benefit Buddy. When I type in 2026 or choose it from the drop down menu it won’t stay put. It reverts back to 2025 each time. When you open the app, it defaults to 2026 until you type in a Member ID, then it reverts to 2025. Please advise on what I might do to get 2026 to stay. The year 2025 is almost over and the benefits of 2026 are relevant.
This happens because Streamlit reruns the app on every input change. When you enter the Member ID, the script reruns and your year widget resets to its default (2025).
Fix:store the selected year in st.session_state so it persists across reruns.
import streamlit as st
if "year" not in st.session_state:
st.session_state.year = 2026
st.selectbox(
"Benefit Year",
[2025, 2026],
key="year"
)
st.text_input("Member ID")
This ensures 2026 stays selected even after entering a Member ID.
Just to clarify, you don’t need to manually store widget values in Session State. Widgets are stateful by default. However, if you change a widget’s identity, that can cause it to reset.
If you don’t set a key for your widget, changing the widget label or default value will reset it. If you set a key for your widget it’s a little more complicated because we are in the middle of implementing a behavior change:
Soon, it will be the case that if you set a key for a widget, then its identity will be stable no matter what other parameters you change. However, right now, even if you set a key, changing options(or min/max on numeric widgets) will still cause it to reset.
For more information about widget behavior and identity, see Widget behavior - Streamlit Docs