How should st.rerun() behave?

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
    ....

Also, note the following fixes my issue

def main():
    logger.info("Page (re)load")

    my_table = st.empty()
    my_table.table(st.session_state['table'])

    with st.container():
        button = st.button("Press Me!")
        if button:
            logger.info("Pressed")
            st.session_state['table'] = update_table()

            # update table
            my_table.table(st.session_state['table'])

    # 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()
4 Likes

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