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
- Use
PersistentObjectas the parent class - Assign the Streamlit
session_stateto a class attribute namedsession_state - Assign class/instance attributes as you would normally
- Check the instantiated class for the instance attribute using
hasattr(as you would checksession_statefor a given key)
- Check the instantiated class for the instance attribute using
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
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e5ac8fe6567d2599b6a0e80c426a8c47899eec11d5f3ebfad30805de00602f84
|
|
| MD5 |
64f9878161106d34fadb06e870806e1c
|
|
| BLAKE2b-256 |
0d49bf234aca6373390ba42247b1fdbb9940c0467560eabbce76f6a3eb74c193
|
File details
Details for the file streamlit_persistence-0.0.3-py3-none-any.whl.
File metadata
- Download URL: streamlit_persistence-0.0.3-py3-none-any.whl
- Upload date:
- Size: 3.7 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 |
f066efc894ab128f6c1f598e78feafdd270d0c0538b21a13ba0012589b020ca2
|
|
| MD5 |
4a9f539b5400e2fd8d092ec52ab3c597
|
|
| BLAKE2b-256 |
e33443a9a080ec2d67ac0561b16da32d77890fce5040c80abd92d6bc228cdd36
|