dcc.Download

The dcc.Download component allows files to be directly downloaded from your app. These files include, but are not limited to, spreadsheets, images and text files , etc. The component 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.

Downloading Content as Strings

Here is an example show how to download content as a string, while showing the raw JSON:

import dash
from dash.dependencies import Output, Input
import dash_html_components as html
import dash_core_components as dcc

app = dash.Dash(__name__)
app.layout = html.Div([
    html.Button("Download Text", id="btn_txt"),
    dcc.Download(id="download-text")
])


@app.callback(
    Output("download-text", "data"),
    Input("btn_txt", "n_clicks"),
    prevent_initial_call=True,
)
def func(n_clicks):
    return dict(content="Hello world!", filename="hello.txt")


if __name__ == "__main__":
    app.run_server(debug=True)

Download Dataframe as CSV file

For downloading dataframes, the many pandas export methods are supported. Below we are downloading a dataframe as a CSV:

import dash
from dash.dependencies import Output, Input
import dash_html_components as html
import dash_core_components as dcc
import pandas as pd

app = dash.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"]})


@app.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_server(debug=True)

Download Dataframe as Excel file

To download a dataframe as an Excel file with pandas, add xlsxwriter or openpyxl as an app dependency:

import dash
from dash.dependencies import Output, Input
import dash_html_components as html
import dash_core_components as dcc
import pandas as pd

app = dash.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"]})


@app.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_server(debug=True)

Downloading Images

To download a file from disk use dcc.send_file, taking care to specify the file path.

import dash
from dash.dependencies import Output, Input
import dash_html_components as html
import dash_core_components as dcc

app = dash.Dash(__name__)
app.layout = html.Div([
    html.Button("Download Image", id="btn_image"),
    dcc.Download(id="download-image")
])


@app.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_server(debug=True)

Download Properties

Access this documentation in your Python terminal with:
```python

help(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.

base64 (boolean; default False):
Default value for base64, used when not set as part of the data
property.

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.

type (string; default 'text/plain'):
Default value for type, used when not set as part of the data
property.