Add your description here
Project description
Streamlit State Attribute
Contents
Idea
Instead of using st.session_state["some_key"], define a typed attribute of some class which is automatically synced
with the session state.
Advantages
- Handling
st.session_stateis abstracted away - Autosuggestions + type hints
- Logging each state change (level = info)
- Easily build Widgets with their own local state
Install
uv pip install streamlit-state-attribute
Examples
import streamlit as st
from streamlit_state_attribute import StateAttribute
class SomeWidget:
some_attribute: str = StateAttribute(default="test")
# Shared state between all instances
some_widget = SomeWidget()
some_widget.some_attribute = "3"
assert st.session_state["SomeWidget.some_attribute"] == "3"
class SomeWidgetWithKey:
key: str
some_attribute: str = StateAttribute(default="test")
def __init__(self, key: str) -> None:
self.key = key
# Each key will have a separate State
other_widget = SomeWidgetWithKey(key="test")
other_widget.some_attribute = "4"
assert st.session_state["SomeWidgetWithKey.test.some_attribute"] == 4
See also counter.py and global_state.py.
Background
Made to play around with descriptors after a workshop descriptors at Pycon2025.
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_attribute-0.1.4.tar.gz.
File metadata
- Download URL: streamlit_state_attribute-0.1.4.tar.gz
- Upload date:
- Size: 47.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1d2f55c493792533510c7c166fa91946c6eb76dac0314f3b78fed727e569139f
|
|
| MD5 |
ae73f01225b8498918f6ff9c1b37d89c
|
|
| BLAKE2b-256 |
07d3e8518123b39f81a795d9ad4dd42bcb4be784245a6ff2cf235265126c2235
|
File details
Details for the file streamlit_state_attribute-0.1.4-py3-none-any.whl.
File metadata
- Download URL: streamlit_state_attribute-0.1.4-py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2fbebd4e57da38d1baa01dc0c98492559da1937fdcd157f56ab73807d71824b5
|
|
| MD5 |
eff5b7519ddb5b3bd9659a03660d0c49
|
|
| BLAKE2b-256 |
e98c873904a8e08c90caf0f7cc2a639cc2a0e8f993e42c26e2c664bf648386e2
|