Quantcast
Channel: 🎛️ Dash - Plotly Community Forum
Viewing all articles
Browse latest Browse all 6271

Remember n_clicks of button

$
0
0

@bebuck wrote:

I’m trying to update one target with multiple buttons and need a way to figure out which button was fired when a callback gets activated, and preferably would not want the callback to run continuously. This code works in debug but from my overview of Dash it’s my understanding it will break in Production. What’s the right way to accomplish this? Thanks.

import dash
import dash_core_components as dcc
import dash_html_components as html


# In[3]:


app = dash.Dash(__name__)
app.layout = html.Div([
    html.Div(["No Button"], id="innerupdate"),
    html.Div(html.Button("Firstbutton", id="firstbutton")),
    html.Div(html.Button("Secondbutton", id="secondbutton")),
    html.Div(html.Button("Thirdbutton", id="thirdbutton"))]
)

nfirst = 0
nsecond = 0
nthird = 0

@app.callback(
    dash.dependencies.Output("innerupdate", "children"),
    [dash.dependencies.Input("firstbutton", "n_clicks"),
     dash.dependencies.Input("secondbutton", "n_clicks"),
     dash.dependencies.Input("thirdbutton", "n_clicks")]
)
def fbup(nfclicks, nsclicks, ntclicks):
    global nfirst
    global nsecond
    global nthird
    if nfclicks is not None and nfclicks > nfirst:
        nfirst = nfclicks
        return "First Button"
    if nsclicks is not None and nsclicks > nsecond:
        nsecond = nsclicks
        return "Second Button"
    if ntclicks is not None and ntclicks > nthird:
        nthird = ntclicks
        return "Third Button"
    return "No Button"

app.run_server(debug=True)

Posts: 2

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 6271

Latest Images

Trending Articles



Latest Images