using-streamlit-session-state

Persist Streamlit session state across script reruns with widget keys and callbacks.

629|85|Updated May 4, 2020
One-click install
npx skills add https://github.com/andfanilo/streamlit-echarts --skill using-streamlit-session-state-andfanilo
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: using-streamlit-session-state
Source: https://github.com/andfanilo/streamlit-echarts/tree/main/.claude/skills/developing-with-streamlit/skills/using-streamlit-session-state
Command: npx skills add https://github.com/andfanilo/streamlit-echarts --skill using-streamlit-session-state-andfanilo

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Streamlit reruns reset local variables on every interaction, causing loss of user input and computed values; session state provides a persistent, per-user storage to retain those values across reruns.

Core Features & Use Cases

  • Persistent values: Initialize and persist counters, selections, and computed results so interactions do not reset the UI.
  • Widget synchronization: Associate widgets with keys to keep return values and session state in sync across components.
  • Callbacks and multipage sharing: Use callbacks to update state before reruns and share stateful data across pages and components.
  • Real-world example: Maintain a selected user or filter across navigation so details pages can reference the choice without re-selection.

Quick Start

Initialize keys at the top of your app with st.session_state.setdefault to persist counters, selections, and computed values across reruns.

Frequently Asked Questions about using-streamlit-session-state

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

FAQPage Schema
Why does my Streamlit app reset user inputs and local variables on every interaction?

Streamlit reruns scripts top-to-bottom on each interaction, resetting local variables. Session state provides persistent, per-user storage to retain user inputs, selections, and computed values across these reruns.

How do I initialize and persist a counter or selection in Streamlit session state?

Initialize keys at the top of your app using st.session_state.setdefault to persist counters, selections, and computed values. This ensures values are only set once and retained across subsequent script reruns.

How do I synchronize widget values with Streamlit session state?

Synchronize widget values by associating widgets with unique keys. This keeps return values and session state in sync across components, preventing desynchronization during script reruns.

Can I share session state data across multiple pages in a Streamlit multipage app?

Yes, you can share stateful data across pages and components in a Streamlit multipage app. Store selected values in session state so details pages can reference the choice without requiring re-selection.

When should I use callbacks to update Streamlit session state?

Use callbacks to update session state before reruns occur. This allows you to trigger state updates directly from widget interactions, ensuring state is correctly modified before the script executes again.

Do I need external dependencies or databases to manage per-user state in Streamlit?

No, Streamlit session state provides per-user per-tab session management natively without external dependencies. State is isolated to the user's session, requiring no database setup to persist values.