本文介绍了Python Bokeh 添加基于 Python 对象的警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm doing a Bokeh page were I do some calculations in Python, sometimes the calculations crashes and I want to use the "Alert" browser popup to let the user know that they added some bad data. I used this example and added a Radiobuttongroup where I only want to Alert the user if the RadioButton is set to Make alert. (I'm looking for a general "python solution").

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 回调.请参阅下面的更正代码.如果您想将它作为服务器应用程序运行,只需注释 show(layout) 并取消注释底部的其他两行,然后使用 bokeh serve --show myapp.py 运行它.代码适用于 Bokeh v2.1.1

In your code you were mixing JS callback with Python callback. You cannot pass CustomJS callback to Python callback. See corrected code below.If you want to run it as server app just comment show(layout) and uncomment the other two lines at the bottom and run it with bokeh serve --show myapp.py. Code works for 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 Bokeh 添加基于 Python 对象的警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 04:49