I can’t understand why the variables a, b, and st.session_state_idx do not update in the first 3 st.write calls the first time I click on the button. The last write, right at the bottom of this snippet does show the correct values.
If this is the normal behaviour, could you please explain why? I thought the whole script was re-executed every time I interact with a widget.
Also, could you explain what I would need to do to obtain the desired behaviour?
Many thanks!
Steps to reproduce
@st.cache_data
def get_numbers(step):
np.random.seed(int(time.time()))
return np.random.randint(0, 10, 20, dtype=int).reshape((-1, 2))
if "idx" not in st.session_state:
st.session_state.idx = 0
numbers = get_numbers(0)
a, b = numbers[st.session_state.idx]
st.write(f"idx={st.session_state.idx}, a={a}, b={b}")
st.write(f"{a} x {b} = ")
reponse = st.text_input("Reponse")
run = st.button('Submit')
if run:
st.write(f"idx={st.session_state.idx}, a={a}, b={b}, reponse={reponse}")
st.session_state.idx += 1
a, b = numbers[st.session_state.idx]
st.write(f"idx={st.session_state.idx}, a={a}, b={b}")
Expected behavior:
I expect all 4 write calls to show the same values at all times
Actual behavior:
Only the last write, at the bottom, shows the current value of the variables. All 3 others show the previous values.
When you click a button, the page reloads with that button returning True.
All the content on the page before the button is going to reload the same as it did before.
When the script gets to if run:, the content of the conditional is executed. This includes one line of writing out the values unmodified before the increment occurs.
Here’s another thread where someone was asking and I described the run logic for a couple cases.
Basically all widgets trigger a rerun. The purely organizational items like tabs and expanders don’t since they don’t send anything to the backend. Also, when you put widgets inside a form, that prevents them from sending the update to the backend (and rerunning) until the form submit button is 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.