A package that provides a StateModel for Streamlit applications, allowing easy management of session state through class attributes.
Project description
Streamlit State Model
I love Streamlit, but working with st.session_state can be tedious. Coming from an OOP background, I cringe at managing each key in st.session_state individually, checking if it exists, and losing intellisense for names and types.
So I made streamlit-state-model which offers a base class StateModel that you can subclass to push your class' attribute I/O directly into st.session_state. This means you define what should exist in st.session_state once in the form of a normal python class, and then access those values anywhere in your Streamlit app with the expected statefulness of st.session_state.
Features
- Persistence: Class instance storage is pushed to
st.session_stateso class attribute values are retained across page refreshes and switches (in multi-page apps). - Easy Setup:
StateModelcan be used with existing or new class definitions. - Default Values: Define the default state of
st.session_statewith normal class' default annotations and gain the ability to reset them all to defaults with a single function call. - Debugging: When debugging, you can now see the current values in
st.session_stateby inspecting your class' attribute values. - Intellisense Support: Retain normal docstrings and type intellisense that you normally lose when working with
st.session_state. - Inline Integration: Declare your class instance inline with the rest of your code and eliminate spaghetti
ifchecks around current state ofst.session_state. - Docstring Access: Retrieve attribute docstrings via the
docstringsdictionary within your app.
Getting Started
Install the package using pip:
pip install streamlit-state-model
Take your existing class and subclass StateModel:
import streamlit_state_model as ssm
class Session(ssm.StateModel):
favorite_color: str = "#252D3D"
"The user's favorite color as a hex string."
favorite_number: int = 0
"The user's favorite number."
At the entry point of your app, initialize your class in "build" mode.
# streamlit_app.py
session = Session(mode="build")
st.write(session.favorite_number) # outputs default value of 0
session.favorite_number = 1 # set to a new value
Anywhere else in your app you can initialize in lazy (default) mode to access current values:
# pages/page_1.py
session = Session() # init in lazy mode
st.write(session.favorite_number) # outputs 1 as set in streamlit_app.py
""
Example
Explore our demo Streamlit app in the /demo_app directory for an example of integrating StateModel into a multipage app on Streamlit Community Cloud. Check out: state-model-demo-app.
We're also working on generating API docs for StateModel since it already includes many helper functions such as:
dump(): Dump all class attribute values to a JSON string (often paired withst.json).- Widget interaction helpers: Utilize widgets with
StateModelfollowing Streamlit's recommended patterns. (See the Streamlit Documentation on Multipage Apps for details.) - Reset function: Reset all attribute values to their defaults.
Development
For details on contributing and the CI/CD process, please refer to the development documentation.
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
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_state_model-0.2.1.tar.gz.
File metadata
- Download URL: streamlit_state_model-0.2.1.tar.gz
- Upload date:
- Size: 11.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dd0ede3f5a4cce1776b091051c6d34d77ccc7c93ca72e16a87a40fdfe7ef8325
|
|
| MD5 |
a1d536b2fa1635defe314f452e04430e
|
|
| BLAKE2b-256 |
fb2471030d8f0ac89964b6c2974d2fa89199e2d2b84a7388a9d1ea37a6003249
|
Provenance
The following attestation bundles were made for streamlit_state_model-0.2.1.tar.gz:
Publisher:
cd.yml on trenton-ftw/streamlit-state-model
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
streamlit_state_model-0.2.1.tar.gz -
Subject digest:
dd0ede3f5a4cce1776b091051c6d34d77ccc7c93ca72e16a87a40fdfe7ef8325 - Sigstore transparency entry: 171703963
- Sigstore integration time:
-
Permalink:
trenton-ftw/streamlit-state-model@bfca11aa06fe296516173c60bace24ca229016bb -
Branch / Tag:
refs/heads/main - Owner: https://github.com/trenton-ftw
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
cd.yml@bfca11aa06fe296516173c60bace24ca229016bb -
Trigger Event:
workflow_run
-
Statement type:
File details
Details for the file streamlit_state_model-0.2.1-py3-none-any.whl.
File metadata
- Download URL: streamlit_state_model-0.2.1-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0b746175aa6ff9e64c2b7bcad218be07b00ea91df6cb41913150ab13af78dbeb
|
|
| MD5 |
bcbf8a6c71d5319eeed5d076036a409c
|
|
| BLAKE2b-256 |
3165e7ac223016d17c52b446527ac181460813b8ef221ccd703ec807a3f1b312
|
Provenance
The following attestation bundles were made for streamlit_state_model-0.2.1-py3-none-any.whl:
Publisher:
cd.yml on trenton-ftw/streamlit-state-model
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
streamlit_state_model-0.2.1-py3-none-any.whl -
Subject digest:
0b746175aa6ff9e64c2b7bcad218be07b00ea91df6cb41913150ab13af78dbeb - Sigstore transparency entry: 171703965
- Sigstore integration time:
-
Permalink:
trenton-ftw/streamlit-state-model@bfca11aa06fe296516173c60bace24ca229016bb -
Branch / Tag:
refs/heads/main - Owner: https://github.com/trenton-ftw
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
cd.yml@bfca11aa06fe296516173c60bace24ca229016bb -
Trigger Event:
workflow_run
-
Statement type: