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
picklemodule. - 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:
- Cookie Key Allocation & HMAC Verification: Allocates a unique identifier to the browser cookies and runs an authenticity check using HMAC signatures.
- 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 referencest.userto 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
- src/streamlit_persist_session/: Core package containing the decorator logic.
- init.py: Package entry point exposing the decorator.
- _persister.py: Internal implementation of the state persistence.
- main.py: A demo Streamlit application demonstrating state persistence.
- pyproject.toml: Project metadata, package definition, and dependencies.
- README.md: This documentation.
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
- 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.
- 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.
- Every modification to the state attributes triggers a serialization step (
- 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. - Clean up: When clicking the "Clear cookies and temporary files" button, the temporary file is deleted, browser cookies are removed, and
st.session_stateis updated.
License
This project is licensed under the MIT License. See the LICENSE file for the full license text.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
49d74e62bcef4f5a780d03db5e5c3431e061e4f1ad2cb41e4de284a78d554a4e
|
|
| MD5 |
1ba53e790d5b3215f10945f6ab5572ff
|
|
| BLAKE2b-256 |
f8d1c5e562db93b44a734ad25a9f9a8d053e1db6b33702a26bc7d7fd8a5226da
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3e3c4d0f4b3979d22aa020143e06096c50cbfcbfff8a0d5ff0389cd7a44547cf
|
|
| MD5 |
694ee681d72c0e662d8a31d2b6153578
|
|
| BLAKE2b-256 |
cb59052f6be711bd8005b548d87106966472efd4291d74108e1f7a4d4c353d82
|