# Click event for button?

**URL:** <https://community.plotly.com/t/click-event-for-button/9118>\
**Category:** Dash Python\
**Created:** [March 21, 2018, 10:03pm UTC](https://community.plotly.com/t/click-event-for-button/9118 "2018-03-21T22:03:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![RagingRoosevelt](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/ragingroosevelt/32/2821_2.png) [@RagingRoosevelt](https://community.plotly.com/u/RagingRoosevelt)\
**Post date:** [March 21, 2018, 10:03pm UTC](https://community.plotly.com/t/click-event-for-button/9118/1 "2018-03-21T22:03:27Z")

</div>

I added a button to my layout. When I try to write a callback for it, I get the following error:

```
dash.exceptions.NonExistantEventException: 
Attempting to assign a callback with
the event "click" but the component
"get_custom_vndr" doesn't have "click" as an event.

Here is a list of the available events in "get_custom_vndr":
[]

```

Here’s how I’m adding it to my layout:

```
app_vndr.layout = html.Div([
    html.Button(
        '+',
        id='get_custom_vndr',
        type='submit'
    )
])

```

and here’s the callback function that’s giving the above error:

```
@app_vndr.callback(
    dash.dependencies.Output('overlay', 'className'),
    events=[dash.dependencies.Event('get_custom_vndr', 'click'),
            dash.dependencies.Event('add_vndr_id_submit', 'click')])
def show_input(b1_n, b2_n):    
    if b1_n>0:
        return ''
    elif b1_n>0:
        return 'hidden'

```

Did I miss something when I added the button to my layout? or when I tried to write the callback?

I got it working for

```
dash.dependencies.Input('get_custom_vndr', 'n_clicks')

```

but I’d like to use two buttons for the same output and with the n\_clicks event, I’d need to try to figure out which button was clicked by comparing the current n\_clicks to the previous n\_clicks for each button, which seems like a pretty hacky way to do it.

---

<div class="post-metadata">

**Author:** ![chriddyp](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/chriddyp/32/16855_2.png) [@chriddyp](https://community.plotly.com/u/chriddyp)\
**Post date:** [March 22, 2018, 4:44am UTC](https://community.plotly.com/t/click-event-for-button/9118/2 "2018-03-22T04:44:31Z")

</div>

> [@RagingRoosevelt](#):
>
> but I’d like to use two buttons for the same output and with the n\_clicks event, I’d need to try to figure out which button was clicked by comparing the current n\_clicks to the previous n\_clicks for each button, which seems like a pretty hacky way to do it.

That is the only way to do it right now, `Event`’s won’t help you here.

---

<div class="post-metadata">

**Author:** ![RagingRoosevelt](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/ragingroosevelt/32/2821_2.png) [@RagingRoosevelt](https://community.plotly.com/u/RagingRoosevelt)\
**Post date:** [March 22, 2018, 3:10pm UTC](https://community.plotly.com/t/click-event-for-button/9118/3 "2018-03-22T15:10:25Z")

</div>

> Event’s won’t help you here

Could you explain that further? Is it something that’s in the pipeline and just isn’t done yet?

---

<div class="post-metadata">

**Author:** ![chriddyp](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/chriddyp/32/16855_2.png) [@chriddyp](https://community.plotly.com/u/chriddyp)\
**Post date:** [March 22, 2018, 4:10pm UTC](https://community.plotly.com/t/click-event-for-button/9118/4 "2018-03-22T16:10:45Z")

</div>

> [@RagingRoosevelt](#):
>
> Could you explain that further? Is it something that’s in the pipeline and just isn’t done yet?

Events just trigger a callback, they don’t tell you which event was fired. Yes, we may improve this in the future.

---

<div class="post-metadata">

**Author:** ![limsanity733](https://avatars.discourse-cdn.com/v4/letter/l/9de053/32.png) [@limsanity733](https://community.plotly.com/u/limsanity733)\
**Post date:** [July 11, 2018, 8:53pm UTC](https://community.plotly.com/t/click-event-for-button/9118/5 "2018-07-11T20:53:25Z")

</div>

I’m having trouble utilizing a html.Button to trigger a callback using the Event function. Can anyone provide a simple reproducible example? Ideally, I would like to use the click button to select certain rows of a dataframe multiple times before downloading it. Thanks.

---

<div class="post-metadata">

**Author:** ![chriddyp](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/chriddyp/32/16855_2.png) [@chriddyp](https://community.plotly.com/u/chriddyp)\
**Post date:** [July 11, 2018, 11:39pm UTC](https://community.plotly.com/t/click-event-for-button/9118/6 "2018-07-11T23:39:00Z")

</div>

For now, use the `n_clicks` property to determine if a button was clicked. See the Button example in the docs: [https://dash.plot.ly/dash-core-components/](https://dash.plot.ly/dash-core-components/)

---

<div class="post-metadata">

**Author:** ![Bachibouzouk](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/bachibouzouk/32/3594_2.png) [@Bachibouzouk](https://community.plotly.com/u/Bachibouzouk)\
**Post date:** [July 14, 2018, 9:24am UTC](https://community.plotly.com/t/click-event-for-button/9118/7 "2018-07-14T09:24:22Z")

</div>

Using a local variable I managed to sort which of the two buttons triggered the callback, I Don’t know what problems it might create having the variable only locally, but at least for one user it works 🙂

```auto
# In[]:
# Import required libraries
import dash
import dash_html_components as html
from dash.dependencies import Input, Output

# Define the app
app = dash.Dash('')
server = app.server
app.config.suppress_callback_exceptions = False
app.scripts.config.serve_locally = True

class DashCallbackVariables:
    """Class to store information useful to callbacks"""

    def __init__ (self):
        self.n_clicks = {1: 0, 2: 0}

    def update_n_clicks(self, nclicks, bt_num):
        self.n_clicks[bt_num] = nclicks

callbacks_vars = DashCallbackVariables()

root_layout = html.Div(
    id='main_page',
    children=[
        html.Div(
                id='btn_name',
                children="No button was clicked"
        ),
        html.Div(
            [
                html.Button(
                    'Button1',
                    id='btn_1',
                    type='submit'
                )
            ]
        ),
        html.Div(
            [
                html.Button(
                    'Button2',
                    id='btn_2',
                    type='submit'
                )
            ]
        )
    ]
)

app.layout = root_layout

@app.callback(
    Output('btn_name', 'children'),
    [
        Input('btn_1', 'n_clicks'),
        Input('btn_2', 'n_clicks'),
    ]
)
def btn_click_callback(nclick_bt1, nclick_bt2):

    # If you have more than one Input, then you need to add this
    # otherwise before the button is clicked its value by default is None
    if nclick_bt1 is None:
        nclick_bt1 = 0
    if nclick_bt2 is None:
        nclick_bt2 = 0

    answer = "No button was clicked"

    if nclick_bt1 != callbacks_vars.n_clicks[1]:
        # It was triggered by a click on the button 1
        callbacks_vars.update_n_clicks(nclick_bt1, 1)

        answer = "Button 1 was clicked"

    if int(nclick_bt2) != callbacks_vars.n_clicks[2]:
        # It was triggered by a click on the button 2
        callbacks_vars.update_n_clicks(nclick_bt2, 2)

        answer = "Button 2 was clicked"

    return answer

# In[]:
# Main
if __name__ == ' __main__':
    app.run_server(debug=True)

```

---

<div class="post-metadata">

**Author:** ![chriddyp](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/chriddyp/32/16855_2.png) [@chriddyp](https://community.plotly.com/u/chriddyp)\
**Post date:** [July 14, 2018, 8:09pm UTC](https://community.plotly.com/t/click-event-for-button/9118/8 "2018-07-14T20:09:03Z")

</div>

This type of local mutable state is not recommended as it is not safe to use across multiple processes or workers. For more information on why this is unsafe, see “Why global variables will break your app”: [https://dash.plot.ly/sharing-data-between-callbacks](https://dash.plot.ly/sharing-data-between-callbacks)

For buttons, use the `n_clicks_timestamp` property and compare those properties across buttons.
