Streamlit buttons not resetting state when a for loop is running

There are a couple open issues for this one (or at least issues that I think may play a role here):

Try doing these two things:

  1. In your config file (create one if you aren’t using one yet), set:
[runner]
 fastReruns = false
  1. 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