Clearing all selected items (check box, radio buttons & selectbox)

Hello Team,

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()

streamlit-sessio-state-issue

1 Like

Github issue

1 Like

Look forward to the fix to this one. I need to reset the widget selection/values after clicking a button like submit.

1 Like

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.

3 Likes

Hey huani, thanks for looking into it :grinning:

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.

Efforts appreciated. :wave: :wave: :wave:

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)

streamlit.errors.StreamlitAPIException: st.session_state.checkbox cannot be modified after the widget with key checkbox is instantiated.

Not reliable solution.

I dont get why streamlit just CANT let you reset a button state that YOU KNOW its key.