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