✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚾️ It's finally Baseball season! Root for the home team... & Register for our Sports Analytics Webinar!

Using Button n_clicks property to trigger per each click

Currently, I can’t seem to figure out how to get a callback to be triggered per each button click. I tried seeing
if n_clicks is not None:
or
if n_clicks >= 1:

But basically this gets my callback to get repeatedly triggered (I am dealing with data that is being refreshed live)

@app.callback(
    dash.dependencies.Output("submit_div", "children"),
    [dash.dependencies.Input("sensor_selection_dropdown", "value"),dash.dependencies.Input("submit_note_button", "n_clicks")],
    [dash.dependencies.State('sensor_notes', 'value')],
)
def submit_note(sensor_selections,submit_note_button,sensor_notes):
    
    print(sensor_notes,sensor_selections)
    for sensor in sensor_selections:

       if submit_note_button is not None : do something....

Can anyone please advise?

I found the following recent comment :

Is this the definite way to accomplish this?

You can just simply use Input('submit_note_button', 'n_clicks') as input and everything else as State if you want to trigger the callback with button. A simple example from https://dash.plot.ly/dash-core-components :

@app.callback(
    dash.dependencies.Output('output-container-button', 'children'),
    [dash.dependencies.Input('button', 'n_clicks')],
    [dash.dependencies.State('input-box', 'value')])
def update_output(n_clicks, value):
    return 'The input value was "{}" and the button has been clicked {} times'.format(
        value,
        n_clicks
    )

The callback above would only be triggered if you click the button. You can see there’s no need using if n_clicks is not None: or if n_clicks >= 1:

Awesome!
I’ll try that out shortly, but it makes sense to me.
Much appreciated