I have made a toy example of a streamlit plot that shows live data as follows
import streamlit as st
from random import random
from time import sleep
data = []
with st.empty():
while True:
st.line_chart(data)
data.append(random())
if len(data) > 30:
data = data[-30:]
sleep(0.1)
if st.toggle("Show message"):
st.write("Meggase")
The problem is that, kind of obviously, this prevents the toggle from rendering. How can I draw an infinitely refreshing plot without making everything after it not render? Also, even though I was not able to replicate it here, in my real application this infinite loop makes the web page very unresponsive. This is mitigated by switching the while True
with a for loop, even though it introduces a delay (you have to wait until the loop finishes to get the action done). Is there a better way to do a live plot on streamlit? Something that is executed once in the python script and keeps refreshing on the background would be ideal.