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.

import dash
import dash_html_components as html
import dash_core_components as dcc

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

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(
    dash.dependencies.Output('container-button-basic', 'children'),
    [dash.dependencies.Input('submit-val', 'n_clicks')],
    [dash.dependencies.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.

# -*- coding: utf-8 -*-
import dash
from dash.dependencies import Input, Output
import dash_html_components as html

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

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 dash.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_components.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 (a 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.

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

aria-* (string; optional):
A wildcard aria attribute.

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

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.

data-* (string; optional):
A wildcard data attribute.

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

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

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

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’, ‘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.

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

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.

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

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

loading_state is a dict with keys:

  • component_name (string; optional):
    Holds the name of the component that is loading.

  • is_loading (boolean; optional):
    Determines if the component is loading or not.

  • prop_name (string; optional):
    Holds which property is loading.

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.

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

role (string; optional):
The ARIA role attribute.

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.

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.