I’m trying to reset my selection by deleting each element in streamlit.session.state.
But not able to do so.
Reproducible Code -
import streamlit as st
from streamlit import caching
st.set_page_config(layout="wide")
columns = st.columns(4)
columns[0].header("Session State Start")
columns[0].write(st.session_state)
n_check_box = 3
columns[1].header("Sample CheckBox")
for i in range(n_check_box):
label = "check_box_" + str(i + 1)
columns[1].checkbox(label=label, value=False, key=label)
n_radio_button = 2
columns[2].header("Sample Radio Button")
for i in range(n_radio_button):
key = "radio_button_" + str(i + 1)
label = "Radio Selection " + str(i + 1)
values = ["Radio " + str(n) for n in range(n_radio_button)]
columns[2].radio(label, values, key=key)
columns[3].header("Session State End")
columns[3].write(st.session_state)
if st.button("Clear Selection", key="clear_selection_button"):
st.subheader("Executing code")
code_clear_session_state = """
for key in st.session_state.keys():
del st.session_state[key]
caching.clear_cache()"""
st.code(code_clear_session_state, language="python")
for key in st.session_state.keys():
del st.session_state[key]
caching.clear_cache()
I think I figured out a solution to this. The widget cannot really be reset, but you can use key for the widget to display a new one, thus resetting it. You can set a variable in session_state, and every time you click “Clear Section”, you can increase that variable by 1. For each widget you want to reset, you can put that variable’s value in the key field, using f-string, or something similar.
Furthermore, you want to do a st.experimental_rerun() to force the page to be rendered again. This approach seems to be working fine for me.
Have already tried a similar way by creating a unique key for each input widget using session_state on ‘Clear Selection’ i.e. increase variable key by 1 & using that as a key in interactive widgets.
That works !!!
Was looking into some concrete resolution avoiding complexity.
Inspired by This answer I’ve written the following code that work for the checkbox as well:
import streamlit as st
# create a function that sets the value in state back to an empty list
def clear_all():
for i in selections:
st.session_state[f'checkbox{i}'] = False
return
st.title("Clear multiselect with stateful button")
selections = ["a","b","c","d"]
for i in selections:
st.checkbox(i, value=False, key=f'checkbox{i}')
# check state
st.session_state
#create your button to clear the state of the checkboxes
st.button("Clear all", on_click=clear_all)
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.