如何在python散景中按钮点击执行onclick动作?

时间:2018-04-17 05:52:07

标签: python python-3.x button visualization bokeh

我是散景新手,我对按钮onclick事件有了兴趣,代码如下:

    x = widgetbox(button)
    show(x)
    fruits = ['Answered', 'Unanswered','Total']
    top1=[1,2,3]
    def callback():
        p = figure(x_range=fruits, plot_height=250, title="sophia bot")
        p.vbar(x=fruits, top=top1, width=0.9)
        p.xgrid.grid_line_color = None
        p.y_range.start = 0
        output_file("abc.html")
        show(p)
    button_one = Button(label="Start", disabled=True, callback=callback)
    show(button_one)

但是这并没有执行动作,单独使用下面的代码时,绘制图形,我希望当我按下按钮时图形显示。

    fruits = ['Answered', 'Unanswered','Total']
    top1=[1,2,3]
    p = figure(x_range=fruits, plot_height=250, title="sophia bot")
    p.vbar(x=fruits, top=top1, width=0.9)
    p.xgrid.grid_line_color = None
    p.y_range.start = 0
    output_file("abc.html")
    show(p)

由于

1 个答案:

答案 0 :(得分:3)

您必须将回调分配给按钮,如下所示:

from bokeh.models import Button
from bokeh.io import curdoc

bt = Button(label='Click me')

def change_click():
    print('I was clicked')

bt.on_click(change_click)

curdoc().add_root(bt)

使用bokeh serve --show example.py启动此功能。

注意:另请查看我在谈论动态布局的my question