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 apps, but I might lose it if I have to write if key not in st.session_state one more time. So I made this package which provides a StateModel for you to subclass in your python class definition. This forwards your class instance's attribute storage/retrieval to session_state with no extra work. This allows you to interact with session_state through normal python class definitions.
Feautures
- Easy to Setup. Use your existing class definitions or define whatever you need to store in
session_stateas a class definition. - Can sit inline with the rest of your app code. Get rid of the spaghetti
ifstatements in your app code to interact withsession_state. - Default values defined in your class are defaulted into
session_state. Eliminate extra code in your app to initalize each individual value insession_state. Defining it in theStateModelautomatically defaults the value intosession_state. - Makes your class instance consistently available across all pages in multi-page app. With
StateModel, your class' attribute storage is now backed directly bysession_state, so all attribute values of your class will persist across page refreshes or page switches that you might find in a multi-page app. - Keep intellisense working while working with
session_state. Your defined docstrings and type intellisense will continue to work as normal with aStateModeleven though you are storing it insession_state(which you typically lose when working withsession_state). - Use attribute docstrings within your app. Your
StateModel's attribute docstrings are parsed and made available to you in thedocstringsdict keyed by its attribute name.
Getting Started
Install the package using pip:
pip install streamlit-state-model
Just take your existing class definition 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 to your app, init an instance of your class definition 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 init an instance of your StateModel using lazy (default) mode. StateModel is a singleton like storage of your class instance, so when you init an instance of your class after building it, it does nothing except provide the class interface to your python code for accessing your class' attribute values.
#pages/page_1.py
session = Session() #init in lazy modey
st.write(session.favorite_number) #outputs 1 as set in streamlit_app.py
""
Example
Check out our demo Streamlit app within this repo at /demo_app for an example (coming to Streamlit Community soon) of integrating StateModel into a simple multi-page app.
Also working on generating API docs for StateModel as it also provides many other commonly needed helper functions for your class. Provides a dump() function to make it easy to view all attribute values, functions to help utilize widets to interact with your StateModel (following the 'Option 2: Save your widget values into a dummy key in Session State' pattern defined by Streamlit Streamlit Documentation | Working with widgets in multipage apps), a function to reset all attribute values to their defaults, and many others!
Contributing
If you have an idea for a new feature or have found a bug, please open an issue or submit a pull request. While we don't have a formal code of conduct or contribution guidelines yet, we appreciate respectful and constructive contributions.
Development
The project is built with Flit and a Conda development environment is provided in the project root.
Tests are built using pytest and Streamlit's AppTest framework against the demo app.
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.1.0.tar.gz.
File metadata
- Download URL: streamlit_state_model-0.1.0.tar.gz
- Upload date:
- Size: 11.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
61612a9d3443b792bb155d4073c86bd3e289389e3b253f0e0a095c7958b6a37f
|
|
| MD5 |
a6e9b4de169b69319ca717c5b9e2a8ed
|
|
| BLAKE2b-256 |
759a00de9246ee524c69f609c8672c78681b90f87ac3de3b64c6a2a4a3053eee
|
File details
Details for the file streamlit_state_model-0.1.0-py3-none-any.whl.
File metadata
- Download URL: streamlit_state_model-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f2042032850cdb5f21383e04a31390836c7ecfbad5a4aa5578424afde501a6c8
|
|
| MD5 |
31aef16146399ad4e86d061ae53ce1bc
|
|
| BLAKE2b-256 |
9244beb898c77ef47cd5b33c7d602a080258cefda880c405ef6079d18564e583
|