1

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"
axel_ande
  • 359
  • 1
  • 4
  • 20

1 Answers1

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"
Tony
  • 7,767
  • 2
  • 22
  • 51
  • Sweet, even if can't put a complex python code in it I can work with a flag toggling the Alert message, tanks alot! – axel_ande Jun 17 '21 at 10:29