关于Bokeh的一个好处是,可以从Python层指定回调,该回调可以在javascript级别上执行操作,而无需使用bokeh-server.因此,可以创建在浏览器中运行的交互式小部件,而无需运行Ipython或Bokeh服务器.
0.9.3.文档提供了一个我可以在ipython笔记本中重现的示例:http://bokeh.pydata.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 …Run Code Online (Sandbox Code Playgroud)