It looks like st.session_state
misbehaves since 1.20, at least when used together with an infinite loop.
import streamlit as st
import time
if "button" not in st.session_state:
st.session_state["button"] = "----"
if "counter" not in st.session_state:
st.session_state["counter"] = 0
def btn1_click():
st.session_state["counter"] = 10
st.session_state["button"] = "Btn1"
def btn2_click():
st.session_state["counter"] = 20
st.session_state["button"] = "Btn2"
cols = st.columns(3)
with cols[0]:
st.button("Btn1", on_click=btn1_click)
with cols[1]:
st.button("Btn2", on_click=btn2_click)
with cols[2]:
ph = st.empty()
i = 0
while True:
time.sleep(.1)
if st.session_state["counter"] > 0:
st.session_state["counter"] -= 1
if st.session_state["counter"] == 0:
st.session_state["button"] = "----"
with ph.container():
st.write(st.session_state)
i+=1
print(f"{i: 4d} button {st.session_state['button']} => counter {st.session_state['counter']}")
- With version 1.19 it is OK
- With version 1.20+ a second click on a button is not recognized anymore, and the
st.session_state
dictionary complains that thecounter
key does not exist.
Was there a features change in 1.20 to justify this behavior?