简体   繁体   English

Python Bokeh:如何在子例程中更新“切换”按钮-在主菜单中定义

[英]Python Bokeh: How to update a Toggle button - defined in the main - in a subroutine

I have the following simple bokeh example. 我有以下简单的bokeh示例。 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. 启动按钮在子例程中启动一个不定式的while循环,一旦按下按钮3或取消选中该复选框,该循环应立即停止运行。 Button2 checks the status without the loop which works fine. Button2会检查状态,而无需循环,这会正常工作。 As button3 and the checkbox cb are defined in the main the subroutine called by button1 does not recognize the change. 由于button3和复选框cb在主目录中定义,因此button1调用的子例程无法识别更改。 Is there a way to solve this? 有办法解决吗?

I used bokeh version 1.0.1. 我使用了bokeh版本1.0.1。 You can run the example lokally with bokeh serve script.py and view it in your browser ( http://localhost:5006 ). 您可以使用bokeh serve script.py完整地运行该示例,然后在浏览器( 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))

在此处输入图片说明

I think the while loop interferes the Tornado IO_loop. 我认为while循环会干扰Tornado IO_loop。 I advice you to use add_periodic_callback instead (Bokeh v1.1.0) 我建议您改用add_periodic_callback (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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM