Bokeh: pass vars to CustomJS for Widgets

戏子无情 提交于 2019-12-07 05:30:30

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/32318890/bokeh-pass-vars-to-customjs-for-widgets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!