dcc_download

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.

Examples

Find a few usage examples below.

Downloading Content as Strings

Here is an example of downloading content as a string, while showing the raw JSON:

using Dash

app = dash(prevent_initial_callbacks=true)

app.layout = html_div(
    [
      html_button("Download Text", id="btn_txt1"), 
      dcc_download(id="download-text-index1")
    ]
)

callback!(
  app, Output("download-text-index1", "data"), 
  Input("btn_txt1", "n_clicks")) do n_clicks
    if n_clicks isa Nothing
      throw(PreventUpdate())
    else
      return (content = "Hello world!", filename="hello.txt")
    end
end

run_server(app, "0.0.0.0", debug=true)

Downloading a Dataframe as a CSV file

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

using Dash
using DataFrames, CSV

app = dash(prevent_initial_callbacks=true)

app.layout = html_div(
    [
      html_button("Download CSV", id="btn_csv"), 
      dcc_download(id="download-dataframe-csv")
    ]
)

df = DataFrame(A = [1, 2, 3, 4], B = [2, 1, 5, 6], C = ["x", "x", "y", "y"])

callback!(
  app, Output("download-dataframe-csv", "data"), 
  Input("btn_csv", "n_clicks")) do n_clicks
    if n_clicks isa Nothing
      throw(PreventUpdate())
    else
      return download(CSV.write("mydf.csv", df))
    end
end

run_server(app, "0.0.0.0", debug=true)

Downloading a Dataframe as an Excel file

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

This example has not been ported to Julia yet - showing the Python version instead.

Visit the old docs site for Julia at: https://community.plotly.com/c/dash/julia/20

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)

Downloading Images

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

This example has not been ported to Julia yet - showing the Python version instead.

Visit the old docs site for Julia at: https://community.plotly.com/c/dash/julia/20

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)

Download Properties

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 (Bool; 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 (Bool; 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.