Python 散景,回调示例在浏览器中不起作用
Python Bokeh, callback example not working in browser
首先,很棒的网站,很棒的人。你们都对我的学习帮助很大。谢谢!
我在使用 Bokeh 和浏览器时遇到问题。特别是,我试图让 Javascript 回调在 Bokeh 中工作。
我从这个网站下载了这个示例代码
https://docs.bokeh.org/en/latest/docs/user_guide/interaction/callbacks.html。
该网站包含使用套索工具的示例。
该代码在网站上运行完美,但是当我将代码复制到 Python 和 运行 中时,JS 回调不起作用。套索工具部分工作正常。我已经在 IE、Chrome、Firefox、工作计算机和家庭计算机上尝试了 运行ning。
总的来说,我不太擅长Javascript,所以任何对这个问题的见解都将不胜感激。
提前致谢。
代码来自以下网站:
from random import random
from bokeh.layouts import row
from bokeh.models import CustomJS, ColumnDataSource
from bokeh.plotting import figure, output_file, show
output_file("callback.html")
x = [random() for x in range(500)]
y = [random() for y in range(500)]
s1 = ColumnDataSource(data=dict(x=x, y=y))
p1 = figure(plot_width=400, plot_height=400, tools="lasso_select", title="Select Here")
p1.circle('x', 'y', source=s1, alpha=0.6)
s2 = ColumnDataSource(data=dict(x=[], y=[]))
p2 = figure(plot_width=400, plot_height=400, x_range=(0, 1), y_range=(0, 1),
tools="", title="Watch Here")
p2.circle('x', 'y', source=s2, alpha=0.6)
s1.callback = CustomJS(args=dict(s2=s2), code="""
var inds = cb_obj.selected['1d'].indices;
var d1 = cb_obj.data;
var d2 = s2.data;
d2['x'] = []
d2['y'] = []
for (i = 0; i < inds.length; i++) {
d2['x'].push(d1['x'][inds[i]])
d2['y'].push(d1['y'][inds[i]])
}
s2.change.emit();
""")
layout = row(p1, p2)
show(layout)
问题是散景版本不同,您使用的是0.12.4版本。在 bokeh 版本 0.12.4 中,要在列数据源中注册更改,您需要使用语法 source.change('trigger').
最新版文档中的例子(就是你引用的例子的出处)使用的是0.12.6版本。
从 bokeh 版本 0.12.6 开始,它已被弃用,语法现在变成了 source.change.emit()
。
首先,很棒的网站,很棒的人。你们都对我的学习帮助很大。谢谢!
我在使用 Bokeh 和浏览器时遇到问题。特别是,我试图让 Javascript 回调在 Bokeh 中工作。
我从这个网站下载了这个示例代码
https://docs.bokeh.org/en/latest/docs/user_guide/interaction/callbacks.html。
该网站包含使用套索工具的示例。
该代码在网站上运行完美,但是当我将代码复制到 Python 和 运行 中时,JS 回调不起作用。套索工具部分工作正常。我已经在 IE、Chrome、Firefox、工作计算机和家庭计算机上尝试了 运行ning。
总的来说,我不太擅长Javascript,所以任何对这个问题的见解都将不胜感激。
提前致谢。
代码来自以下网站:
from random import random
from bokeh.layouts import row
from bokeh.models import CustomJS, ColumnDataSource
from bokeh.plotting import figure, output_file, show
output_file("callback.html")
x = [random() for x in range(500)]
y = [random() for y in range(500)]
s1 = ColumnDataSource(data=dict(x=x, y=y))
p1 = figure(plot_width=400, plot_height=400, tools="lasso_select", title="Select Here")
p1.circle('x', 'y', source=s1, alpha=0.6)
s2 = ColumnDataSource(data=dict(x=[], y=[]))
p2 = figure(plot_width=400, plot_height=400, x_range=(0, 1), y_range=(0, 1),
tools="", title="Watch Here")
p2.circle('x', 'y', source=s2, alpha=0.6)
s1.callback = CustomJS(args=dict(s2=s2), code="""
var inds = cb_obj.selected['1d'].indices;
var d1 = cb_obj.data;
var d2 = s2.data;
d2['x'] = []
d2['y'] = []
for (i = 0; i < inds.length; i++) {
d2['x'].push(d1['x'][inds[i]])
d2['y'].push(d1['y'][inds[i]])
}
s2.change.emit();
""")
layout = row(p1, p2)
show(layout)
问题是散景版本不同,您使用的是0.12.4版本。在 bokeh 版本 0.12.4 中,要在列数据源中注册更改,您需要使用语法 source.change('trigger').
最新版文档中的例子(就是你引用的例子的出处)使用的是0.12.6版本。
从 bokeh 版本 0.12.6 开始,它已被弃用,语法现在变成了 source.change.emit()
。