我尝试构建一个 Dash 应用程序的最小示例,它说明了动态生成文件的问题,然后可以通过下载按钮下载该文件。
如果运行此示例,您将看到一个可以输入文本的文本区域。单击“输入”按钮会将文本存储到一个文件中,并为该文件创建一个下载按钮。
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import uuid
stylesheets = [
"https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.2/css/bulma.min.css", # Bulma
]
# create app
app = dash.Dash(
__name__,
external_stylesheets=stylesheets
)
app.layout = html.Div(
className="section",
children=[
dcc.Textarea(
id="text-area",
className="textarea",
placeholder='Enter a value...',
style={'width': '300px'}
),
html.Button(
id="enter-button",
className="button is-large is-outlined",
children=["enter"]
),
html.Div(
id="download-area",
className="block",
children=[]
)
]
)
def build_download_button(uri):
"""Generates a download button for the resource"""
button = html.Form(
action=uri,
method="get",
children=[
html.Button(
className="button",
type="submit",
children=[
"download"
]
)
]
)
return button
@app.callback(
Output("download-area", "children"),
[
Input("enter-button", "n_clicks")
],
[
State("text-area", "value")
]
)
def show_download_button(n_clicks, text):
# turn text area content into file
filename = f"{uuid.uuid1()}.txt"
path = f"downloadable/{filename}"
with open(path, "w") as file:
file.write(text)
uri = path
return [build_download_button(uri)]
if __name__ == '__main__':
app.run_server(debug=True)
但是,生成的 URI 似乎不正确,因为单击按钮只会重定向到索引页面。需要什么才能让它发挥作用?
最佳答案
由于 Dash 是基于 Flask 构建的,因此 Flask 无法找到生成的文本文件的 URI。
解决方案是添加flask路由重定向下载资源, 官方 plotly dash 存储库中有一个简单的示例, https://github.com/plotly/dash-recipes/blob/master/dash-download-file-link-server.py
下面修改后的代码解决了你的问题
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import uuid
import os
import flask
stylesheets = [
"https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.2/css/bulma.min.css", # Bulma
]
# create app
app = dash.Dash(
__name__,
external_stylesheets=stylesheets
)
app.layout = html.Div(
className="section",
children=[
dcc.Textarea(
id="text-area",
className="textarea",
placeholder='Enter a value...',
style={'width': '300px'}
),
html.Button(
id="enter-button",
className="button is-large is-outlined",
children=["enter"]
),
html.Div(
id="download-area",
className="block",
children=[]
)
]
)
def build_download_button(uri):
"""Generates a download button for the resource"""
button = html.Form(
action=uri,
method="get",
children=[
html.Button(
className="button",
type="submit",
children=[
"download"
]
)
]
)
return button
@app.callback(
Output("download-area", "children"),
[
Input("enter-button", "n_clicks")
],
[
State("text-area", "value")
]
)
def show_download_button(n_clicks, text):
if text == None:
return
# turn text area content into file
filename = f"{uuid.uuid1()}.txt"
path = f"downloadable/{filename}"
with open(path, "w") as file:
file.write(text)
uri = path
return [build_download_button(uri)]
@app.server.route('/downloadable/<path:path>')
def serve_static(path):
root_dir = os.getcwd()
return flask.send_from_directory(
os.path.join(root_dir, 'downloadable'), path
)
if __name__ == '__main__':
app.run_server(debug=True)
或者,您可以使用 static
目录而不是 downloadable
目录,它也可以工作。
关于 flask 静态目录的更多信息: http://flask.pocoo.org/docs/1.0/tutorial/static/
这是片段,
#your code
def show_download_button(n_clicks, text):
if text == None:
return
filename = f"{uuid.uuid1()}.txt"
path = f"static/{filename}" # =====> here change the name of the direcotry to point to the static directory
with open(path, "w") as file:
file.write(text)
uri = path
return [build_download_button(uri)]
#your code
关于python - 从 Dash/Flask 应用程序下载动态生成的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54443531/