Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't reset selected items (check box, radio buttons) by clearing session.state #3841

Closed
sachinpatel248 opened this issue Sep 24, 2021 · 7 comments
Assignees
Labels

Comments

@sachinpatel248
Copy link

Summary

Can not clear existing selected items from check box, radio buttons.
Tried to reset my selection by deleting each element in streamlit.session.state

Is there any workaround to achieve this?

Steps to reproduce

Code snippet:

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

Steps :

  1. Select any option/(s) from radio button & checkbox.
  2. Click on Clear Selection button

Expected behavior:

On clicking Clear Selection all selected items should get clear/reset.

Actual behavior:

Selected data persists

streamlit-sessio-state-issue

Is this a regression?

Not sure

Debug info

  • Streamlit version: 0.86.0
  • Python version: 3.8.8
  • Using: PipEnv
  • OS version: Windows 10
  • Browser version: Chrome 93

Additional information

Had asked this in streamlit forum https://discuss.streamlit.io/t/clearing-all-selected-items-check-box-radio-buttons-selectbox/16605

@sachinpatel248 sachinpatel248 added type:bug Something isn't working status:needs-triage Has not been triaged by the Streamlit team labels Sep 24, 2021
@sachinpatel248
Copy link
Author

Hi Team,
It's almost 2 weeks, any updates here?

@sachinpatel248
Copy link
Author

@Amey-D , @kantuni , @vdonato , @mesmith027
Any views?

@vdonato vdonato self-assigned this Oct 14, 2021
@vdonato vdonato removed the status:needs-triage Has not been triaged by the Streamlit team label Oct 14, 2021
@kmcgrady
Copy link
Collaborator

Hey @sachinpatel248 Thanks for reaching out. The team has been busy the past few weeks and is finally catching up a little bit. We are taking a look at this issue and hopefully will have a fix out soon. Stay tuned! 😊

@Dowdtw1
Copy link

Dowdtw1 commented May 23, 2022

Any updates/fixes/solutions put in place to enable this functionality please?

@sachinpatel248
Copy link
Author

sachinpatel248 commented May 23, 2022 via email

@sfc-gh-kbregula
Copy link
Contributor

sfc-gh-kbregula commented Sep 16, 2022

Here is workaround:
#4457

ctx = get_script_run_ctx()
state_key = ctx.session_state._state._get_widget_id('checkbox')
ctx.session_state[session_key] = val

@sfc-gh-jcarroll
Copy link
Contributor

I know this is a zombie issue but I was looking at it recently from the link Kamil posted above.

I wanted to report that this is intended behavior - deleting the key associated to a widget in session_state is not intended to clear or reset the widget.

Here's similar working app code that meets the intent of OP's snippet above - changing the button execution to a callback and setting the key to the expected value instead of deleting it.

import streamlit as st

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)

def clear_selection():
    for key in st.session_state.keys():
        if key.startswith("radio"):
            st.session_state[key] = "Radio 0"
        elif key.startswith("check"):
            st.session_state[key] = False


st.button("Clear Selection", on_click=clear_selection)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants