Skip to main content
Version: 0.2.0

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

NameType
Description
idstr | Mapping[str, Any]The ID of the component. A dictionary-id is used for creating a pattern-matching callback.
urlstr | 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.

headersMapping[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_originbool

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 True while the remote site needs to configure the headers Access-Control-Allow-Origin.

statusDownloaderStatus

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.

mitmstr | NoneThe 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_stateLoadingStateThe loading state set by Dash. This value should not be used by users.

Examples

Minimal example

minimal_example_downloader.py
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()

Download a file with a background callback

See

Download file