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


columns = st.columns(4)

columns[0].header("Session State Start")

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

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]
    st.code(code_clear_session_state, language="python")

    for key in st.session_state.keys():
        del st.session_state[key]


Github issue

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

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.

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: