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.2.tar.gz (47.0 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.2-py3-none-any.whl (2.6 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for streamlit_state_attribute-0.1.2.tar.gz
Algorithm Hash digest
SHA256 53dc83e5205df9d7fa33ebb5d47c480c64083338c87d88f6c0b7278bb68aa5b9
MD5 44e973b734b1023bb73ed146e5f88e54
BLAKE2b-256 c3e0741418e17e694b8be1025b530fea8d20912d0b1855c9e1e7b23cd2b7d3bd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for streamlit_state_attribute-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2973b9bb8754f680fe427a57ec35d521b4e93ebaacb62ac6ebfd8876b7460905
MD5 1f3e3c3b6963c4ae46f7435a7d11acbf
BLAKE2b-256 db0c30bb112343cd31f51cc0f6a60e1d2e94058355660e926b30f9a4c8ffcc82

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