Skip to main content

A Streamlit module for session state management

Project description

StreamlitMetaState

StreamlitMetaState is a Python package that enables seamless session persistence for class instances in Streamlit applications. By leveraging a metaclass and descriptors, it automatically binds class attributes to Streamlit's session state, eliminating the need for manual session management.

Features

  • Automatic Session Persistence: No need to manually store and retrieve instance data in Streamlit session state.
  • Seamless Integration: Works with any class by simply using StreamlitMetaState as a metaclass.
  • State Synchronization: Ensures attributes remain synchronized between session state and class instances.
  • Instance Persistence: Supports multiple instances with unique session keys.

Installation

Install StreamlitMetaState via pip:

pip install streamlit-meta-state

Usage

To ensure proper functionality, class variables must be annotated in the class definition.

Simple Use

import streamlit as st
from streamlit_meta_state import MetaSessionState

class MyPersistentClass(metaclass=MetaSessionState):
    name: str
    counter: int

    def __init__(self, name: str, counter: int):
        self.name = name
        self.counter = counter

# Create a persistent instance
my_instance = MyPersistentClass(name="Example", counter=10, instance_key="example_instance")

st.write(f"The counter for '{my_instance.name}' is currently {my_instance.counter}")
if st.button(label=f"Increase '{my_instance.name}'"):
    my_instance.counter += 1  # Changes persist across reruns
    st.rerun()

Widget Binding

You can bind Streamlit widgets directly to class attributes using their session keys:

st.text_input(label="MyPersistentClass.name", key=f"{my_instance.__instance_key__}.name")

st.write(f"The current value on the 'MyPersistentClass.name' is '{my_instance.name}'")

How It Works

  1. SessionVar Descriptor: Each class attribute is wrapped with a descriptor that synchronizes its value with Streamlit's session state.
  2. MetaSessionState Metaclass: Replaces annotated attributes with SessionVar and ensures instances are stored persistently using a unique instance_key.
  3. Automatic State Management: Attribute changes are automatically reflected in session state without extra handling.

License

This project is licensed under the MIT License.

Contributing

Contributions are welcome! If you'd like to improve StreamlitMetaState, feel free to submit a pull request.

Issues

If you encounter any issues, please open an issue on GitHub.

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_meta_state-0.1.3.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

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

streamlit_meta_state-0.1.3-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: streamlit_meta_state-0.1.3.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.5

File hashes

Hashes for streamlit_meta_state-0.1.3.tar.gz
Algorithm Hash digest
SHA256 a2f1c4cc4168f9428994fdc96b2446351f75272c3ba8b7c0c312202c25bfe6cd
MD5 b0e418333ba36033c1a3b5b33d080395
BLAKE2b-256 504bb58bcb90bab01572888b7bb3b9d0b758248a169a210576bcb27ae585fffa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for streamlit_meta_state-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6080252276be57529a9588b5625fd5b611bad9c7480ac7b3b424f25b62021de0
MD5 8e47654f5efd47a71aba6b919599aecd
BLAKE2b-256 ab9074f57032551b5033f4dbb7bd19c68e72657dce1bd0511bdebb0b9c82a129

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