How to capture value of dropdown widget in bokeh python?

不问归期 提交于 2019-12-03 08:50:10

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:

http://bokeh.pydata.org/en/latest/docs/user_guide/interaction/widgets.html

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:

http://bokeh.pydata.org/en/latest/docs/user_guide/server.html

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