Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Right way to plot live data with django and bokeh

i have a bokeh plot embedded in a django app. I create the plot in the django view and push the plot to the bokeh server to show the plot in my webpage.

#view.py
def view_plot(request):
    f=figure()
    f.plot(#some data#)
    session = push_session(curdoc())
    context = {'script': autoload_server(f, session_id=session.id)}
    return render_to_response('plot.html', context=context)

It all works quite good. Now I want to do a live plot, every time when a new DB-Entry is created the plot should be updated. I'm not sure what is the best way.

Is it a good practice to use a timer on the webpage to ask for now data?

Or is there a way to push the update form the server so that every currently connected client gets the plot update?

I would be very thankful for every hint.

Thanks a lot.

like image 807
Stefan Farmbauer Avatar asked Apr 24 '16 20:04

Stefan Farmbauer


People also ask

What kind of output file is created using Bokeh visualizations?

Bokeh creates the HTML file when you call the show() function. This function also automatically opens a web browser to display the HTML file.

What is bokeh used for Python?

Bokeh is a Python library that is used to make highly interactive graphs and visualizations. This is done in bokeh using HTML and JavaScript. This makes it a powerful tool for creating projects, custom charts, and web design-based applications.


1 Answers

Basically your problem here is that browsers use a request-response pattern: they send a request and then get back an answer immediately. You have two options, polling the server periodically or some kind of notification system.

Notifications could be long-polling, i.e. client makes a request and server doesn't respond until there's data, or through websockets or through HTML5 server-side events.

Now, the thing is that these notification systems don't integrate too well with a traditional Django deployment, since they result in an open socket and a corresponding hanging thread. So if your webserver has 10 Django threads, one browser with 10 tabs could tie up all of them.

Work is underway to change that, but in the mean time, unless you have a hard real-time requirement or lots of clients, just set a timer and poll every x seconds, where x depends on what latency is acceptable. Depending on how your data is stored, I would probably put in a simple mechanism so that the server doesn't send the whole dataset each time, but only either what's new or a carry-on-nothing-changed return code.

For instance, on the first request, the server may put in a timestamp or serial number in the response, and then the client asks for any changes since that timestamp/serial number.

A notification system gives you better latency with lower overhead, but it is probably also going to be more difficult to deploy, and will probably be overkill if this is just an app for internal use. Even with a notification system, you need to do some careful protocol design to be sure not to miss something.

like image 55
olau Avatar answered Sep 21 '22 07:09

olau