☰Read-only archive⌕
Looking for help with Bokeh?

This is a read-only archive. Ask questions and join the conversation on GitHub.

Visit Community Support on GitHub →
Archived discussions · Captured October 28, 2026

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

Community Support
Roberto_Gonzalez_San
Roberto_Gonzalez_San

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

⌁   Link to post
Roberto_Gonzalez_San
Roberto_Gonzalez_San

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

⌁   Link to post
Bryan
Bryan

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 robegs@gmail.com 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 bokeh+unsubscribe@continuum.io.

To post to this group, send email to bokeh@continuum.io.

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.

⌁   Link to post

End of archived discussion · Replies are disabled