ButtonClick event not working with RadioButtonGroup

hi!

I am trying to use the bokeh.events.ButtonClick with a RadioButtonGroup to trigger a simple callback, but it doesn’t seem to work.
Am I fairly new to bokeh, so I might be doing something wrong.

This is the code I have tried out:

from bokeh.events import ButtonClick
from bokeh.models import RadioButtonGroup
from bokeh.layouts import row
from bokeh.plotting import curdoc

radio_button = RadioButtonGroup(labels = ["yes","no"], active = 0)

def callback(event):
    print('Click')

radio_button.on_event(ButtonClick, callback)

doc = curdoc()
doc.add_root(row(radio_button)) 

Hi @itg

I think the behavior you observe is intentional; a radio button group is fundamentally different than a basic button.

The bokeh documentation has links to the source code, and in this particular case, you can see that the ButtonClick event does a type-check on the model, exiting if it is not an instance of AbstractButton.

A Button models is an instance of an AbstractButton; a RadioButtonGroup model is not an instance of AbstractButton. See here.

class ButtonClick(ModelEvent):
    ''' Announce a button click event on a Bokeh button widget.

    '''
    event_name = 'button_click'

    def __init__(self, model):
        from .models.widgets import AbstractButton
        if model is not None and not isinstance(model, AbstractButton):
            msg ='{clsname} event only applies to button models'
            raise ValueError(msg.format(clsname=self.__class__.__name__))
        super().__init__(model=model)

Depending on your use case, you might consider an on_change callback associated with the active property of the RadioButtonGroup, which will trigger whenever you select a different button in the group. The signature of the callback includes old and new properties so you know what the attribute - in this case the active button - is changed from and changed to, respectively.

3 Likes