using-streamlit-session-state

Manage Streamlit app state across reruns using st.session_state.

Updated Jan 31, 2026
One-click install
npx skills add https://github.com/Mahaboob26/NEXUS-TRUSAI --skill using-streamlit-session-state-mahaboob26
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: using-streamlit-session-state
Source: https://github.com/Mahaboob26/NEXUS-TRUSAI/tree/main/.agents/skills/developing-with-streamlit/skills/using-streamlit-session-state
Command: npx skills add https://github.com/Mahaboob26/NEXUS-TRUSAI --skill using-streamlit-session-state-mahaboob26

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Streamlit reruns scripts top-to-bottom on every interaction, which resets variables unless you persist them. This Skill provides a practical pattern to keep values across reruns using st.session_state.

Core Features & Use Cases

  • Initialization patterns: initialize values with st.session_state.setdefault to avoid KeyError and ensure a known starting state.
  • Widget-state synchronization: connect widgets to session_state via keys so user inputs persist across interactions.
  • Callbacks and multi-page state: use per-widget callbacks to mutate session state and maintain consistent data when navigating between pages.
  • Use Case: build a multi-step form where user selections persist while navigating the app.

Quick Start

Open your Streamlit app and at the top of the script initialize per-user state, e.g. st.session_state.setdefault("user", None). Read and update the value via st.session_state["user"] or st.session_state.user in response to widget events.

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 reruns, bind widgets to st.session_state using keys. This ensures user inputs remain consistent and prevents variables from resetting on every interaction.

Why does my Streamlit app throw a KeyError when accessing session state?

A KeyError in Streamlit session state occurs when accessing uninitialized keys. Prevent this by using initialization patterns like st.session_state.setdefault to establish a known starting state before accessing values.

What is the best way to manage state in a multi-page Streamlit app?

The best way to manage state in a multi-page Streamlit app is using st.session_state with per-widget callbacks. This mutates session state safely and maintains consistent data when navigating between pages.

How do I initialize variables in Streamlit to avoid resetting data?

Initialize variables in Streamlit using st.session_state.setdefault at the top of your script. This establishes a known starting state and prevents data from resetting during top-to-bottom script reruns.

Can I use callbacks to update Streamlit session state?

Yes, you can use per-widget callbacks to update Streamlit session state. Assign callback functions to widgets to mutate session state directly in response to user interactions, ensuring persistent values.

Does Streamlit session state work for building multi-step forms?

Streamlit session state works perfectly for multi-step forms. By mapping widget keys to session state, user selections persist while navigating the app, ensuring data is not lost across reruns.