Streamlit fire state
In a multipage streamlit app, one of the most headache issues is that your state is not preserved if you switch between pages.
That's why fire-state is here for you.
Installation
pip install fire-state
Quick Start
Persist state in Form
import streamlit as st
from fire_state import create_store, form_update
# Register state with initiate values in a slot
slot = "home_page"
key1, key2 = create_store(slot, [
("state1", 5),
("state2", 12),
])
# Now create a form using the generated keys
with st.form("my form"):
st.slider("State 1", 1, 10, step=1, key=key1)
st.slider("State 1", 10, 20, step=1, key=key2)
st.form_submit_button(label="Submit", on_click=form_update, args=(slot,))
When you switch between pages, the states are preserved.
Persist state in any place
You need to control the state by yourself, using the set_state function.
import streamlit as st
from fire_state import create_store, get_state, set_state
slot = "home_page"
create_store(slot, [
("state1", 0),
])
def increment():
prev = get_state(slot, "state1")
set_state(slot, ("state1", prev + 1))
st.button("+1", on_click=increment)
st.text(f"Value: {get_state(slot, 'state1')}")
Advanced Usage
Persist state after form submission
In this example, we have a form to control how the line chart is drawn.
Let's do some setup first
import numpy as np
import pandas as pd
import streamlit as st
np.random.seed(0)
@st.cache
def chart_data(line_count, data_size):
return pd.DataFrame(
np.random.randn(data_size, line_count),
columns=np.random.choice(list('abcdefghijlkmn'), line_count))
Now we can use fire state to record the user action.
The idea is that the first time user opens the page, they never click the run button, so the number of times they click a button is 0. When it no longer is 0, which means user clicked it. Therefore, the plot is rendered or updated (if chart data is changed).
from fire_state import create_store, get_state, set_state, form_update
PAGE_SLOT = "Home_Page"
key1, key2, key3 = create_store(PAGE_SLOT, [
("line_count", 2),
("data_size", 13),
("run", 0)
])
with st.form(key="a form"):
line_count = st.slider("Line Count", 1, 10, step=1, key=key1)
data_size = st.slider("Data Size", 10, 20, step=1, key=key2)
run = st.form_submit_button(label="Run", on_click=form_update, args=(PAGE_SLOT,))
prev_run_state = get_state(PAGE_SLOT, 'run')
if (prev_run_state != 0) or run:
data = chart_data(line_count, data_size)
st.line_chart(data)
# increase by 1 every time user click it
set_state(PAGE_SLOT, ("run", prev_run_state + 1))
Reset the state
Use the set_store function to update states in a batch:
import streamlit as st
from fire_state import create_store, \
get_store, set_store, \
get_state, set_state
slot = "page"
init_state = [
("state1", 1),
("state2", 2),
("state3", 3),
]
create_store(slot, init_state)
def reset():
set_store(slot, init_state)
st.button("Reset", on_click=reset)
The set_store and get_store functions allow you to
modify and get your state in a batch.
The Life Cycle of State
The state persists if you don't close or refresh the page. The state instance is only destroyed if you stop your app.
Release files for fire-state 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| fire_state-0.1.3.tar.gz | 5.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fire_state-0.1.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.8 kB
Release files / fire_state-0.1.3.tar.gz
| Download URL | fire_state-0.1.3.tar.gz |
|---|---|
| Size | 5.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
793d649af0cf1d647cccfca9bd78199d31b177d956fdb6256d40cbed745d6663
|
|
BLAKE2b-256 checksum How to use checksums |
967da926cf9f08c52fd659b103a71bc15a3f9b83d634a9d6c521ae2e7a999399
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.32.3
|
Release files / fire_state-0.1.3-py3-none-any.whl
| Download URL | fire_state-0.1.3-py3-none-any.whl |
|---|---|
| Size | 5.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7aa5bda7ed83e1f951d18ad122514aff250887f85091ad845e78ca22c1ffe7be
|
|
BLAKE2b-256 checksum How to use checksums |
34f836c294f2c01923bd42d910d847f5ecb2af17c3ffe82146c16529687764ac
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.32.3
|