python bokeh 控件_如何在bokeh python中捕获下拉窗口小部件的值?

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

Question

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?

EDIT

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 This answer does not apply for Bokeh Versions 2.X.X anymore. See comment and the other answer below.

If you set on_change e.g. as follows:

dropdown.on_change('value', 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

For this to work dropdown has to be defined before function_to_call.

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:

EDIT

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:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值