Create bokeh diagrams with interactive django controls

I have a django application that ultimately uses the built-in bokeh renderings.

Right now I am using the bokeh.embed.components function and a template, for example:

 <body> {{the_div|safe}} {{the_script|safe}} </body> 

Thanks to https://stackoverflow.com/a/318618/

The fact is that now I will need to create a more interactive visualization by adding sliders, checkboxes and other controls.

This example looks the way I want, with the exception of a few issues:

  • I do not know how to embed this object in Django. I would say it , but perhaps it is not.
  • I am a little confused by the fact that you need to use a bokeh server for this. Is there a simple javascript solution easy to use?

So, in summary, I would like to know what is the standard approach for creating dynamic diagram interactions using django and bokeh.

+7
python django dynamic embed bokeh
source share
1 answer

There are two uses:


without server

If you can perform any updates in JS (no need to call valid python code), then it is very easy to add interactions using CustomJS callbacks . There are many examples on this link, but a simple simple code example looks like this:

 from bokeh.io import vform from bokeh.models import CustomJS, ColumnDataSource, Slider from bokeh.plotting import Figure, output_file, show output_file("callback.html") x = [x*0.005 for x in range(0, 200)] y = x source = ColumnDataSource(data=dict(x=x, y=y)) plot = Figure(plot_width=400, plot_height=400) plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6) callback = CustomJS(args=dict(source=source), code=""" var data = source.get('data'); var f = cb_obj.get('value') x = data['x'] y = data['y'] for (i = 0; i < x.length; i++) { y[i] = Math.pow(x[i], f) } source.trigger('change'); """) slider = Slider(start=0.1, end=4, value=1, step=.1, title="power", callback=callback) layout = vform(slider, plot) show(layout) 

This will create a standalone HTML document with a Bokeh chart and a slider that will update the chart in response to the slider, without the need for a server (i.e. you can send it to someone or send it to a static page and it will work).


with server

If you want widgets, interactions, etc. Managed real Python code (e.g. scikit-learn or Pandas), you need to use a Bokeh server. Fortunately, the new server with version 0.11 is much more reliable, functional, scalable and easy to use. You can see several deployed Bokeh applications live (with links to their source code):

http://demo.bokehplots.com/

As well as extensive documentation on the various deployment types in the documentation:

http://bokeh.pydata.org/en/0.11.1/docs/user_guide/server.html

+2
source share

All Articles