With the dcc.Download
component, you can allow users to directly download files from your app. These files include (but are not limited to) spreadsheets, images, text files, etc. dcc.Download
opens a download dialog when the data
property changes.
Note that the following examples make use of the prevent_initial_call
attribute to prevent the callbacks from being triggered when the app inputs are initially rendered. See Advanced Callbacks for more details.
Find a few usage examples below.
For more examples of minimal Dash apps that use dcc.Download
, go to the community-driven Example Index.
Here is an example of downloading content as a string, while showing the raw JSON:
from dash import Dash, dcc, html, Input, Output, callback
app = Dash(__name__)
app.layout = html.Div([
html.Button("Download Text", id="btn-download-txt"),
dcc.Download(id="download-text")
])
@callback(
Output("download-text", "data"),
Input("btn-download-txt", "n_clicks"),
prevent_initial_call=True,
)
def func(n_clicks):
return dict(content="Hello world!", filename="hello.txt")
if __name__ == "__main__":
app.run(debug=True)
For downloading dataframes, the many Pandas export methods are supported. Below we are downloading a dataframe as a CSV:
from dash import Dash, dcc, html, Input, Output, callback
import pandas as pd
app = Dash(__name__)
app.layout = html.Div(
[
html.Button("Download CSV", id="btn_csv"),
dcc.Download(id="download-dataframe-csv"),
]
)
df = pd.DataFrame({"a": [1, 2, 3, 4], "b": [2, 1, 5, 6], "c": ["x", "x", "y", "y"]})
@callback(
Output("download-dataframe-csv", "data"),
Input("btn_csv", "n_clicks"),
prevent_initial_call=True,
)
def func(n_clicks):
return dcc.send_data_frame(df.to_csv, "mydf.csv")
if __name__ == "__main__":
app.run(debug=True)
To download a dataframe as an Excel file with Pandas, add xlsxwriter
or openpyxl
as an app dependency:
from dash import Dash, dcc, html, Input, Output, callback
import pandas as pd
app = Dash(__name__)
app.layout = html.Div([
html.Button("Download Excel", id="btn_xlsx"),
dcc.Download(id="download-dataframe-xlsx"),
])
df = pd.DataFrame({"a": [1, 2, 3, 4], "b": [2, 1, 5, 6], "c": ["x", "x", "y", "y"]})
@callback(
Output("download-dataframe-xlsx", "data"),
Input("btn_xlsx", "n_clicks"),
prevent_initial_call=True,
)
def func(n_clicks):
return dcc.send_data_frame(df.to_excel, "mydf.xlsx", sheet_name="Sheet_name_1")
if __name__ == "__main__":
app.run(debug=True)
To download a file from disk, use dcc.send_file
, taking care to specify the file path.
from dash import Dash, dcc, html, Input, Output, callback
app = Dash(__name__)
app.layout = html.Div([
html.Button("Download Image", id="btn_image"),
dcc.Download(id="download-image")
])
@callback(
Output("download-image", "data"),
Input("btn_image", "n_clicks"),
prevent_initial_call=True,
)
def func(n_clicks):
return dcc.send_file(
"./dash_docs/assets/images/gallery/dash-community-components.png"
)
if __name__ == "__main__":
app.run(debug=True)
Access this documentation in your Python terminal with:
```pythonhelp(dash.dcc.Download)
```
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.
id
(string; optional):
The ID of this component, used to identify dash components in
callbacks.
data
(dict; optional):
On change, a download is invoked.
data
is a dict with keys:
base64
(boolean; optional):
Set to True, when data is base64 encoded.
content
(string; required):
File content.
filename
(string; required):
Suggested filename in the download dialogue.
type
(string; optional):
Blob type, usually a MIME-type.
base64
(boolean; default False
):
Default value for base64, used when not set as part of the data
property.
type
(string; default 'text/plain'
):
Default value for type, used when not set as part of the data property.