如何在bokeh python中捕获下拉窗口小部件的值? [英] How to capture value of dropdown widget in bokeh python?
问题描述
链接中的bokeh 0.12.1的官方文档提供了以下用于创建下拉列表的代码.
The official documentation of bokeh 0.12.1 in the link give the below code for creating a dropdown.
但是它并没有明确提到当有人单击并从下拉菜单中选择一个值时如何捕获下拉窗口小部件的值.
But it doesn't clearly mention how to capture the value of the dropdown widget when someone click and selects a value from the dropdown.
from bokeh.io import output_file, show
from bokeh.layouts import widgetbox
from bokeh.models.widgets import Dropdown
output_file("dropdown.html")
menu = [("Item 1", "item_1"), ("Item 2", "item_2"), None, ("Item 3", "item_3")]
dropdown = Dropdown(label="Dropdown button", button_type="warning", menu=menu)
show(widgetbox(dropdown))
问题
看到有2种方法称为on_click()& on_change(),但是从文档中无法得知如何捕获该值. 如何将选定的值分配给新变量?
Is see that there are 2 methods called on_click() & on_change() but from the documentation couldn't figure out how to capture the value. How can we assign the selected value to a new variable?
编辑
根据来自@Ascurion的输入,我更新了我的代码,如下所示.但是当我在下拉列表中选择一个值时,在Spyder的ipython控制台中不会打印任何内容. 请告知.
Based on input from @Ascurion i have updated my code as shown below. But when i select a value in dropdown nothing is printed in ipython console in Spyder. Please advise.
from bokeh.io import output_file, show
from bokeh.layouts import widgetbox
from bokeh.models.widgets import Dropdown
output_file("dropdown.html")
menu = [("Item 1", "item_1"), ("Item 2", "item_2"), None, ("Item 3", "item_3")]
dropdown = Dropdown(label="Dropdown button", button_type="warning", menu=menu)
def function_to_call(attr, old, new):
print dropdown.value
dropdown.on_change('value', function_to_call)
dropdown.on_click(function_to_call)
show(widgetbox(dropdown))
推荐答案
编辑此答案不再适用于Bokeh版本2.X.X.请参阅下面的评论和其他答案.
EDIT This answer does not apply for Bokeh Versions 2.X.X anymore. See comment and the other answer below.
如果您设置on_change,例如如下:
If you set on_change e.g. as follows:
dropdown.on_change('value', function_to_call)
可以按以下方式访问function_to_call
中所选项目的值:
one can access the value of the selected item in function_to_call
as follows:
def function_to_call(attr, old, new):
print dropdown.value
要使其正常工作,必须在function_to_call之前定义下拉列表.
For this to work dropdown has to be defined before function_to_call.
有关如何通过on_click和on_change(散景版本12.1)访问在小部件中设置的值的文档,请参见页面顶部:
The documentation on how to access values set in widgets with on_click and on_change (bokeh version 12.1) can be found here at the top of the page:
http://docs.bokeh.org/en /latest/docs/user_guide/interaction/widgets.html
编辑
要获得交互式反馈,您必须在服务器模式下运行bokeh,以便在与小部件进行交互时可以评估python代码.我稍微修改了您的示例,使其可以与
To get interactive feedback you have to run bokeh in server mode, so that the python code can be evaluated when you interact with a widget. I changed your example slightly to allow to be run with the
bokeh serve --show file_name.py
命令.然后,下面的代码将在终端中打印出选定的项目.
command. The code below then prints out the selected item in the terminal.
from bokeh.io import output_file, show
from bokeh.layouts import widgetbox
from bokeh.models.widgets import Dropdown
from bokeh.plotting import curdoc
menu = [("Quaterly", "time_windows"), ("Half Yearly", "time_windows"), None, ("Yearly", "time_windows")]
dropdown = Dropdown(label="Time Period", button_type="warning", menu=menu)
def function_to_call(attr, old, new):
print dropdown.value
dropdown.on_change('value', function_to_call)
curdoc().add_root(dropdown)
有关更多信息,请参见此处:
See here for more information:
http://docs.bokeh.org/en/latest /docs/user_guide/server.html
这篇关于如何在bokeh python中捕获下拉窗口小部件的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!