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:
So, summarizing, I would like to know what is the standard approach to create dynamic chart interactions using django and bokeh.
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.)
Bokeh creates the HTML file when you call the show() function. This function also automatically opens a web browser to display the HTML file.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With