Python Bokeh:如何在子例程中更新“切换"按钮(在主菜单中定义) [英] Python Bokeh: How to update a Toggle button - defined in the main - in a subroutine

查看:152
本文介绍了Python Bokeh:如何在子例程中更新“切换"按钮(在主菜单中定义)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下简单的bokeh示例. 开始"按钮在子例程中启动一个不定式的while循环,一旦按下按钮3或取消选中该复选框,该循环应立即停止运行. Button2会检查状态,而无需循环,这会正常工作.由于button3和复选框cb在主目录中定义,因此button1调用的子例程无法识别更改.有办法解决吗?

I have the following simple bokeh example. The start button starts an infinitive while loop in a subroutine, which should stop running as soon as button 3 is pressed or the checkbox is unchecked. Button2 checks the status without the loop which works fine. As button3 and the checkbox cb are defined in the main the subroutine called by button1 does not recognize the change. Is there a way to solve this?

我使用了bokeh版本1.0.1.您可以使用bokeh serve script.py完整运行该示例,然后在浏览器中查看该示例( http://localhost:5006 ).

I used bokeh version 1.0.1. You can run the example lokally with bokeh serve script.py and view it in your browser (http://localhost:5006).

from bokeh.models import Column
from bokeh.plotting import curdoc
from bokeh.models.widgets import Button, Toggle, CheckboxGroup
import time

def start_loop():
    while (not button3.active) and (len(cb.active)):
        time.sleep(1)
        print(button3.active)
        print(cb.active)

def check_status():
    print(button3.active)
    print(cb.active)

button1 = Button(label = "start")
button1.on_click(start_loop)

button2 = Button(label = "check status")
button2.on_click(check_status)

button3 = Toggle(label="stop")
cb = CheckboxGroup(labels=['stop'],active=[0])

curdoc().add_root(Column(button1,button2,button3,cb))

推荐答案

我认为while循环会干扰Tornado IO_loop.我建议您改用add_periodic_callback(Bokeh v1.1.0)

I think the while loop interferes the Tornado IO_loop. I advice you to use add_periodic_callback instead (Bokeh v1.1.0)

from bokeh.models import Column
from bokeh.plotting import curdoc
from bokeh.models.widgets import Button, Toggle, CheckboxGroup
import time

# def start_loop():
#     while (not button3.active) and (len(cb.active)):
#         time.sleep(1)
#         print(button3.active)
#         print(cb.active)

def check_status():
    print(button3.active)
    print(cb.active)

# button1 = Button(label = "start")
# button1.on_click(start_loop)

button2 = Button(label = "check status")
button2.on_click(check_status)

button3 = Toggle(label = "stop")
cb = CheckboxGroup(labels = ['stop'], active = [0])

curdoc().add_root(Column(button2, button3, cb))
curdoc().add_periodic_callback(check_status, 1000)

这篇关于Python Bokeh:如何在子例程中更新“切换"按钮(在主菜单中定义)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆