using-streamlit-session-state

Persist per-user UI state across Streamlit reruns with st.session_state.

Updated Feb 26, 2026
One-click install
npx skills add https://github.com/NickEsColR/gemini-rag-demo --skill using-streamlit-session-state-nickescolr
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: using-streamlit-session-state
Source: https://github.com/NickEsColR/gemini-rag-demo/tree/main/.agents/skills/developing-with-streamlit/skills/using-streamlit-session-state
Command: npx skills add https://github.com/NickEsColR/gemini-rag-demo --skill using-streamlit-session-state-nickescolr

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 and keep UI consistent.

Core Features & Use Cases

  • Persistence of widget values and arbitrary data across reruns using st.session_state
  • Synchronization of widgets with keys to enable per-user state and cross-page usage
  • Debugging tips and initialization patterns for robust multi-page apps

Quick Start

Initialize a session key with st.session_state.setdefault("value", 0) and read or update it across 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 reruns?▼

You persist Streamlit widget values across reruns by storing them in st.session_state, which prevents variables from resetting when the script executes top-to-bottom on every user interaction. Assign widget keys to synchronize and retain their specific values automatically.

Why does my Streamlit app reset variables on every interaction?▼

Your Streamlit app resets variables because it reruns the entire script top-to-bottom on each interaction. You need to use st.session_state to persist per-user UI state and arbitrary data, keeping your dashboard or web app consistent throughout these reruns.

What is the best way to initialize session state in a Streamlit multi-page app?▼

The best way to initialize session state in a Streamlit multi-page app is using st.session_state.setdefault("value", 0). This initialization pattern safely establishes default values without overwriting existing ones, ensuring robust cross-page state synchronization.

How do I sync Streamlit widgets with session state keys?▼

You sync Streamlit widgets with session state keys by assigning a unique key parameter to each widget. This synchronization links the widget directly to st.session_state, enabling per-user state retention and safe, callback-driven updates across your web app.

Can I use callbacks to safely update Streamlit session state?▼

Yes, you can use callbacks to safely update Streamlit session state. Callback-driven updates ensure that widget interactions modify st.session_state values securely during reruns, providing debugging tips and initialization patterns for robust multi-page applications.