Skip to main content

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_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.1.3.tar.gz (47.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.1.3-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for streamlit_state_attribute-0.1.3.tar.gz
Algorithm Hash digest
SHA256 46e22887439cd6e6160debc732ba0cc9b665c430dc7d2a1515fc99a4da93b84b
MD5 e7beb8423c4eaa6db1c856431355f079
BLAKE2b-256 e10f57c8c040cf704b066527a52890b61bffb660b27f0ddee9ac757b470c1f6f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for streamlit_state_attribute-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c9f82466ea1db9542b93af12919a4cbdaab5709e3d8effd8ff455017f7dcd594
MD5 88f92a4d37d1dd7b875057522a74a7f1
BLAKE2b-256 2c0133b9e9317c3c40d3d764751cc8851e8964bf24766785aa5e19470f6dac43

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