Downloader
ClassComponentSource
comp = Downloader(
id: str | Mapping[str, Any],
url: str | DownloaderURL,
headers: Mapping[str, str],
allow_cross_origin: bool,
status: DownloaderStatus,
mitm: str | None,
loading_state: LoadingState
)
Downloader
is a React component based on StreamSaver
.
The StreamSaver.js
project provides a customizable way to access
and download an online stream. This is the recommended downloader for practical uses.
It has the optimized performance for triggering multiple downloading events.
Aliases
This type can be acquired by
import dash_file_cache as dfc
dfc.Downloader
dfc.components.Downloader
Component Properties
- Descriptions
- Properties
Name | Type | |
---|---|---|
id | str | Mapping[str, Any] | The ID of the component. A dictionary-id is used for creating a pattern-matching callback. |
url | str | DownloaderURL | The URL used to access the data to be downloaded. Each time when this value is set, a download event will be triggered. After triggering the download event, this value will be reset by a blank string. |
headers | Mapping[str, str] | The extra headers to be used when submitting the request of the downloading event. This property may need to be configured when the downloading event needs to add authentication information. |
allow_cross_origin | bool | A flag determining whether the cross-origin downloading link can be used. If the data to be downloaded is from a cross-domain site, need to configure this value as |
status | DownloaderStatus | The status code when a downloading event is finalized. If multiple downloading events are triggered by the same downloader, the later event will overwrite the status from the former events. |
mitm | str | None | The MITM-IFrame used for maintaining the status of the downloader. It prevents the downloader to be closed when the broswer is idle. See details here. If not specified, will use the default MITM service of StreamSaver.js , which needs the Internet. |
loading_state | LoadingState | The loading state set by Dash. This value should not be used by users. |
Name | Type | Interactive | Setable | Required | Default |
---|---|---|---|---|---|
id | str | Mapping[str, Any] | undefined | |||
url | str | DownloaderURL | "" | |||
headers | Mapping[str, str] | undefined | |||
allow_cross_origin | bool | False | |||
status | DownloaderStatus | undefined | |||
mitm | str | None | undefined | |||
loading_state | LoadingState | undefined |
- "Interactive" means that the property can be changed by users' interactions, thus firing a callback.
- "Setable" means that changing it by callback
Output(...)
will take effects. - "Required" means that the component cannot be initialized if this value is not explicitly provided.
Examples
Minimal example
- Codes
- Results
from typing import Optional
import dash
import flask
from dash import html, Output, Input
from dash_file_cache.services.utilities import get_server
from dash_file_cache.components import Downloader
from dash_file_cache.services import ServiceDownloader
app = dash.Dash("demo")
ServiceDownloader("/dfc-downloader").serve(app)
server = get_server(app)
app.layout = html.Div(
(
html.Button(id="btn", children="Download"),
Downloader(id="downloader", mitm="/dfc-downloader"),
)
)
@server.route("/file")
def file():
resp = flask.Response("test file content", status=200, mimetype="text/plain")
resp.headers["Content-Disposition"] = "attachment; filename=test.txt"
return resp
@app.callback(Output("downloader", "url"), Input("btn", "n_clicks"))
def trigger_get_file(n_clicks: Optional[int]):
if not n_clicks:
return dash.no_update
return "/file"
if __name__ == "__main__":
app.run()
An application with a downloading button will be rendered.
By clicking the button, a file named text.txt
will be downloaded, the file content is
test file content
Download a file with a background callback
See