using-streamlit-session-state

Installation
SKILL.md

Using Streamlit session state

Streamlit reruns scripts top-to-bottom on every interaction. Without session state, variables reset each time. Use st.session_state to persist values across reruns.

Basic usage

Session state is a dictionary-like object supporting attribute and bracket notation:

# Initialize with setdefault (preferred)
st.session_state.setdefault("count", 0)

# Alternative: check before setting
if "count" not in st.session_state:
    st.session_state.count = 0

# Read
current = st.session_state.count
Related skills

More from streamlit/agent-skills

Installs
3
GitHub Stars
186
First Seen
Mar 28, 2026