My idea is to develop an app that takes data from mongoDB database. It will pull out from the MongoDB data at every fixed time-step (so far I have fixed every 5 min). Then, I will turn into dataframes, perform some calculations and plot some results. The idea is to present both accomolative results and results related to the real-time data. I would to know if anyone has an example code (with similar characteristics), or recommend best packages to execute that - packages like to plot real-time data.
The āPlottingā demo from streamlit hello does something similar to what you are describing:
import time
import numpy as np
progress_bar = st.sidebar.progress(0)
status_text = st.sidebar.empty()
last_rows = np.random.randn(1, 1)
chart = st.line_chart(last_rows)
for i in range(1, 101):
new_rows = last_rows[-1, :] + np.random.randn(5, 1).cumsum(axis=0)
status_text.text("%i%% Complete" % i)
chart.add_rows(new_rows)
progress_bar.progress(i)
last_rows = new_rows
time.sleep(0.05)
progress_bar.empty()
# Streamlit widgets automatically run the script from top to bottom. Since
# this button is not connected to any other logic, it just causes a plain
# rerun.
st.button("Re-run")
The key line is chart.add_rows(new_rows), which appends data frame rows to a chart object. When you do this, the app knows that an input has changed and re-renders the graph appropriately.
Using this method, the rest of the code wonāt get executed until the end of the for loop.
Is there a way to run the loop in the ābackgroundā while letting the rest of the code run? Is there where the āasyncā technique comes into handle?
Thanks.
This eventually runs for a certain durations and halts.
It can also be achieved with a similar condition for the seamless running of the chart.
is there a way to control the data rows(last_rows) inside the chart?
If I want to restrict the data points inside the chart( to show only last 10 points).
import time
import numpy as np
last_rows = np.random.randn(1, 1)
chart = st.line_chart(last_rows)
i = 0
while True:
i += 1
new_rows = last_rows[-1, :] + np.random.randn(5, 1).cumsum(axis=0)
chart.add_rows(new_rows)
last_rows = new_rows
time.sleep(0.05)
I was able to create a dynamic radar chart that continuously updates with a stream of data using a āst.empty()ā placeholder to write my chart to and a while loop to continuously re-run the chart.
Great demonstration @mkhorasani! Do you have a Streamlit sharing invite? If so, itād be a great example for the community to see liveā¦if not, send me a direct message and weāll get you set up
is it possible to use the add_row function also with Plotly?
I didnt archive it. I tried to create a like that: chart = st.plotly_chart(ādataā) and then chart.add_rows(new_rows). It didnt worked.
I have jumped through majority of streamlit forums with keywords relating to graphical updates and have yet to see a good solution. I am quite surprised that this issue has not been formally addressed considering there have been so many questions asked regarding this. Majority if not all of the forum posts have been ignored by streamlit. Seems like a common issue amongst many streamlit users.
@snehankekre, Thank for the suggestion! Plotly is v5.1+ at this time. That feature does not work unless you run an outdate version with drastically less features. Sorry, not quite the solution, perhaps there is another way to accomplish this?
@SEVEN As this is a limitation / issue with the Plotly Python package, might be best to ask in the Plotly forum or create a new GitHub issue for a feature request?