How to use pn.bind with Button?

import panel as pn
pn.extension()

def callback(event):
    return button.clicks

button = pn.widgets.Button()
counter = pn.bind(callback, button)
pn.Row(counter, button).servable()

Is this the best way of using pn.bind with Button?

1 Like

Can you explain what you see as the problem with your code example?

If you want the clicks as input you could do this:

import panel as pn
pn.extension()

def callback(event):
    return event

button = pn.widgets.Button()
counter = pn.bind(callback, button.param.clicks)
pn.Row(counter, button).servable()
2 Likes

I would say both examples are fine. To start with pn.bind is the recommended api to use. Probably @Hoxbro example a bit better because it does not use the button inside the callback function. This makes it possible to separate the UI widgets and the functionality.

1 Like

Yes that is exactly what I was looking for; the use of button.param.clicks for the reason that Marc stated.