Way to find out what triggered a rerun

Hello everyone,

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.

Thank you very much for your feedback!

Best regards
Fabian

3 Likes

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.

Thank you for the clarification on status.update.

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 :slightly_smiling_face:

1 Like

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!")
1 Like