Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make bokeh charts with interactive controls in django

I have a django application which eventually uses embedded bokeh visualizations.

Right now I get by using the bokeh.embed.components function and a template like:

<body>
    {{the_div|safe}}

    {{the_script|safe}}
</body>

Thanks to this stackoverflow question.

The thing is that now I would need to create more interactive visualizations, adding sliders, checkboxes and other controls.

This example looks like what I want, except for a couple of issues:

  1. I don't know how to embed that kind of object inside Django. I would say this is the way to go, but perhaps it's not.
  2. I'm a little bit confused about having to use the bokeh-server for this. Isn't there any easy-to-use pure javascript solution?

So, summarizing, I would like to know what is the standard approach to create dynamic chart interactions using django and bokeh.

like image 587
NublicPablo Avatar asked Jul 23 '15 11:07

NublicPablo


People also ask

How do you use Bokeh in Django?

Step 3:Complete Bokeh Setup into our project:Create a new file in the templates directory and save it as base. html. Add the following links of CSS in your base. html file in the head tag and replace the version of your bokeh at the place bokeh-x.y.z.min (underlined place x.y.z.)

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.


1 Answers

There are two use cases:


without server

If you can perform any updates in JS (don't need to call actual python code), then it is very easy to add interactions using CustomJS callbacks. There are lots of examples at that link, but a basic simple code sample looks like:

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)

That will create a standalone HTML document with a Bokeh plot and a slider, that updates the plot in reaction to the slider, with no need for a server (i.e. you could email it to someone or serve it on a static page and it would work).


with server

If you want the widgets, interactions, etc. to drive actual python code (e.g. scikit-learn, or Pandas) then you need to use the Bokeh server. Happily the new server as of version 0.11 is much more robust, performant, scalable, and simple to use. You can see several live deployed Bokeh applications (with links to their source code) here:

http://demo.bokeh.org/

As well as extensive documentation about various kinds of deployments in the documentation here:

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

like image 176
bigreddot Avatar answered Oct 19 '22 11:10

bigreddot