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
- Use
PersistentObject
as the parent class - Assign the Streamlit
session_state
to a class attribute namedsession_state
- 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
- Still need to implement a proper way to handle the
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file streamlit-persistence-0.0.2.tar.gz
.
File metadata
- Download URL: streamlit-persistence-0.0.2.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee14b767671a9d609667a9c65027dca263c189df06f569c81a863cee43a37940 |
|
MD5 | a171dc4b9c0e4e69c5d4026938be3c8d |
|
BLAKE2b-256 | 5724863d09169c8ef23c004a4e8690f0c3036ddf019310c5ddb12f8bff588faf |
File details
Details for the file streamlit_persistence-0.0.2-py3-none-any.whl
.
File metadata
- Download URL: streamlit_persistence-0.0.2-py3-none-any.whl
- Upload date:
- Size: 3.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 816e630548867b75135d1a8a22d42c8f27f034aec3bd3d742c64c05593750141 |
|
MD5 | 78bdd0c6cb1847800c7e576f0829c5a6 |
|
BLAKE2b-256 | aa30569c16e83204eecd324cbb7a64a09e23d8f6c5d1e28e840c0b74f97f9d71 |