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.

Advantages

  • Handling st.session_state is 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

streamlit_state_attribute-0.2.1.tar.gz (4.9 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.1-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for streamlit_state_attribute-0.2.1.tar.gz
Algorithm Hash digest
SHA256 ee3b15e0c2c6c2609e2ea55534492005565d4259ccbc8df13b57c5497d9d58a2
MD5 9d23277de13cbe48fc9918ffc9506948
BLAKE2b-256 5b866b4c227d2eb14a9ced124fbe6f71593fca5c71af578367bf7b9cb1f83a11

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for streamlit_state_attribute-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 847f08430c00060e7b7fce3ee1029d17ff0bb472e182aa6ec7d104f99c5e1a89
MD5 e4f582fddb7ac0604e003fd097cd7bd7
BLAKE2b-256 57b70bba786190f168d6f81d968ec7d6bf30657a2ddd27c44603435de18e7e7d

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