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

Install using pip

pip install streamlit-persistence

or build from source

git clone https://github.com/yaphott/streamlit-persistence.git
cd streamlit-persistence
python3 -m pip install -U 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
    • Check the instantiated class for the instance attribute using hasattr (as you would check session_state for a given key)
import streamlit as st
from streamlit_persistence import PersistentObject


options = [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
        selected = st.selectbox(
            "Select a pizza",
            options,
            index=options.index(self.pizza),
        )

        # Update instance attribute if user changes their selection
        if self.pizza != selected:
            self.pizza = selected
            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.3.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

streamlit_persistence-0.0.3-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: streamlit-persistence-0.0.3.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.5

File hashes

Hashes for streamlit-persistence-0.0.3.tar.gz
Algorithm Hash digest
SHA256 e5ac8fe6567d2599b6a0e80c426a8c47899eec11d5f3ebfad30805de00602f84
MD5 64f9878161106d34fadb06e870806e1c
BLAKE2b-256 0d49bf234aca6373390ba42247b1fdbb9940c0467560eabbce76f6a3eb74c193

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for streamlit_persistence-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f066efc894ab128f6c1f598e78feafdd270d0c0538b21a13ba0012589b020ca2
MD5 4a9f539b5400e2fd8d092ec52ab3c597
BLAKE2b-256 e33443a9a080ec2d67ac0561b16da32d77890fce5040c80abd92d6bc228cdd36

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