I’m trying to create real time monitoring and control dashboard using streamlit following this tutorial: How to build a real-time live dashboard with Streamlit
This works well for the monitoring part, but I’m also trying to add buttons to do things like a http post.
When i add multiple buttons, every time i press a button all previously pressed buttons get pressed too, which does not make sense as streamlit should rerun after every interaction and should not keep the state of buttons. I noticed this behaviour happen because a for loop is present, which is needed for the real time dashboard as per the tutorial for refreshing the graphs and loading the data.
Steps to reproduce
Code snippet:
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():
# Continuously generate/update the graph
while True:
time.sleep(1)
# st.write(datetime.now())
def main():
generate_graph()
if __name__ == '__main__':
main()
When pressing button 1 it will print 1. When pressing 2 after this both 1 and 2 will be printed.
Expected behavior:
Streamlit does the button action stated in the if and reruns not remembering old state.
Actual behavior:
Streamlit does button action stated in the if of the pressed button and all previously pressed buttons.
Debug info
Streamlit version: 1.23.1
Python version: 3.11
Using Conda
Requirements file
streamlit==1.23.1
Additional information
Pressing the clear cache button in the hamburger menu fixes this problem, but i did not find a way to programmatically do this action after every rerun.
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:
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()
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.