Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Download component #926

Merged
merged 26 commits into from
Mar 4, 2021
Merged

Download component #926

merged 26 commits into from
Mar 4, 2021

Conversation

alexcjohnson
Copy link
Collaborator

Updated version of #863 - Thanks @emilhe!
Closes #216

The syntax we landed on there, starting with this setup:

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

app = dash.Dash(prevent_initial_callbacks=True)
app.layout = html.Div([html.Button("Download", id="btn"), dcc.Download(id="download")])

Contents as a plain string, showing the raw JSON required:

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

And the utility functions are now on the main dcc namespace.

To send a file from disk:

@app.callback(Output("download", "data"), Input("btn", "n_clicks"))
def func(n_clicks):
    return dcc.send_file("./tests/integration/download/download-assets/Lenna.jpeg")

And data frames, supporting many of the pandas export methods:

import pandas as pd
df = pd.DataFrame({"a": [1, 2, 3, 4], "b": [2, 1, 5, 6], "c": ["x", "x", "y", "y"]})

@app.callback(Output("download", "data"), Input("btn", "n_clicks"))
def func(n_clicks):
    return dcc.send_data_frame(df.to_csv, "mydf.csv")

emilhe and others added 20 commits September 12, 2020 18:04
* Reverting test files from black 20.8b1 to 19.10b0 (for compatibility)
* Moving express.py to dash_core_components_base + added import in __import__ (+ updated tests accordingly)
move up top, and include utility functions
v3 seems to fail on py3.6 on CI, but v2 worked earlier
@emilhe
Copy link
Contributor

emilhe commented Feb 6, 2021

Looks good! I think it makes perfect sense to include the utility functions in the dcc namespace, it yields a more coherent experience working with the download component.

@alexcjohnson
Copy link
Collaborator Author

Still some investigation needed into why the Py2.7 and Py3.7 tests are failing repeatably even though Py3.6 works fine (after downgrading to pyarrow v2, the brand new v3 wouldn't install on Py3.6 on CI). Once we sort that out we can merge this.

@alexcjohnson alexcjohnson mentioned this pull request Feb 7, 2021
@alexcjohnson alexcjohnson merged commit 495097b into dev Mar 4, 2021
@alexcjohnson alexcjohnson deleted the DownloadComponent branch March 4, 2021 04:46
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Official download component
2 participants