I have a fairly complex Streamlit App with a lot of code. I have noticed that the pages loads twice on the first visit: Meaning once a users visits the app the first time the interface loads then clears itself and loads again (it looks like st.rerun gets called). After that everything else works fine.
I already tried to few things to find out what triggers the rerun (probably some widget changes it state which then triggers a rerun…) but had no look so far. That is why I was wondering if there is some way to find out what exactly triggered a app rerun in Streamlit.
It would be helpful if Streamlit published a list of actions that trigger a re-run of the script. I have noticed updates to the streamlit package result in behaviors triggering a re-run, which once did not. For example, st.status.update() Having a definitive list would save us lots of time as evidenced by this thread.
Interacting with an input widget or calling st.rerun() and changing the code (depending on sesttings) cause the script to rerun. That is all, and I think it is covered in the docs.
Calling st.status.update()should not (and AFAICT does not) trigger a rerun. If it does for you it’is a bug.
My larger request is for some way to know which actions trigger reruns. It’s not always obvious, at least to me.
Clicking into st.text_input will trigger a re-run., but only if there’s already a value in it. Otherwise it doesn’t. Same for item select. If you click into it and select the same item that is aleeady selected, it re-runs the script.
Date and time inputs? Not true. They only update when you change the value. Even though they may be pre-filled, clicking into them does not trigger a re-run.
The process of clicking the browse button and closing the resulting browse window does not trigger a re-run.
Tabbing into and out of input components doesn’t seem to ever trigger a re-run as long as the value isn’t changed in the process. Clicking into the same inputs usually, though not always, triggers a re-run.
Previously, clicking into and out of a widget without changing the value did not trigger a rerun. I just tested it with 1.40.0 and see that it now does trigger a rerun, just by focusing and unfocusing the widget. I’m not sure if this was an intentional change, so I’ll ask engineering.
As for telling what triggered a rerun, there’s not some simple built-in way, other then running the code in debug mode to step through it. One thing you could do is add callback functions to all your widgets, that print some debug text to your console or app. Callbacks on widgets are run right before the page rerun. Similarly, you could add the same debug text before any call of st.rerun().
@dvvilkins@mathcatsand Thanks for raising this! We have just released a patch version 1.40.1 that should also contain a fix for this text_input causes a rerun issue you described (PR here). Please give it a spin and let us know
That addressed it. They all behave similarly now. Thanks.
Here’s the page I used to test the various components. It recalculates the random number each time the script reruns. I just realized it has a 0.1% false negative rate lol.
import streamlit as st
import random
random_number = random.randint(1, 1000)
st.write("Random Number:", random_number)
# Status
with st.status("This is an expandable status", expanded=False) as status:
st.info("status is expanded")
# Text input
user_text = st.text_input("Enter some text:")
# Number input
user_number = st.number_input("Pick a number", min_value=0, max_value=100, value=50)
# Date input
user_date = st.date_input("Select a date")
# Time input
user_time = st.time_input("Select a time")
# Slider input
user_slider = st.slider("Slide to choose a value", min_value=0, max_value=100, value=25)
# Checkbox input
user_checkbox = st.checkbox("Check this box if you agree")
# Radio button selection
user_radio = st.radio("Choose an option", ("Option 1", "Option 2", "Option 3"))
# Select box
user_selectbox = st.selectbox("Select an item", ["Item A", "Item B", "Item C"])
# Multi-select
user_multiselect = st.multiselect("Select multiple items", ["Item 1", "Item 2", "Item 3", "Item 4"])
# File uploader
user_file = st.file_uploader("Upload a file")
if user_file:
st.write("Uploaded File Name:", user_file.name)
# Button
if st.button("Click Me"):
st.write("Button was clicked!")
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.