Hi!
I am currently developing an application, in which I have a button that triggers dcc.Download to export some data into an excel sheet. Since I want the user to select where the file gets downloaded, I return dcc.send_data_frame to the dcc.Download, in order to have a “Save As” dialog.
However, I would want to print out different text depending on which button was clicked in the dialog (Save or Cancel). Is it possible to catch the event of these buttons? So far I couldn’t manage to do it, and the text already appears when clicking the “Download” button, it won’t even wait until Save is clicked on.
Example application to demonstrate the behaviour:
from collections import OrderedDict
import dash
import dash_table
import pandas as pd
import dash_html_components as html
import dash_bootstrap_components as dbc
from dash import dcc
from dash.dependencies import Output, Input, State
data = OrderedDict(
[
("Date", ["2015-01-01", "2015-10-24", "2016-05-10", "2017-01-10", "2018-05-10", "2018-08-15"]),
("Region", ["Montreal", "Toronto", "New York City", "Miami", "San Francisco", "London"]),
("Temperature", [1, -20, 3.512, 4, 10423, -441.2]),
("Humidity", [10, 20, 30, 40, 50, 60]),
("Pressure", [2, 10924, 3912, -10, 3591.2, 15]),
]
)
df = pd.DataFrame(data)
app = dash.Dash(prevent_initial_callbacks=True, external_stylesheets=[dbc.themes.BOOTSTRAP])
app.layout = html.Div(
children=[
html.Div(
dash_table.DataTable(
data=df.to_dict('records'),
columns=[{'id': c, 'name': c} for c in df.columns]
)
),
html.Button("Download", id="btn"),
html.Div(
dbc.Modal(
[
dbc.ModalHeader(dbc.ModalTitle(id='modal-title')),
dbc.ModalBody(id='modal-body'),
dbc.ModalFooter(
dbc.Button(
"Close", id="close", className="ms-auto", n_clicks=0
)
),
],
id="success-modal",
is_open=False,
),
),
dcc.Download(id="download")
]
)
@app.callback(
Output("download", "data"),
Output('modal-title', 'children'),
Output("modal-body", "children"),
[Input("btn", "n_clicks")],
prevent_initial_call=True
)
def excel_download(n_clicks):
print("excel download callback invoked")
return dcc.send_data_frame(df.to_excel, "mydf.xls"), "Export", "Export Successful!"
@app.callback(
Output("success-modal", "is_open"),
[Input("btn", "n_clicks"), Input("close", "n_clicks")],
[State("success-modal", "is_open")],
)
def toggle_modal(n1, n2, is_open):
if n1 or n2:
return not is_open
return is_open
if __name__ == '__main__':
app.run_server()
Notice how the modal appears right after clicking on the export button, and won’t wait for the choice of the dialog.
Thanks in advance!