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:
    time.sleep(1)
    state = run(read_plc())
    json_view.json(state)
    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.

Matteo

2 Likes

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: https://github.com/streamlit/streamlit/issues/514

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.

Matteo

1 Like

This is a small gist that implements a sliding-window dashboard-style plot that periodically pulls from a random generator: https://gist.github.com/monchier/4ba216cc9168a7d2bea7f3f31a1c4491

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.

Best,
Matteo

Hello,
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()
        send_data_to_db()

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

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

p1.start()
p2.start()

p1.join()
p2.join()

Result: An the default webpage spawned by streamlit.