Summary
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.
Debug info
- Streamlit version: 1.21.0
- Python version: 3.8.16
- Using pip inside a Docker image
- OS version: Debian 11.6
- Browser version: Chrome 112.0.5615.137