Button Examples and Reference

Button Basic Example

An example of a default button without any extra properties
and n_clicks in the callback. n_clicks is an integer that represents
that number of times the button has been clicked. Note that the original
value is None.

from dash import Dash, dcc, html, Input, Output, State

app = Dash(__name__)

app.layout = html.Div([
    html.Div(dcc.Input(id='input-on-submit', type='text')),
    html.Button('Submit', id='submit-val', n_clicks=0),
    html.Div(id='container-button-basic',
             children='Enter a value and press submit')
])


@app.callback(
    Output('container-button-basic', 'children'),
    Input('submit-val', 'n_clicks'),
    State('input-on-submit', 'value')
)
def update_output(n_clicks, value):
    return 'The input value was "{}" and the button has been clicked {} times'.format(
        value,
        n_clicks
    )


if __name__ == '__main__':
    app.run_server(debug=True)
Enter a value and press submit

Determining which Button Changed with callback_context

This example utilizes the dash.callback_context property
to determine which input was changed.

from dash import Dash, html, Input, Output, callback_context

app = Dash(__name__)

app.layout = html.Div([
    html.Button('Button 1', id='btn-nclicks-1', n_clicks=0),
    html.Button('Button 2', id='btn-nclicks-2', n_clicks=0),
    html.Button('Button 3', id='btn-nclicks-3', n_clicks=0),
    html.Div(id='container-button-timestamp')
])

@app.callback(
    Output('container-button-timestamp', 'children'),
    Input('btn-nclicks-1', 'n_clicks'),
    Input('btn-nclicks-2', 'n_clicks'),
    Input('btn-nclicks-3', 'n_clicks')
)
def displayClick(btn1, btn2, btn3):
    changed_id = [p['prop_id'] for p in callback_context.triggered][0]
    if 'btn-nclicks-1' in changed_id:
        msg = 'Button 1 was most recently clicked'
    elif 'btn-nclicks-2' in changed_id:
        msg = 'Button 2 was most recently clicked'
    elif 'btn-nclicks-3' in changed_id:
        msg = 'Button 3 was most recently clicked'
    else:
        msg = 'None of the buttons have been clicked yet'
    return html.Div(msg)

if __name__ == '__main__':
    app.run_server(debug=True)

Button Properties

Access this documentation in your Python terminal with:
```python

help(dash.html.Button)
```

Our recommended IDE for writing Dash apps is Dash Enterprise’s
Data Science Workspaces,
which has typeahead support for Dash Component Properties.
Find out if your company is using
Dash Enterprise
.

children (list of or a singular dash component, string or number; optional):
The children of this component.

id (string; optional):
The ID of this component, used to identify dash components in
callbacks. The ID needs to be unique across all of the components in
an app.

n_clicks (number; default 0):
An integer that represents the number of times that this element has
been clicked on.

n_clicks_timestamp (number; default -1):
An integer that represents the time (in ms since 1970) at which
n_clicks changed. This can be used to tell which button was changed
most recently.

key (string; optional):
A unique identifier for the component, used to improve performance by
React.js while rendering components See
https://reactjs.org/docs/lists-and-keys.html for more info.

autoFocus (a value equal to: ‘autoFocus’, ‘autofocus’ or ‘AUTOFOCUS’ | boolean; optional):
The element should be automatically focused after the page loaded.

disabled (a value equal to: ‘disabled’ or ‘DISABLED’ | boolean; optional):
Indicates whether the user can interact with the element.

form (string; optional):
Indicates the form that is the owner of the element.

formAction (string; optional):
Indicates the action of the element, overriding the action defined in
the <form>.

formEncType (string; optional):
If the button/input is a submit button (type=”submit”), this attribute
sets the encoding type to use during form submission. If this
attribute is specified, it overrides the enctype attribute of the
button’s form owner.

formMethod (string; optional):
If the button/input is a submit button (type=”submit”), this attribute
sets the submission method to use during form submission (GET, POST,
etc.). If this attribute is specified, it overrides the method
attribute of the button’s form owner.

formNoValidate (a value equal to: ‘formNoValidate’, ‘formnovalidate’ or ‘FORMNOVALIDATE’ | boolean; optional):
If the button/input is a submit button (type=”submit”), this boolean
attribute specifies that the form is not to be validated when it is
submitted. If this attribute is specified, it overrides the novalidate
attribute of the button’s form owner.

formTarget (string; optional):
If the button/input is a submit button (type=”submit”), this attribute
specifies the browsing context (for example, tab, window, or inline
frame) in which to display the response that is received after
submitting the form. If this attribute is specified, it overrides the
target attribute of the button’s form owner.

name (string; optional):
Name of the element. For example used by the server to identify the
fields in form submits.

type (string; optional):
Defines the type of the element.

value (string; optional):
Defines a default value which will be displayed in the element on page
load.

accessKey (string; optional):
Keyboard shortcut to activate or add focus to the element.

className (string; optional):
Often used with CSS to style elements with common properties.

contentEditable (string; optional):
Indicates whether the element’s content is editable.

contextMenu (string; optional):
Defines the ID of a <menu> element which will serve as the element’s
context menu.

dir (string; optional):
Defines the text direction. Allowed values are ltr (Left-To-Right) or
rtl (Right-To-Left).

draggable (string; optional):
Defines whether the element can be dragged.

hidden (a value equal to: ‘hidden’ or ‘HIDDEN’ | boolean; optional):
Prevents rendering of given element, while keeping child elements,
e.g. script elements, active.

lang (string; optional):
Defines the language used in the element.

role (string; optional):
Defines an explicit role for an element for use by assistive
technologies.

spellCheck (string; optional):
Indicates whether spell checking is allowed for the element.

style (dict; optional):
Defines CSS styles which will override styles previously set.

tabIndex (string; optional):
Overrides the browser’s default tab order and follows the one
specified instead.

title (string; optional):
Text to be displayed in a tooltip when hovering over the element.

loading_state (dict; optional):
Object that holds the loading state object coming from dash-renderer.

loading_state is a dict with keys: