从Jupyter笔记本中的bokeh小部件访问数据 [英] Access data from bokeh widgets in a jupyter notebook
问题描述
我想在自动完成的jupyter笔记本中使用文本输入小部件.因此,我使用了bokeh.models.widgets.inputs
中的AutocompleteInput()
.
I want to use a text input widget in a jupyter notebook with autocompletion. I therefore used AutocompleteInput()
from bokeh.models.widgets.inputs
.
from bokeh.models.widgets.inputs import AutocompleteInput
from bokeh.io import output_notebook
from bokeh.plotting import show
output_notebook()
txt_input = AutocompleteInput(completions=['val1', 'val2'])
show(txt_input)
显示窗口小部件和自动完成功能正常,但是如何获取该值更改后的输入小部件的数量? txt_input.value
仅返回默认值(空字符串).
Displaying the widget and autocompletion works fine, but how can I access the value of the input widget upon change? txt_input.value
only returns the default value (an empty string).
推荐答案
从Bokeh 0.12.3
开始,在Jupyter笔记本中Bokeh小部件的完整集成是
As of Bokeh 0.12.3
, fuller integration of Bokeh widgets in the Jupyter notebook is still an open issue.
但是,有一些变通办法,尽管可能有些笨拙.这是一个CustomJS
回调,您可以将其传递给将设置python值的窗口小部件:
However, there are some workarounds, though they may be considered somewhat clunky. Here is a CustomJS
callback you can pass to the widget that will set the value of a python value:
from bokeh.models import CustomJS
callback = CustomJS(code="""
if (IPython.notebook.kernel !== undefined) {
var kernel = IPython.notebook.kernel;
cmd = "widget_value = '" + cb_obj.value + "'";
kernel.execute(cmd, {}, {});
}
""")
结果如下:
CustomJS
代码中的cmd
变量的值是将在当前运行的Jupyter内核中执行的python代码字符串.例如,如果您需要调用某些python函数,也可以这样做.
The value of the cmd
variable in the CustomJS
code is string of python code that will be executed in your currently running Jupyter kernel. If you need to call some python function, e.g., you could do that too.
这篇关于从Jupyter笔记本中的bokeh小部件访问数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!