There are a couple open issues for this one (or at least issues that I think may play a role here):
opened 08:21AM - 08 May 23 UTC
type:bug
priority:P3
feature:config
### Checklist
- [X] I have searched the [existing issues](https://github.com/st… reamlit/streamlit/issues) for similar issues.
- [X] I added a very descriptive title to this issue.
- [X] I have provided sufficient information below to help reproduce this issue.
### Summary
It looks like `st.session_state` misbehaves since 1.20, at least when used together with an infinite loop.
Buttons on_click functions are no more executed a second time.
1) With version **1.19 it is OK**: clicking buttons it reacts correctly resetting and descreasing the respective counter.
2) With version 1.20+ a second click on a button is not recognized anymore, and the `st.session_state` dictionary complains that the `counter` key does not exist.
Was there a features change in 1.20 to justify this behavior?
### Reproducible Code Example
```Python
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']}")
```
### Steps To Reproduce
Install streamlit 1.19, then 1.20.
Clicking each button you see that the behavior is completely different, because they stop working after the first click for the newest libraries.
### Expected Behavior
_No response_
### Current Behavior
_No response_
### Is this a regression?
- [X] Yes, this used to work in a previous version.
### Debug info
- Streamlit version: 1.19 - 1.20+
- Python version: 3.8
- Operating System: Ubuntu
- Browser: Firefox, Brave, Edge
- Virtual environment: venv
### Additional Information
_No response_
### Are you willing to submit a PR?
- [ ] Yes, I am willing to submit a PR!
opened 12:34AM - 15 Apr 23 UTC
type:bug
status:needs-triage
### Checklist
- [X] I have searched the [existing issues](https://github.com/st… reamlit/streamlit/issues) for similar issues.
- [X] I added a very descriptive title to this issue.
- [X] I have provided sufficient information below to help reproduce this issue.
### Summary
Upon user click of b1 the loop begins and after click of b2 the loop continues despite the while loop condition evaluating to false.
### Reproducible Code Example
```Python
import streamlit as st
import time
st.title("while loop test")
col1, col2 = st.columns(2)
with col1:
b1 = st.button("Button 1")
with col2:
b2 = st.button("Button 2")
if b1:
# run until user clicks b2
while True:
print("im working")
if b2:
break
time.sleep(2)
if b2:
print("hit b2")
```
### Steps To Reproduce
_No response_
### Expected Behavior
The console should stop printing "im working" after pressing b2
### Current Behavior
The console prints "im working" indefinitely
### Is this a regression?
- [ ] Yes, this used to work in a previous version.
### Debug info
- Streamlit version:
- Python version:
- Operating System:
- Browser:
- Virtual environment:
### Additional Information
_No response_
### Are you willing to submit a PR?
- [ ] Yes, I am willing to submit a PR!
Try doing these two things:
In your config file (create one if you aren’t using one yet), set:
[runner]
fastReruns = false
Add some frontend Streamlit command into your infinite loop (if you don’t have already)
while True:
time.sleep(1)
st.write('')
Or:
foo = st.empty()
while True:
time.sleep(1)
foo.write('')
This behaves as expected (with fastRerun disabled)
import streamlit as st
import time
st.write(st.session_state)
st.button("Test 1", key='test_1')
st.button('test 2', key='test 2')
st.write(st.session_state)
while True:
time.sleep(1)
st.write('')
This also behaves as expected (with fastRerun disabled)
import streamlit as st
import time
if st.button("Test 1", key='test_1'):
print(1)
if st.button('test 2', key='test 2'):
print(2)
def generate_graph():
foo = st.empty()
while True:
time.sleep(1)
foo.write('')
def main():
generate_graph()
if __name__ == '__main__':
main()
1 Like