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.
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 ? I may be missing something too.
@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?
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.