I'm trying to update a Bokeh plot in Django when new data is received via a websocket implemented with Django Channels. The aim is to stream new data received over the websocket without having to refresh the browser.
My django view creating the bokeh plot is:
#in views.py
def sensor(request):
plot = figure(title= 'title' ,
x_axis_label= 'X-Axis',
y_axis_label= 'Y-Axis',
plot_width =900,
plot_height =500
source = ColumnDataSource(data=dict(x=[], y=[]))
plot.line('x', 'y', source=source, legend= 'f(x)', line_width = 2)
script, div = components(plot)
return render(request,"sensor.html",{'div':div,'script':script})
The JQuery associated with the websocket is the following (I receive the message as JSON formatted text):
//In a script tag in sensor.html
$(function() {
var ws_scheme = window.location.protocol == "https:" ? "wss" : "ws";
var endpoint = ws_scheme + '://' + window.location.host + window.location.pathname
var ws = new WebSocket(endpoint);
ws.onopen = function(e) {
console.log("open", e);
};
ws.onmessage = function(e) {
console.log("message", e);
var new_data = jQuery.parseJSON(e.data).data);
// UPDATE BOKEH ColumnDataSource WITH new_data
};
});
My problem is that I have no idea how to update the Bokeh ColumnDataSource via JQuery. The closest I've found is using Bokeh CustomJS Callbacks, but I don't see any way of connecting them with the websocket onmessage event, and accessing data collected via the event.
Another option I've tried is embedding a separate Bokeh Server, but I can't find a way to update the plots using my Django Channels websockets. I would also have to pass data from my Django models to the Bokeh server when initializing the plots, which is inconvenient.
And assistance/advice would be appreciated. Thank you