Can you please help me clarify something very basic?
I recently came across the experimental_rerun, which seems to me it is equivalent to a widget interaction that causes the script to run from top to bottom.
If we attempt to create a widget within a for-loop we need to assign a unique key, otherwise we get a ‘DuplicateWidgetID’ error message. So far so good. Why don’t we get the duplication error with experimental_rerun? The widgets already exist with the same id’s…
is it the case that every time the code reruns the widgets are built from scratch? the session_state then is used to update the state of the widgets so we get the impression the widgets were always there?
I have two simple examples/pseudocode below that might help clarifying.
Thank you in advance,
M
#Pseudocode 1 (As expected this will cause an error due to key duplication)
import streamlit as st
for i in range(3):
st.widget_blue(key=f'widget_blue')
however,
#Pseudocode 2 (All good here)
import streamlit as st
import time
st.widget_blue(key=f'widget_blue')
time.sleep(1)
st.experimental_rerun()
I modified the example code to an actual widget so that the code would run.
import streamlit as st
for i in range(3):
# Code 1
#st.slider(label='My Slider', min_value=1, max_value=10, value=5, key=f'my_slider')
# Solution to Code 1: Gives 3 widgets
st.slider(label='My Slider', min_value=1, max_value=10, value=5, key=f'my_slider_{i}')
In the Code 1 example, the f-string was defined in the key but did not explicitly define the loop variable i. Thus, you’ll see that the first loop runs successfully as the key my_slider was used for the first time while subsequent runs failed as the key value is a duplicate.
In the Solution code block below, the loop variable was defined in the key parameter f'my_slider_{i}'. Including the loop variable in the definition of the key value creates a unique key value for each loop and thus you’ll see 3 instances of the slider widget.
Perhaps I was not clear. I understand code 1) and the result is as expected since the keys are not unique. As you indicated (and in other posts as well) all we need to do is assign a new key. I used example 1 as an example to compare with st.experimental_rerun:
Does st.experimental_rerun create the widgets from scratch every time the code is rerun from top to bottom ? Is this also the case when we interact with a widget? If this is not the case, then it is not clear to me why with the rerun (or with widget interact) we do not need to change the key as in the for loop.
Widgets are retained in memory until a script rerun occurs where they are not rendered. Both st.experimental_rerun and the rerun that occurs from widget interaction are the same thing: a rerun of the script. The difference with widget interaction is that the interaction updates the widget state, executes a callback if assigned, then reruns the script. st.experimental_rerun() is a standalone prompt to rerun the script.
A DuplicateWidgetID error occurs if two widgets with the same key (or same contruction parameters without a key) coexist within the same script run. You don’t need to keep changing the key of a widget with each rerun, in fact that would cause the widget to lose its statefulness. Having a widget on the page with a given key (that it keeps, rerun after rerun) is how Streamlit knows to associate that widget information it has in memory with a particular execution of a widget function.
When Streamlit executes a widget function, it checks to see if it already has a matching widget in memory
a. If yes, it connects to that widget (assuming it hasn’t already been executed/connected previously in the same script run).
b. If no, it constructs a new widget.
When Streamlit gets to the end of a script, it checks if it’s holding on to any extra widgets in memory. If it has any left over that weren’t executed/connected, they are forgotten/deleted.
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.