Bokeh 的一个好处是,可以从 Python 层指定回调,从而在 javascript 级别上产生操作,而无需 bokeh 服务器。因此,人们可以创建在浏览器中运行的交互式小部件,而无需运行 Ipython 或 Bokeh 服务器。
0.9.3。文档给出了一个我可以在 ipython 笔记本中重现的示例:http://docs.bokeh.org/en/latest/docs/user_guide/interaction.html#cutomjs-for-widgets 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)
我想调整这样的代码来创建一些简单的在线作业。我的问题是如何在不调用滑块的情况下直接将其他变量从 python 传递到 javascript。例如,假设我希望 Javascript 变成:
y[i] = Math.pow(x[i], A*f)
其中 A 是在上面的 ipython 代码单元中定义的(例如 A = 10)。在 javascript 中定义 'var A = 10' 很容易,但我想在 python 中设置 A 和其他变量的值,然后将它们传递到此 javascript 中。有办法吗?