Delete/reset widgets in the main page on changing selections in the radio button on sidebar

One week back started using Streamlit. The docs are self-understandable and helped to quickly go prepare an app for my project. However, I ran into one critical issue as follows:

My app is consists of:

  • Two radio buttons on the sidebar
  • Based on these selections, a different number of checkboxes, sliders, and dropdown appears on the main page.

While changing the radio buttons selections, I receive the error:
IndexError: list index out of range

After debugging, I came to know this issue is because in st.session_state the earlier widgets are still present i.e. two checkboxes to be true, etc. My expectation here is that when a change of selection from radio button is noticed by app during rerun, all the widgets should be deleted or reset because the number of widgets and their values/names will change.

Hello @Sumit_Chaudhary , welcome to the community!

I’d have the same assumption, but though the widgets are put down / recreated, their state would be preserved in session_state, so multiple widgets with the same labels could conflict (see the advanced state docs). Have you perhaps tried to add distinct key= argument to your widgets so that they don’t conflict?

Would you be able to share a code snippet to help us get a better view of your issue :slight_smile: ? I may be missing something too.

Have a nice day Streamlitin’ !
Fanilo :balloon:

@andfanilo Thanks for the response. You have understood my issue perfectly, multiple widgets with the same labels conflicting as they are preserved in the session_state. Since the number of checkboxes, sliders… controls will vary base on selections from the radio buttons. In some cases, they may have the same names but different values as in the case of sliders. I am already using key for all these widgets (I have not assigned a variable name to widgets). How can I delete them from session_state as soon as the radio button input is changed?


I have solved the problem by using the radio buttons selection in the key name and deleting the previously selected widgets using

del st.session_state[key]

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.