Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
722 views
in Technique[技术] by (71.8m points)

plot - How to capture value of dropdown widget in bokeh python?

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

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))
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In Bokeh 2.0.0, Dropdown.value was removed. The correct way to get what item has been clicked is:

from bokeh.models import Dropdown

d = Dropdown(label='Click me', menu=['a', 'b', 'c'])


def handler(event):
    print(event.item)


d.on_click(handler)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...