Skip to main content

Persistent data across page reloads for Streamlit applications

Project description

Streamlit Persist Session

A Python library/utility designed for Persistent data across page reloads in Streamlit applications using cryptographically signed browser cookies and local state files.

Purpose

The primary goal of streamlit-persist-session is to provide Persistent data across page reloads.

By default, Streamlit's built-in st.session_state resets whenever a user refreshes/reloads their browser tab or when the WebSocket connection is interrupted. This component bypasses that limitation by automatically preserving decorated class instances on the server side, indexing them via a unique session ID stored securely in the client's browser cookies.

Features

  • Automatic Persistence: A simple class decorator (@streamlit_persist_session.persist) wraps your state class, intercepting attribute reads/writes to keep the state synchronized.

    [!IMPORTANT] The decorated class must be pickleable (i.e., defined at the module level, not nested inside functions or other classes) because the state is serialized using Python's pickle module.

  • Secure by Design:
    • Cryptographically signs session cookies using HMAC-SHA256 with a unique server-side secret key to prevent client-side tampering.
    • Strict input validation of session IDs (UUIDv4) to prevent path traversal or file injection attacks.
  • Session Lifecycles: Supports explicit state clearing via state.clear_cookie_state().

Key Architecture

streamlit-persist-session wraps streamlit_cookies_controller to implement:

  1. Cookie Key Allocation & HMAC Verification: Allocates a unique identifier to the browser cookies and runs an authenticity check using HMAC signatures.
  2. Data Storage & Restoration: Automatically serializes python objects to local server files on attribute mutation, and restores the original state on page reload.

Integration with Authentication

[!NOTE] If your application implements login session management using Streamlit's native st.login(), this manually managed cookie state functionality might overlap. In such cases, you should reference st.user to manage user sessions and state rather than using this component.

Usage Example

Here is a basic example of how to persist a simple counter class.

import streamlit_persist_session

# Note: The decorated class must be pickleable.
@streamlit_persist_session.persist("app-state")
class AppState:
    def __init__(self):
        self.count = 0

    # ... see main.py for full example

Directory Structure

Prerequisites

Ensure you have the following installed:

  • Python 3.13 or higher
  • uv (recommended Python package and workflow manager)
  • mise (optional, runtime management tool)

Getting Started

1. Installation & Dependency Setup

Initialize and synchronize the virtual environment with the necessary dependencies:

uv sync

2. Run the Demo Application

Launch the interactive Streamlit dashboard:

uv run streamlit run main.py

If the browser does not open automatically, visit the URL output in your terminal (usually http://localhost:8501).

How It Works

  1. Instantiation & Placeholder: When the app first loads, the custom decorator checks if the session cookie exists. If the cookies are not yet loaded, a temporary placeholder instance is returned while the cookie controller retrieves data.
  2. State Serialization:
    • Every modification to the state attributes triggers a serialization step (pickle.dump) to a secure temporary file on the server.
    • The filename is a random UUIDv4, which is stored as a signed value (<UUID>.<HMAC-signature>) in the browser's cookies.
  3. Restoration: On page reload or session reconnection, the cookie is read, signature verified, and the state object is deserialized (pickle.load), keeping the state intact.
  4. Clean up: When clicking the "Clear cookies and temporary files" button, the temporary file is deleted, browser cookies are removed, and st.session_state is updated.

License

This project is licensed under the MIT License. See the LICENSE file for the full license text.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

streamlit_persist_session-0.1.0.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

streamlit_persist_session-0.1.0-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file streamlit_persist_session-0.1.0.tar.gz.

File metadata

  • Download URL: streamlit_persist_session-0.1.0.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for streamlit_persist_session-0.1.0.tar.gz
Algorithm Hash digest
SHA256 49d74e62bcef4f5a780d03db5e5c3431e061e4f1ad2cb41e4de284a78d554a4e
MD5 1ba53e790d5b3215f10945f6ab5572ff
BLAKE2b-256 f8d1c5e562db93b44a734ad25a9f9a8d053e1db6b33702a26bc7d7fd8a5226da

See more details on using hashes here.

File details

Details for the file streamlit_persist_session-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: streamlit_persist_session-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for streamlit_persist_session-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3e3c4d0f4b3979d22aa020143e06096c50cbfcbfff8a0d5ff0389cd7a44547cf
MD5 694ee681d72c0e662d8a31d2b6153578
BLAKE2b-256 cb59052f6be711bd8005b548d87106966472efd4291d74108e1f7a4d4c353d82

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page