Button Examples and Reference

html.Button is a component for rendering a user-selectable button.

Examples

Find a few usage examples below.

For more examples of minimal Dash apps that use html.Button, go to the community-driven Example Index.

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, callback

app = Dash()

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')
])


@callback(
    Output('container-button-basic', 'children'),
    Input('submit-val', 'n_clicks'),
    State('input-on-submit', 'value'),
    prevent_initial_call=True
)
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(debug=True)
Enter a value and press submit

Determining which Button Changed with dash.ctx

This example uses the dash.ctx property
to determine which input changed.

Note: dash.ctx is available in Dash 2.4 and later. dash.callback_context provides similar functionality in earlier
versions of Dash.

from dash import Dash, html, Input, Output, ctx, callback

app = Dash()

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')
])

@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):
    msg = "None of the buttons have been clicked yet"
    if "btn-nclicks-1" == ctx.triggered_id:
        msg = "Button 1 was most recently clicked"
    elif "btn-nclicks-2" == ctx.triggered_id:
        msg = "Button 2 was most recently clicked"
    elif "btn-nclicks-3" == ctx.triggered_id:
        msg = "Button 3 was most recently clicked"
    return html.Div(msg)

if __name__ == '__main__':
    app.run(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.

disable_n_clicks (boolean; optional):
When True, this will disable the n_clicks prop. Use this to remove
event listeners that may interfere with screen readers.

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 (e.g. 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 (e.g. 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 (e.g. 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 (e.g. 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.

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 | number; 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:

  • 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.