如何在散景 python 中捕获下拉小部件的值?
问题描述
链接中散景 0.12.1 的官方文档提供了以下用于创建下拉列表的代码.
The official documentation of bokeh 0.12.1 in the link give the below code for creating a dropdown.
http://docs.bokeh.org/en/latest/docs/user_guide/interaction/widgets.html#userguide-interaction-widgets
但它没有明确提及当有人单击并从下拉列表中选择一个值时如何捕获下拉小部件的值.
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))
问题
看到有两个方法叫做 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))
解决方案
EDIT 此答案不再适用于 Bokeh Versions 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(bokeh 版本 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
编辑
要获得交互式反馈,您必须在服务器模式下运行散景,以便在您与小部件交互时评估 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)
更多信息请看这里:
http://docs.bokeh.org/en/latest/docs/user_guide/server.html
相关文章