using-streamlit-session-state

Persist user data and widget values across Streamlit script reruns using st.session_state.

1|1|Updated Mar 20, 2026
One-click install
npx skills add https://github.com/Shamrock2245/shamrock-trading-bot --skill using-streamlit-session-state-shamrock2245
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: using-streamlit-session-state
Source: https://github.com/Shamrock2245/shamrock-trading-bot/tree/main/.agent/skills/developing-with-streamlit/skills/using-streamlit-session-state
Command: npx skills add https://github.com/Shamrock2245/shamrock-trading-bot --skill using-streamlit-session-state-shamrock2245

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Streamlit reruns the entire script on every interaction, causing variables to reset and making it difficult to maintain persistent state across user actions.

Core Features & Use Cases

  • Persistent dictionary: st.session_state stores values that survive reruns.
  • Widget binding: assigning a key to a widget automatically syncs it with session state.
  • Callbacks: on_change and on_click let you modify state before the script reruns.
  • Initialization patterns: use setdefault at the top of the app for clear state defaults.
  • Multi‑page sharing: share data between pages by reading and writing st.session_state variables.

Quick Start

Create a Streamlit app that sets st.session_state.setdefault('count', 0) at the top, shows the current count, and adds a button that increments the count, observing the value persist after each click.

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 data across Streamlit script reruns?

You persist data across Streamlit reruns by storing values in the st.session_state dictionary, which survives script reruns and prevents variables from resetting on every user interaction.

Why do my Streamlit widget values reset when I interact with the app?

Streamlit widget values reset because the framework reruns the entire script on every interaction. Binding widgets by assigning a key automatically syncs their values with st.session_state to maintain persistence.

How do I share data between multiple pages in a Streamlit webapp?

You share data across Streamlit pages by reading and writing variables to the st.session_state dictionary, allowing state variables to be accessed and updated globally across different pages of the webapp.

What is the best way to initialize default state in a Streamlit application?

The best way to initialize default state in Streamlit is using st.session_state.setdefault at the top of the app, which establishes clear state defaults before the script executes further rerun logic.

Can I modify Streamlit session state before a widget triggers a rerun?

Yes, you can modify Streamlit session state before a rerun by using on_change and on_click callbacks, which let you execute logic and update state values immediately before the script reruns.

Does using st.session_state work for managing interactive counters in Streamlit?

Yes, st.session_state works for interactive counters in Streamlit by storing the count value in the persistent dictionary and incrementing it via button clicks, ensuring the value persists across reruns.