How to know the clicked button when we have multiple buttons.

Hi all,

I have a figure that should be modified depending on the clicked button.

I’m generating the buttons like this:

def update(value):
print "pushed button ", value

for i, data in enumerate(personas_data):

button_persona = Button(label=data["title"], width = 210)

button_persona.on_click(lambda: update(i))    

``

But, when I click a button, it always receive the index of the last one. So, I cannot know the button the user is clicking.
Is there an easy way to do this?

Thanks

Just in case someone have my same problem, I solved it in a really non elegant way, but it works:

def update():
for i in xrange(len(buttons)):
if (buttonsStatus[i] < buttons[i].clicks): #Checking the button that has changed
print "pushed button ", i
buttonsStatus[i] = buttons[i].clicks
break;

buttons =

for i, data in enumerate(personas_data):

button_persona = Button(label=data["title"], width = 210, clicks = 0)

 button_persona.on_click(update)

buttons.append(button_persona)

buttonsStatus = np.zeros((len(buttons), 1))

``

···

El viernes, 23 de septiembre de 2016, 12:32:35 (UTC+2), Roberto González Sánchez escribió:

Hi all,

I have a figure that should be modified depending on the clicked button.

I’m generating the buttons like this:

def update(value):
print "pushed button ", value

for i, data in enumerate(personas_data):

button_persona = Button(label=data["title"], width = 210)
button_persona.on_click(lambda: update(i))    

``

But, when I click a button, it always receive the index of the last one. So, I cannot know the button the user is clicking.
Is there an easy way to do this?

Thanks

This actually has nothing to do with Bokeh, it’s a particular example of scoping behavior in Python that is often surprising to people. See

http://stackoverflow.com/questions/1841268/generating-functions-inside-loop-with-lambda-expression-in-python

For more details and a solution.

Thanks,

Bryan

···

On Sep 23, 2016, at 05:32, Roberto González Sánchez [email protected] wrote:

Hi all,

I have a figure that should be modified depending on the clicked button.

I’m generating the buttons like this:

def update(value):
print "pushed button ", value

for i, data in enumerate(personas_data):

button_persona = Button(label=data["title"], width = 210)
button_persona.on_click(lambda: update(i))    

``

But, when I click a button, it always receive the index of the last one. So, I cannot know the button the user is clicking.
Is there an easy way to do this?

Thanks

You received this message because you are subscribed to the Google Groups “Bokeh Discussion - Public” group.

To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

To post to this group, send email to [email protected].

To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/ded5475d-68bc-4ad0-89d7-80556578e014%40continuum.io.

For more options, visit https://groups.google.com/a/continuum.io/d/optout.