Basics of streamlit - how does it work (experiment_rerun vs for loop)

Hi All,

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()

Hi @michalis

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.

Best regards,
Chanin

Thank @dataprofessor,

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.

Thanks again…
M

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.

  1. 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.
  2. 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.
1 Like

@mathcatsand thank you so much for the clear explantation! you have answered all my questions fully

1 Like

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