Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Bokeh基于Python对象添加警报_Python_Alert_Bokeh - Fatal编程技术网

Python Bokeh基于Python对象添加警报

Python Bokeh基于Python对象添加警报,python,alert,bokeh,Python,Alert,Bokeh,我正在做一个Bokeh页面,如果我用Python进行一些计算,有时计算会崩溃,我想使用警报浏览器弹出窗口让用户知道他们添加了一些错误数据。我使用并添加了一个Radiobuttongroup,我只想在RadioButton设置为发出警报时提醒用户。我正在寻找一个通用的python解决方案 from bokeh.io import curdoc from bokeh.models.widgets import Button, RadioButtonGroup from bokeh.layouts i

我正在做一个Bokeh页面,如果我用Python进行一些计算,有时计算会崩溃,我想使用警报浏览器弹出窗口让用户知道他们添加了一些错误数据。我使用并添加了一个Radiobuttongroup,我只想在RadioButton设置为发出警报时提醒用户。我正在寻找一个通用的python解决方案

from bokeh.io import curdoc
from bokeh.models.widgets import Button, RadioButtonGroup
from bokeh.layouts import column
from bokeh.models.callbacks import CustomJS

button_classify = Button(label="Create Pop-up alert")
callback = CustomJS(args={}, code='alert("hello!");')
callback2 = CustomJS(args={}, code='')
button_group = RadioButtonGroup(labels=['Make alert', 'Dont make an alert'],
                                active=0)

def determine_button() -> CustomJS:
    if button_group.active == 0:
        return callback
    else:
        return callback2

button_classify.js_on_click(determine_button)
layout = column(button_group,
                button_classify)
curdoc().add_root(layout)
curdoc().title = "Pop-up Alert"

在代码中,您混合了JS回调和Python回调。不能将CustomJS回调传递给Python回调。请参阅下面的更正代码。 如果您想以服务器应用程序的形式运行它,只需注释showlayout并取消底部其他两行的注释,然后使用bokeh serve-show myapp.py运行它。代码适用于Bokeh v2.1.1

from bokeh.io import curdoc, show
from bokeh.models.widgets import Button, RadioButtonGroup
from bokeh.layouts import column
from bokeh.models.callbacks import CustomJS

button_classify = Button(label="Create Pop-up alert")
button_group = RadioButtonGroup(labels=['Alert ON', 'Alert OFF'], active=0)

code = 'if (button_group.active == 0) { alert("ALERT !"); }'
button_classify.js_on_click(CustomJS(args={'button_group': button_group}, code=code))
layout = column(button_group, button_classify)

show(layout)

# curdoc().add_root(layout)
# curdoc().title = "Pop-up Alert"

好极了,即使我不能在里面放一个复杂的python代码,我也可以用一个标记来切换警报消息,很多!