Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bokeh: pass vars to CustomJS for Widgets

A nice thing about Bokeh is that callbacks can be specified from the Python layer that result actions on the javascript level without the need of bokeh-server. So one can create interactive widgets that run in a browser without an Ipython or Bokeh server running.

The 0.9.3. documentation gives an example that I can reproduce in an ipython notebook: http://docs.bokeh.org/en/latest/docs/user_guide/interaction.html#cutomjs-for-widgets

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)

I want to adapt code like this to create some simple online assignments. My question is how can I pass other variables from python to javascript directly without invoking a Slider. For example suppose I want the Javascript to become:

y[i] = Math.pow(x[i], A*f)

where the A was defined in an ipython code cell above (for example A = 10). It's easy enough to define 'var A = 10' in the javascript but I'd like to set the value of A and other variables in python and then pass them into this javascript. Is there a way?

like image 924
JHuw Avatar asked Aug 31 '15 19:08

JHuw


1 Answers

As of Bokeh 0.9.3 you can only pass "Bokeh Models" (e.g. things like data sources and renderers), not arbitrary python objects. But we are working on extending bokeh documents with a simple namespace concept that can be mirrored easily.

like image 60
bigreddot Avatar answered Sep 28 '22 11:09

bigreddot