跳到主要内容
版本:0.2.0

PlainDownloader

组件源码

comp = PlainDownloader(
id: str | Mapping[str, Any],
url: str,
loading_state: LoadingState
)

PlainDownloader是简单、原生的React组件。

该组件基于一个临时创建的、指向指定URL的魔术链接实现。

由于PlainDownloader的实现简单地基于HTML标签<a>,故而无法为其配置请求头或者验证信息。

别名

该类可以按以下方式之一获取

import dash_file_cache as dfc


dfc.PlainDownloader
dfc.components.PlainDownloader

组件属性

名称类型
说明
idstr | Mapping[str, Any]组件ID。若设置为字典,则该ID用于模式匹配 callback
urlstr

用来访问要下载数据的URL。

每当设置该值时,就会触发一次下载事件。触发事件后,该值会重置为空字符串。

loading_stateLoadingStateDash的加载状态字典。用户不应手动设置该值。

范例

最小范例

minimal_example_plain_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 PlainDownloader


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

app.layout = html.Div(
(html.Button(id="btn", children="Download"), PlainDownloader(id="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()