Skip to main content

Allows for persistence of class/instance attributes when using Streamlit in Python

Project description

streamlit-persistence

Allows for persistence of class/instance attributes when using Streamlit in Python

Getting started

Installing

git clone https://github.com/yaphott/streamlit-persistence.git
cd streamlit-persistence
python3 -m pip install --upgrade pip setuptools wheel
python3 -m pip install .

Using the module

  1. Use PersistentObject as the parent class
  2. Assign the Streamlit session_state to a class attribute named session_state
  3. Assign class/instance attributes as you would normally
    • Still need to implement a proper way to handle the __init__ method
    • Until then you can simply check an instantiated class for the instance attribute using hasattr before it is referenced
import streamlit as st
from streamlit_persistence import PersistentObject


pizza_choices = [None, "veggie", "pepperoni"]


class Test(PersistentObject):
    session_state = st.session_state

    def run(self):
        if not hasattr(self, "pizza"):
            self.pizza = None

        # Default to the index of previously selected
        pizza = st.selectbox(
            "Select a pizza",
            pizza_choices,
            index=pizza_choices.index(self.pizza),
        )

        # Update instance attribute if user changes their selection
        if pizza != self.pizza:
            self.pizza = pizza
            st.experimental_rerun()

        st.success(f"You have selected the pizza: {self.pizza}")


def main():
    Test().run()


if __name__ == "__main__":
    main()

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-persistence-0.0.2.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

streamlit_persistence-0.0.2-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file streamlit-persistence-0.0.2.tar.gz.

File metadata

File hashes

Hashes for streamlit-persistence-0.0.2.tar.gz
Algorithm Hash digest
SHA256 ee14b767671a9d609667a9c65027dca263c189df06f569c81a863cee43a37940
MD5 a171dc4b9c0e4e69c5d4026938be3c8d
BLAKE2b-256 5724863d09169c8ef23c004a4e8690f0c3036ddf019310c5ddb12f8bff588faf

See more details on using hashes here.

File details

Details for the file streamlit_persistence-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for streamlit_persistence-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 816e630548867b75135d1a8a22d42c8f27f034aec3bd3d742c64c05593750141
MD5 78bdd0c6cb1847800c7e576f0829c5a6
BLAKE2b-256 aa30569c16e83204eecd324cbb7a64a09e23d8f6c5d1e28e840c0b74f97f9d71

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page