Access data from bokeh widgets in a jupyter notebook

不羁的心 提交于 2019-12-11 04:15:32

问题


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)

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).


回答1:


As of Bokeh 0.12.3, fuller integration of Bokeh widgets in the Jupyter notebook is still an open issue.

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, {}, {});
}
""")

The result looks like:


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.



来源:https://stackoverflow.com/questions/40483510/access-data-from-bokeh-widgets-in-a-jupyter-notebook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!