Continuously updating dashboard

Love using this project so far!

Our key application of this tool would be a live dashboard reading data from sensors and continuously updating their state and history. In order to do that I want the page to automatically update every second or so.

I can get this to work by abusing a while loop like this:

state = run(read_plc())
json_view = st.json(state)
line_chart = st.line_chart([{key: val for key, val in state.items()}])
while True:
    state = run(read_plc())
    line_chart.add_rows([{key: val for key, val in state.items()}])

But that feels like it’s abusing the abstraction a bit. Is there a better way to achieve this behavior? I’d love a method to specify an update frequency.

1 Like

Actually. To me thats the beauty of streamlit that you can do it like that.

Hi @JamesJeffryes, this is a very interesting use-case. The code you are suggesting will work, but we are working at providing first-class support for dashboards. To give you some context, by wrapping your logic inside the while True loop, Streamlit will not release resources (thread and connection) while a user is connected. Streamlit will release resources once the user disconnects. Depending on your specifics this may or may not be acceptable. In general, we think that the code of a Streamlit App should run to completion and re-runs should be used on changes.

Unfortunately, it is not possible at the moment to programmatically trigger re-runs, but we are investigating whether it makes sense to implement a mechanism to allow that. By programmatically triggering a re-run, you would be able to control when re-executing and pulling new data. If you are interested, I can provide a gist on top of Streamlit to try that out and definitely I will keep you posted on the progress of this feature.



I’d definitely be interested in programmatic triggers for re-runs. Streamlet seems to abstract away web concepts better than any other python library I’ve played with so I think there could be real interest in using it to make simple interfaces for IoT devices. Not releasing resources while the user is connected would likely be fine for our use cases, especially if the websocket is reconnecting.

1 Like

Thanks @JamesJeffryes. I opened a feature request for reruns:

I do think another piece of the problem is how to keep some state between re-runs. We are looking at solutions for this as well.


1 Like

This is a small gist that implements a sliding-window dashboard-style plot that periodically pulls from a random generator:

It uses two other gists that we are considering merging into Streamlit, but you should be able to use them right now. One gist implements reruns, the other one implements SessionState. The gist above has the links to the other two.


I am trying to develop a dashboard too. However, I would like to keep the sensor data collection isolated from the dashboard. So I spawn them within two different processes. The code runs without errors but does not output anything? How to use streamlit within a subprocess? Or what is the best way to isolate these two parts. Code snippet:

def collect_data():
    while True:
        s = read_sensor_data()

def dashboard():
    while True:
        df = pd.read_sql(...)

p1 = mp.Process(collect_data())
p2 = mp.Process(dashboard())



Result: An the default webpage spawned by streamlit.