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.
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.
Streamlit does the button action stated in the if and reruns not remembering old state.
Streamlit does button action stated in the if of the pressed button and all previously pressed buttons.
- Streamlit version: 1.23.1
- Python version: 3.11
- Using Conda
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.