在下面的示例中移动 slider 时,如何更新 python 变量 extern_python_variable? extern_python_variable 的值应始终为 f( slider 的实际位置)。谢谢!
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
extern_python_variable=0
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)
test=source.to_df()
layout = vform(slider, plot)
show(layout)
请您参考如下方法:
我最多可以看到三种方法来做你想做的事:
1) 使用 Bokeh 服务器,您可以响应工具事件并与在 Python 中完成的计算同步。 (参见 Running a Bokeh Server)
2) 如果您仍想使用静态 html 页面,那么在工具事件之后,您可以使用 XMLHttpRequest(参见 Using XMLHttpRequest)将数据发送到服务器(可以是本地主机),然后它将等待来自服务器的响应。服务器可以有一个 CGI python 脚本(参见 Python CGI Programming )读取 XMLHttpRequest 发送的数据并将一些值返回给发出请求的 CustomJS。
3) 第三种基本方法可能是打开一个空网页并使用 document.write 函数将数据转储到那里(参见 Window open() Method)。然后将数据复制并粘贴到 python 脚本或 ipython 笔记本中进行分析。
