Update dashboard elements of plotly-DASH using pymongo.change_streams()

Hi,
I am trying to build a customized dashboard using plotly-dash which needs to get updated (automatically) whenever a new record is inserted into the MongoDB Atlas collection on the cloud. I was looking for examples online but could not find any. For example, lets assume the data that gets inserted into mongoDB (asynchronously at random time intervals) is a time series data like:

{
"time" : "14.04.2022 21:50:59",
"balance" : 10500.50
}

I understand that it is possible to set an interval to routinely update charts in dash, but is it possible to use pymongo.change_streams to watch for new insertions into the collection, and then automatically update the plotly charts or dataframe table in the dashboard.

Can anyone show a simple example of a plotly-dash line chart or may be a datatable that gets updated using plotly callbacks and pymongo.change_streams whenever a new record is inserted into a MongoDB collection?

Best Regards,
Dilip

One possible option would be to create a small data routing server (e.g. using Quart) that integrates with pymongo and exposes a websocket endpoint with the data, which is then read by a Websocket component in Dash.

Thanks but seems slightly roundabout task, Perhaps it should be possible with pymongo.change_streams itself. It seems someone has asked a similar question in stackoverflow but no reply from any plotly/mongoDB expert on this issue, so far?

To my knowledge, there is no native integration between Dash and pymongo streams. That’s why I suggested to use a web socket server as an adapter (unlike using e.g. an interval component, the web socket solution would in fact yield instant updates).

If you need a native integration, I believe the best approach would be to create a custom Dash component that takes care of the integration. If you are not too afraid of JavaScript/React it should be doable, but it’s a bit more work that the adapter solution, I would say.

2 Likes