Skip to main content

Downloader

ClassSource

downloader = Downloader(
id: str
to_addr: Callable[[str], str] | None
)

Purely Python-Dash implemented downloader component.

This component is purely implemented by Python codes.

Aliases

This class can be acquired by

import dash_file_cache as dfc


dfc.Downloader
dfc.components.Downloader
dfc.components.downloader.Downloader

Arguments

ArgumentTypeRequired
Description
idstrThe basic ID of this component.
to_addrCallable[[str], str] | None

An optional pre-processing function which converts the trigger from the callback to the address used for downloading the file.

If not provided, will use the trigger value directly (equivalent to lambda x: x).

If returning an empty string, the downloading event will be prevented.

Methods

layout

layout: html.Div = downloader.layout()

Get the layout of this component.

Returns

ArgumentType
Description
layouthtml.DivThe layout of this downloader.

use_callbacks

downloader.use_callbacks(app: dash.Dash)

Use callbacks.

Bind the automatically generated callbacks used by this components to the given app.

Requires

ArgumentTypeRequired
Description
appDashThe dash application.

Properties

as_input

cbk_input: dash.Input = downloader.as_input

Provide the trigger as the callback input. This input should be triggered when the address of the downloadable file is prepared.


as_output

cbk_output: dash.Output = downloader.as_output

Provide the trigger as the callback output.


as_state

cbk_state: dash.State = downloader.as_state

Provide the trigger as the callback state. This input should be triggered when the address of the downloadable file is prepared.

Examples

Minimal example

minimal_example_downloader.py
from typing import Optional
import dash
import flask
from dash import html, Input
from dash_file_cache.services.utilities import get_server
from dash_file_cache.components.downloader import Downloader


app = dash.Dash("demo")
server = get_server(app)

downloader = Downloader(id="downloader", to_addr=None)

app.layout = html.Div((html.Button(id="btn", children="Download"), downloader.layout()))

downloader.use_callbacks(app)


@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(downloader.as_output, 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