If you’re creating a debugging post, please include the following info:
Are you running your app locally or is it deployed? Locally
Share the Streamlit and Python versions. Streamlit 1.39.0 Python 3.9.18
I have a simple repro case for this.
Steps to reproduce:
Run the simple app below
Select a few columns in the multi-select
Now click on “Rerun” button
Now, you will see the multiselect is showing selections in the UI but thats just stale UI. Actually, the widget internally thinks it has nothing selected.
Say Twing if you agree this is a bug
import streamlit as st
import pandas as pd
if st.button('Rerun now'):
st.rerun()
pdf = pd.DataFrame({
'BusinessUnit': ['A', 'B', 'C', 'D'],
'Category': ['SGA', 'Interest', 'Taxes', 'COGS'],
'Grade': ['P', 'P', 'VIP', 'N']
})
cols = st.multiselect('Select columns', list(pdf.columns), key='orchard')
if len(cols) == 0:
st.write('No col selected')
for col in cols:
st.write('Selected col = {}'.format(col))
It does look like a bug to me. Here is a simpler version that doesn’t need pandas.
import streamlit as st
if st.button('Rerun twice'):
st.rerun()
selected = st.multiselect('Select options', options=["one", "two", "three"])
st.write(selected)
I can reproduce it with other input widgets too. They keep displaying whatever value they had when the button was pressed, but they return the default value instead.
Since “rerun” is called before rendering the multiselect widget, streamlit simply marks it as “not rendered during last run”. So in the new run, it thinks the widget is stateless and hence associates its default value.
This can be easily verified by adding this line before the rerun. Though it looks crazy, when we assign the value, streamlit thinks “user” has touched this key, and so it maintains it in the “rerun” run.
As counter-intuitive as that might look, that’s what always happen is not rendered in a rerun, so it makes complete sense to me. The actual issue is that the rendering of the widget doesn’t match that.
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.