Make bokeh charts with interactive controls in django Make bokeh charts with interactive controls in django django django

Make bokeh charts with interactive controls in django


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 vformfrom bokeh.models import CustomJS, ColumnDataSource, Sliderfrom bokeh.plotting import Figure, output_file, showoutput_file("callback.html")x = [x*0.005 for x in range(0, 200)]y = xsource = 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