using-streamlit-session-state

Persist session values across Streamlit script reruns using key-based widget binding.

Updated Apr 20, 2025
One-click install
npx skills add https://github.com/cathayrisk/Anya --skill using-streamlit-session-state-cathayrisk
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: using-streamlit-session-state
Source: https://github.com/cathayrisk/Anya/tree/main/skills/developing-with-streamlit/skills/using-streamlit-session-state
Command: npx skills add https://github.com/cathayrisk/Anya --skill using-streamlit-session-state-cathayrisk

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Streamlit reruns the script top-to-bottom on every interaction, which causes in-memory variables and widget values to reset; this Skill explains how to persist values and user inputs across reruns so apps remain interactive and stateful.

Core Features & Use Cases

  • Persist per-user, per-tab state across Streamlit reruns using session-backed variables.
  • Sync widget values to named keys so form inputs, sliders, and selections survive interactions.
  • Use callbacks to update state immediately before a rerun and follow initialization patterns to avoid KeyError and conflicting value warnings.
  • Use Case: Build a multipage Streamlit app that remembers a selected user, applied filters, and UI theme when navigating between pages and across widget interactions.

Quick Start

Initialize session variables at the top of your Streamlit app and bind widgets to stable keys so values persist across interactions.

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?

To persist Streamlit widget values across script reruns, bind widgets to stable keys and initialize those keys in st.session_state. This ensures user inputs like sliders and form selections survive top-to-bottom script reruns.

Why does my Streamlit app lose its state when navigating between multipage pages?

Streamlit apps lose state between multipage pages when variables are not stored in st.session_state. You must initialize per-user, per-tab session variables at the top of your script to retain applied filters and UI state.

How do I use callbacks to update Streamlit session state before a rerun?

Use Streamlit callbacks to update session state immediately before a rerun executes. Binding callback functions to widget keys ensures values are safely updated without triggering conflicting value warnings or KeyError exceptions.

How do I avoid KeyError when accessing session state in Streamlit?

Avoid KeyError in Streamlit session state by using safe initialization patterns. Check if a key exists in st.session_state before accessing it, setting default values at the top of your script to prevent runtime warnings.

Can I share data between pages in a Streamlit multipage app?

You can share data between pages in a Streamlit multipage app by storing values in st.session_state. This per-user, per-tab state persistence allows you to pass selected user data and applied filters across different pages.

What is the best way to manage per-user state in Streamlit applications?

The best way to manage per-user state in Streamlit applications is using st.session_state. It provides per-tab state persistence for widgets and callbacks, ensuring interactive apps retain user inputs across interactions.