-
-
Save okld/0aba4869ba6fdc8d49132e6974e2e662 to your computer and use it in GitHub Desktop.
import streamlit as st | |
from persist import persist, load_widget_state | |
def main(): | |
if "page" not in st.session_state: | |
# Initialize session state. | |
st.session_state.update({ | |
# Default page. | |
"page": "home", | |
# Radio, selectbox and multiselect options. | |
"options": ["Hello", "Everyone", "Happy", "Streamlit-ing"], | |
# Default widget values. | |
"text": "", | |
"slider": 0, | |
"checkbox": False, | |
"radio": "Hello", | |
"selectbox": "Hello", | |
"multiselect": ["Hello", "Everyone"], | |
}) | |
page = st.sidebar.radio("Select your page", tuple(PAGES.keys()), format_func=str.capitalize) | |
PAGES[page]() | |
def page_home(): | |
st.write( | |
f""" | |
Settings values | |
--------------- | |
- **Input**: {st.session_state.text} | |
- **Slider**: `{st.session_state.slider}` | |
- **Checkbox**: `{st.session_state.checkbox}` | |
- **Radio**: {st.session_state.radio} | |
- **Selectbox**: {st.session_state.selectbox} | |
- **Multiselect**: {", ".join(st.session_state.multiselect)} | |
""" | |
) | |
def page_settings(): | |
st.header("Change settings") | |
st.text_input("Input", key=persist("text")) | |
st.slider("Slider", 0, 10, key=persist("slider")) | |
st.checkbox("Checkbox", key=persist("checkbox")) | |
st.radio("Radio", st.session_state["options"], key=persist("radio")) | |
st.selectbox("Selectbox", st.session_state["options"], key=persist("selectbox")) | |
st.multiselect("Multiselect", st.session_state["options"], key=persist("multiselect")) | |
PAGES = { | |
"home": page_home, | |
"settings": page_settings, | |
} | |
if __name__ == "__main__": | |
load_widget_state() | |
main() |
from streamlit import session_state as _state | |
_PERSIST_STATE_KEY = f"{__name__}_PERSIST" | |
def persist(key: str) -> str: | |
"""Mark widget state as persistent.""" | |
if _PERSIST_STATE_KEY not in _state: | |
_state[_PERSIST_STATE_KEY] = set() | |
_state[_PERSIST_STATE_KEY].add(key) | |
return key | |
def load_widget_state(): | |
"""Load persistent widget state.""" | |
if _PERSIST_STATE_KEY in _state: | |
_state.update({ | |
key: value | |
for key, value in _state.items() | |
if key in _state[_PERSIST_STATE_KEY] | |
}) |
Apparently if a widget bound to session state (with key param) is displayed in run #1, but not in run #2, the associated session state variable is cleared in run #3. Self-applying session state values seems to do the trick.
Session state behavior changed in Streamlit 0.89.0. Related issue: streamlit/streamlit#4338
Many thanks for this snippet, it helped me work around #4338.
When I try the code however, on first page load, the widgets are initialized to default values rather than the initial values in the session state.
Does the same happen fo you? Is there any workaround?
Hi All,
I am new to streamlit,trying to build multipage webapp.
In first page i will upload a file and click and button and it redirects to second page.
In second page i have side bar and radio buttons. Based on button selection the output need to be changed.
If i change radio button multiple times i am facing EOF ran of input error could you please help me
Thanks for this! Works great for me.
I'm trying to edit the code so that I can persist a session state of an editable dataframe while navigating between different pages of the app. I'm using AgGrid to make the df editable, but when refreshing or going to another page, the df defaults back to original values. Do you have any suggestions on how to implement this from your code above? I've been stuck on this for a week now
@okld Really nice work. However, I see that for refreshing is not working anymore. Do you have any idea of how to handle it?
Really neat concept! Does this only work if you have a main script registering and driving the different pages?
I tried this persist
/load_widget_state()
approach on a multi-page app using the current directory structure of implementing multipage apps, and it didn't work for me.
Hey man, many thanks for this snippet!
Thanks @okld ! Now it works!