I just wanted to quickly drop an example here for how to show a spinner once the button has been clicked. Pretty much the same as described here for the dmc.Button()
import dash
from dash import clientside_callback, html, Input, Output, MATCH
import dash_bootstrap_components as dbc
import json
import time
app = dash.Dash(
__name__,
external_stylesheets=[dbc.themes.BOOTSTRAP]
)
app.layout = html.Div(
[
dbc.Button(
id={'type': 'btn', 'index': 0},
children='Load data'
),
html.Div(id={'type': 'container', 'index': 0})
]
)
# callback for showing a spinner within dbc.Button()
clientside_callback(
"""
function (click) {
return [""" + json.dumps(dbc.Spinner(size='sm').to_plotly_json()) + """, " Loading..."]
}
""",
Output({'type': 'btn', 'index': MATCH}, 'children'),
Input({'type': 'btn', 'index': MATCH}, 'n_clicks'),
prevent_initial_call=True
)
# callback for showing a message and setting back the dbc.Button() to the initial state
@app.callback(
Output({'type': 'container', 'index': MATCH}, 'children'),
Output({'type': 'btn', 'index': MATCH}, 'children', allow_duplicate=True),
Input({'type': 'btn', 'index': MATCH}, 'children'),
prevent_initial_call=True
)
def show_message(_):
time.sleep(3)
return 'Data has been loaded', 'Load data'
if __name__ == '__main__':
app.run(debug=True)
@AIMPED@jinnyzor, thanks for the great solutions. Was looking quite a while to achieve this. Your posts gave me inspiration to look some more and combine a feature of the Dash Mantine components (LoadingOverlay), to keep it simple.
By setting the LoadingOverlay and using the parameters:
The desired button loading is displayed as long as the callback takes to execute. My button in this example is dark, adjust the colors to your own needs. Hope it helps someone in their project!