![](/img/trans.png)
[英]Bokeh Python CustomJS Callback Update Select Widget to filter Scatterplot
[英]Bokeh: Reference Select Widget in CustomJS Callback
我希望能够在CustomJS回调中引用2个选择小部件( select1.value
和select2.value
),所以我不能使用cb_obj.value
。
select1 = Select(title="Level:", options=['All Levels', '1', '2', '3'], callback=callback)
首先,我尝试直接在回调中引用其值:
callback = CustomJS(args=dict(source=source, ts=true_source), code="""
var f = select1.value
这导致错误:未定义select1:
Uncaught ReferenceError: select1 is not defined
at eval (eval at get (bokeh-1.1.0.min.js:31), <anonymous>:9:11)
at i.execute (bokeh-1.1.0.min.js:31)
at e.change_input (bokeh-widgets-1.1.0.min.js:31)
at e.change_input (bokeh-widgets-1.1.0.min.js:31)
at HTMLSelectElement.<anonymous> (bokeh-widgets-1.1.0.min.js:31)
然后我尝试将select1
传递给回调中的args:
callback = CustomJS(args=dict(source=source, ts=true_source, select1=select1), code="""
var f = select1.value
因为select1
的参数是callback=callback
,如果我在定义callback
之前定义select1
,python将生成错误,因为在赋值之前引用了callback
。 反之亦然,如果我在select小部件之前定义callback
。
所以我尝试了这个:定义select1
两次
select1 = ... (without the callback argument)
callback = ...
select1 = ... (with the callback argument)
这最终产生了散景图。 但是当我点击选择小部件时,没有任何价值。
var f=select1.value;
console.log('Select1 type ' + f.constructor.name.toLowerCase()); // string, as expected
console.log('Value ' + f); // outputs 'Value ', so f is nothing
console.log('Select1 options ' + select1.options); // Output is as expected
如果我为select1
提供默认值参数, select1.value
将不为空:
select1 = Select(title="Level:", value='1', options=['All Levels', '1', '2', '3'], callback=callback)
但是,无论我将窗口小部件的实际值更改为什么, select1.value
都将保持为“1”。 所以价值根本没有更新。
我很感激帮助..谢谢!
我终于开始工作了。 我使用js_on_change
而不是将callback
添加为widget参数。
在回调函数中,我将小部件名称作为参数传递。
1)定义选择小部件,指定默认值
select1 = Select(title="Level:", value='All Levels', options=['All Levels', '1', '2', '3'])
select2 = Select(...)
2)定义回调,明确地将小部件作为参数传递
callback = CustomJS(args=dict(source=source, ts=true_source, select1=select1, select2=select2), code="""
f = select1.value
g = select2.value
"""
3)js_on_change
select1.js_on_change('value', callback)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.