using-streamlit-session-state

Persist Streamlit widget values across reruns using st.session_state.

Updated Jan 27, 2026
One-click install
npx skills add https://github.com/erickfmm/transformer-encoder-frankestein --skill using-streamlit-session-state-erickfmm
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: using-streamlit-session-state
Source: https://github.com/erickfmm/transformer-encoder-frankestein/tree/main/.agents/skills/developing-with-streamlit/skills/using-streamlit-session-state
Command: npx skills add https://github.com/erickfmm/transformer-encoder-frankestein --skill using-streamlit-session-state-erickfmm

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

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.

Core Features & Use Cases

  • Session-state persistence: retain values across reruns so widgets keep their state during interaction flows.
  • Initialization patterns: safely initialize defaults and guard against missing keys.
  • Callbacks and widget synchronization: wire on_change/on_click handlers to update session state and reflect changes across components.
  • Use cases: multi-page apps, multi-step forms, and dashboards where user preferences persist.

Quick Start

Initialize the session state at the top of your app using st.session_state.setdefault("key", default) and reference the keys in your widgets.

Frequently Asked Questions about using-streamlit-session-state

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I persist Streamlit widget values across script reruns?

Use st.session_state to persist Streamlit widget values across script reruns. It stores per-user state so variables and interactions do not reset when the script executes from top to bottom on user input.

What is the best way to initialize default values in Streamlit session state?

The best way to initialize Streamlit session state is using st.session_state.setdefault at the top of your app. This safely establishes default values and guards against missing keys during subsequent reruns.

How do Streamlit callbacks synchronize with session state?

Streamlit callbacks synchronize with session state by wiring on_change and on_click handlers to widgets. These handlers update session state keys directly, ensuring component changes reflect instantly across the app.

Can I use Streamlit session state for multi-page apps and multi-step forms?

Yes, you can use Streamlit session state for multi-page apps and multi-step forms. It maintains user preferences and interaction flows by retaining per-user state as users navigate between pages or steps.

Why do my Streamlit variables reset every time I interact with a widget?

Streamlit variables reset because the framework reruns the entire script top-to-bottom on every widget interaction. Implementing st.session_state persistence prevents this reset by retaining values across reruns.

Does using session state in Streamlit require external dependencies?

No, using Streamlit session state requires no external dependencies. It relies entirely on the built-in st.session_state object for key management, default initialization, and per-widget synchronization.