Skip to main content

A lightweight utility to bind class attributes directly to Streamlit's session state, making it easy to persist and share values across reruns.

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.

from streamlit_state_attribute import StateAttribute
import streamlit as st

class SomeWidget:
    some_attribute: str = StateAttribute(default="test")

some_widget = SomeWidget()
some_widget.some_attribute = "3"
assert st.session_state["SomeWidget.some_attribute"] == "3"

Advantages

  • Handling st.session_state is abstracted away
  • Autosuggestions + type hints
  • Logging each state change (default logging level = debug, can be configured per-attribute)
  • 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 SomeWidgetWithKey:
    key: str
    some_attribute: str = StateAttribute(default="test", unique_attribute="key")

    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

streamlit_state_attribute-0.2.3.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

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

streamlit_state_attribute-0.2.3-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file streamlit_state_attribute-0.2.3.tar.gz.

File metadata

File hashes

Hashes for streamlit_state_attribute-0.2.3.tar.gz
Algorithm Hash digest
SHA256 b4dd99eea375681bf55f8c284b2fbd0117b20a504412e4063a8843d4bb29adf3
MD5 34a977920204df26abb043433800261d
BLAKE2b-256 f4ddb8cdf95cc809fb8cddf1f02460f385b15d9257dd88811f0bc6c3d1710c41

See more details on using hashes here.

File details

Details for the file streamlit_state_attribute-0.2.3-py3-none-any.whl.

File metadata

File hashes

Hashes for streamlit_state_attribute-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 72cc5e24bc8b79b6adc50affcd5c68d971f4938e70dfc46ee665294e665098ce
MD5 b4548f4fee41809ca1dbc65873b81930
BLAKE2b-256 00a753f5c020447be214e89c5317a4b5cc61223b3295fe9baed9fd653c77f60c

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