Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yaphott/streamlit-persistence
Allows for persistence of class attributes when using Streamlit in Python.
https://github.com/yaphott/streamlit-persistence
attributes data-science developer-tools persistence python streamlit
Last synced: 4 months ago
JSON representation
Allows for persistence of class attributes when using Streamlit in Python.
- Host: GitHub
- URL: https://github.com/yaphott/streamlit-persistence
- Owner: yaphott
- License: mit
- Created: 2022-04-03T03:53:15.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2022-12-05T02:54:14.000Z (about 2 years ago)
- Last Synced: 2024-10-01T15:44:00.226Z (4 months ago)
- Topics: attributes, data-science, developer-tools, persistence, python, streamlit
- Language: Python
- Homepage: https://pypi.org/project/streamlit-persistence
- Size: 7.81 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# streamlit-persistence
Allows for persistence of class/instance attributes when using Streamlit in Python
## Getting started
### Install using pip
```bash
pip install streamlit-persistence
```### or build from source
```bash
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)```python
import streamlit as st
from streamlit_persistence import PersistentObjectoptions = [None, "veggie", "pepperoni"]
class Test(PersistentObject):
session_state = st.session_statedef 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()
```