Javascript Bokeh从CustomJS获取值(更改数据源中的值)
我改了。 最终,我想要的是一种在图形中选择数据点并在python代码中修改它们的方法。因此,我在这里添加了一个函数,它应该返回第二个图形中的值(这就是按钮的作用)。但是,如果我选择了点,它们被正确地绘制,但是数据源没有改变(按钮点击提供了{'X':[],'Y':[])。我如何从JS写回python bokeh数据源?s2.change.emit()或s2.trigger('change')不应该这样做吗?我也尝试了后者,但它不起作用Javascript Bokeh从CustomJS获取值(更改数据源中的值),javascript,python,bokeh,Javascript,Python,Bokeh,我改了。 最终,我想要的是一种在图形中选择数据点并在python代码中修改它们的方法。因此,我在这里添加了一个函数,它应该返回第二个图形中的值(这就是按钮的作用)。但是,如果我选择了点,它们被正确地绘制,但是数据源没有改变(按钮点击提供了{'X':[],'Y':[])。我如何从JS写回python bokeh数据源?s2.change.emit()或s2.trigger('change')不应该这样做吗?我也尝试了后者,但它不起作用 from random import random from
from random import random
from bokeh.layouts import row
from bokeh.models import CustomJS, ColumnDataSource
from bokeh.plotting import figure
from bokeh.models.widgets import Button
from bokeh.io import curdoc
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.selected.js_on_change('indices', CustomJS(args=dict(s1=s1, s2=s2), code="""
var inds = cb_obj.indices;
var d1 = s1.data;
var d2 = s2.data;
d2['x'] = []
d2['y'] = []
for (var i = 0; i < inds.length; i++) {
d2['x'].push(d1['x'][inds[i]])
d2['y'].push(d1['y'][inds[i]])
}
s2.change.emit();
""")
)
def get_values():
global s2
print(s2.data)
button = Button(label="Get selected set")
button.on_click(get_values)
layout = row(p1, p2,button)
curdoc().add_root(layout)
curdoc().title = "my dashboard"
从随机导入随机
从bokeh.layouts导入行
从bokeh.models导入CustomJS,ColumnDataSource
从bokeh.plotting导入图形
从bokeh.models.widgets导入按钮
从bokeh.io导入curdoc
x=[random()表示范围(500)内的x]
y=[random()表示范围(500)内的y]
s1=列数据源(数据=dict(x=x,y=y))
p1=图形(绘图宽度=400,绘图高度=400,工具=“套索选择”,标题=“在此处选择”)
p1.圆('x','y',源=s1,α=0.6)
s2=ColumnDataSource(数据=dict(x=[],y=[]))
p2=图形(绘图宽度=400,绘图高度=400,x范围=(0,1),y范围=(0,1),
tools=“”,title=“在此处观看”)
p2.圆('x','y',源=s2,α=0.6)
s1.selected.js_on_change('index',CustomJS(args=dict(s1=s1,s2=s2),code=”“”
var inds=cb_对象指数;
var d1=s1.1数据;
var d2=s2.0数据;
d2['x']=[]
d2['y']=[]
对于(变量i=0;i
单击按钮时,此代码将显示第二个绘图中更新的源数据。使用:bokeh serve--show app.py
from random import random
from bokeh.models import CustomJS, ColumnDataSource, Row
from bokeh.plotting import figure, show, curdoc
from bokeh.models.widgets import Button
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.selected.js_on_change('indices', CustomJS(args = dict(s1 = s1, s2 = s2), code = """
var inds = cb_obj.indices;
var d1 = s1.data;
d2 = {'x': [], 'y': []}
for (var i = 0; i < inds.length; i++) {
d2['x'].push(d1['x'][inds[i]])
d2['y'].push(d1['y'][inds[i]])
}
s2.data = d2 """))
def get_values():
print(s2.data)
button = Button(label = "Get selected set")
button.on_click(get_values)
curdoc().add_root(Row(p1, p2, button))
从随机导入随机
从bokeh.models导入CustomJS、ColumnDataSource、Row
从bokeh.plotting导入图形,显示,curdoc
从bokeh.models.widgets导入按钮
x=[random()表示范围(500)内的x]
y=[random()表示范围(500)内的y]
s1=列数据源(数据=dict(x=x,y=y))
p1=图形(绘图宽度=400,绘图高度=400,工具=“套索选择”,标题=“在此处选择”)
p1.圆('x','y',源=s1,α=0.6)
s2=ColumnDataSource(数据=dict(x=[],y=[]))
p2=图形(绘图宽度=400,绘图高度=400,x\U范围=(0,1),y\U范围=(0,1),tools=“”,title=“在此处观看”)
p2.圆('x','y',源=s2,α=0.6)
s1.selected.js_on_change('index',CustomJS(args=dict(s1=s1,s2=s2),code=”“”
var inds=cb_对象指数;
var d1=s1.1数据;
d2={'x':[],'y':[]}
对于(变量i=0;i
在您的示例中,源数据没有更新,因为JS中没有检测推送更改的机制。只能将新对象分配给
源。可以检测到数据。一旦检测到更改,BokehJS和Python模型之间的数据就可以同步。太好了,thx!您能简要描述一下s2.change.emit()是什么吗是否需要?似乎不再需要它了。当您通过引用直接修改源.data
时使用它(不使用新对象替换整个source.data对象)。请注意,您的原始示例使用了对现有source.data的引用(因此不使用新对象替换它)