create an observe handler for multiple ipywidgets

Viewed 562

I have to create a collection of buttons dynamically based on lists of words. I create them with a class. Then I have to dynamically see how many of them are clicked by the user and act accordingly (get the collection of clicked buttons per column).

My problem is how to deal with the event handlers. I can not make my mind around as how to deal collectively with all the buttons created. I basically need a listener that every time a change happens looks how many buttons are clicked and of course click and unclick the corresponding buttons.

This is so far my code:

class verticalButtons():

def __init__(self,concepts):
    self.verticalbuttons = self.create_buttons(concepts)

def create_buttons(self,concepts):
    import ipywidgets
    wd_buttons = []
    for i,concept in enumerate(concepts):
        newbutton = ipywidgets.Button(description=concept,
                                     button_style='success')
        wd_buttons.append(newbutton)

    verticalbox = VBox(wd_buttons)
    return verticalbox

basically passing a list of words to the class creates a VBox with the buttons.

mylist1 = ['1first','1second','1third']
mylist2 = ['2first','2second','2third','2fourth']
one = verticalButtons(mylist1)
two = verticalButtons(mylist2)
button_columns = widgets.HBox([one.verticalbuttons,two.verticalbuttons])
display(button_columns)

the result is as follows: enter image description here

Some idea? I don't even need the code, but just kind of the idea how to deal with it.

thanks.

0 Answers
Related