I’ve created a streamlit app and am experiencing infinite reload loops, and buttons that act as if they’ve been pressed when they’re not.
For example the code below is an extract of my app. I added st.rerun() to ensure that the table gets updated after the button is pressed. But what I’ve observed is that when the script reloads it behaves as if the button is still pressed - i.e. the logs shows
Page (re)load
Pressed
Page (re)load
Pressed
So the second time the script runs (after the button is pushed) button still evaluated as true. Is this expected?
I think I should probably put the table in a container and instead of doing a reload I should recreated it using table_container.write() but I’d still like to understand whats happening here as it’s not how I thought streamlit should work.
def main():
logger.info("Page (re)load")
st.table(st.session_state['table'])
with st.container():
button = st.button("Press Me!")
if button:
logger.info("Pressed")
st.session_state['table'] = update_table()
# ensure table redraws before running rest of script
st.rerun()
# rest of script
....
This is one of the surprising behaviours of st.rerun() instantly re-running the script.
st.button returns whether the button is pressed on the previous script run. When it is pressed, the script is re-run, the function returns True and, in at the end of the run, the state of the button is set to False. When another widget is altered, the script is re-run and the function returns False (because the button wasn’t pressed last run).
When st.rerun() is triggered by a button, the script never gets to finish and the button’s state never gets reset to False, so st.rerun() is executed over and over again.
If you must use st.rerun(), a workaround is to give the button a key then delete its entry from st.session_state. Even though you can’t set the st.session_state value of a button, deleting it seems to work. Try:
with st.container():
button = st.button("Press Me!",key = 'button')
if button:
logger.info("Pressed")
st.session_state['table'] = update_table()
del st.session_state['button']
# ensure table redraws before running rest of script
st.rerun()
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.