With the dccDownload
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. dccDownload
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.
Here is an example of downloading content as a string, while showing the raw JSON:
This example has not been ported to R yet - showing the Python version instead.
Visit the old docs site for R at: https://community.plotly.com/c/dash/r/21
from dash import Dash, dcc, html, Input, Output, callback
app = Dash()
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 export methods are supported. Below we are downloading a dataframe as a CSV:
This example has not been ported to R yet - showing the Python version instead.
Visit the old docs site for R at: https://community.plotly.com/c/dash/r/21
from dash import Dash, dcc, html, Input, Output, callback
import pandas as pd
app = Dash()
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:
This example has not been ported to R yet - showing the Python version instead.
Visit the old docs site for R at: https://community.plotly.com/c/dash/r/21
from dash import Dash, dcc, html, Input, Output, callback
import pandas as pd
app = Dash()
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.
This example has not been ported to R yet - showing the Python version instead.
Visit the old docs site for R at: https://community.plotly.com/c/dash/r/21
from dash import Dash, dcc, html, Input, Output, callback
app = Dash()
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)
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
(character; optional):
The ID of this component, used to identify dash components in
callbacks.
data
(named list; optional):
On change, a download is invoked.
data
is a named list with keys:
base64
(logical; optional):
Set to TRUE, when data is base64 encoded.
content
(character; required):
File content.
filename
(character; required):
Suggested filename in the download dialogue.
type
(character; optional):
Blob type, usually a MIME-type.
base64
(logical; default FALSE
):
Default value for base64, used when not set as part of the data
property.
type
(character; default 'text/plain'
):
Default value for type, used when not set as part of the data property.