using-streamlit-session-state

Persist and manage UI state across Streamlit reruns with st.session_state.

Updated Aug 23, 2026
One-click install
npx skills add https://github.com/n-elia/barbarapp --skill using-streamlit-session-state
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: using-streamlit-session-state
Source: https://github.com/n-elia/barbarapp/tree/main/.skills/developing-with-streamlit/skills/using-streamlit-session-state
Command: npx skills add https://github.com/n-elia/barbarapp --skill using-streamlit-session-state

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Streamlit reruns the entire script on every interaction, resetting non-persistent variables. This Skill shows how to use st.session_state to persist values across reruns, enabling stable, per-user experiences.

Core Features & Use Cases

  • Persist widget state across reruns by linking widgets to session_state via keys.
  • Initialize and safely access session data, avoiding KeyError and race conditions.
  • Implement per-page or multi-page state sharing and robust callbacks.

Quick Start

Set up initial state with st.session_state.setdefault("key", default), attach widgets with a key parameter (e.g., st.text_input("Name", key="user_name")), then read and update state from st.session_state in your callbacks and page logic.

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 widget state across Streamlit reruns?

Initialize st.session_state with setdefault, attach widgets using the key parameter, and access values directly in your page logic. This prevents variables from resetting on every script interaction and maintains stable per-user experiences.

Why does Streamlit reset my variables on every interaction?

Streamlit reruns the entire script on every interaction, resetting non-persistent variables. You need session_state to persist values across reruns, enabling stable state management for multi-page apps, complex widgets, and dynamic dashboards.

How do I safely initialize session state in Streamlit without KeyError?

Safely initialize session state in Streamlit by using st.session_state.setdefault("key", default) before accessing the value. This prevents KeyError and race conditions during script reruns by ensuring the key exists prior to widget rendering.

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

You can share session state across multiple pages in a Streamlit app using st.session_state. It enables per-page and multi-page state sharing, ensuring state consistency for per-user values throughout dynamic dashboards and complex widget interactions.

How do I implement callbacks with session state in Streamlit?

Implement callbacks in Streamlit by reading and updating st.session_state directly within your callback functions. Attach widgets with a key parameter, then use these callbacks to handle state changes safely without breaking reruns.

When do I need session state for a Streamlit dashboard?

You need session state for a Streamlit dashboard when maintaining state consistency and per-user values across reruns is crucial. It is specifically required for multi-page apps, complex widgets, and dynamic dashboards to prevent data loss.