使用 Flask 返回 404 错误下载 csv
Download csv with Flask returning 404 error
当我点击下载按钮时,我得到了
404 not found The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
我正在使用 flask 下载动态创建的 CSV 文件
请看下面我的代码
filename = f"{CSV}"
path = f"downloadable/{filename}"
data = [titles, prices, sizes, sold]
data = [list(map(lambda element: element.text, arr)) for arr in data]
with open(CSV, 'w') as file:
writer = csv.writer(file)
j = 0
while j < len(titles):
row = []
for i in range(len(data)):
row.append(data[i][j])
writer.writerow(row)
j += 1
uri = path
#df = pd.read_csv(CSV, names=['Title', 'Sold Price', 'Size', 'Sold Date'])
#df = df.to_csv(CSV)
return [build_download_button(uri)]
else:
raise dash.exceptions.PreventUpdate
@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(debug=True)
app.run_server(host=os.getenv('IP', '0.0.0.0'),
port=int(os.getenv('PORT', 1112)))
您是否考虑过使用 Download
component?语法更简单,它支持更大的文件。这是文档中的一个小示例,它从磁盘下载文件,
import dash
import dash_html_components as html
from dash.dependencies import Output, Input
from dash_extensions import Download
from dash_extensions.snippets import send_file
app = dash.Dash(prevent_initial_callbacks=True)
app.layout = html.Div([html.Button("Download", id="btn"), Download(id="download")])
@app.callback(Output("download", "data"), [Input("btn", "n_clicks")])
def func(n_clicks):
return send_file("/home/emher/Documents/Untitled.png")
if __name__ == '__main__':
app.run_server()
如果您愿意,也可以下载数据。
当我点击下载按钮时,我得到了
404 not found The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
我正在使用 flask 下载动态创建的 CSV 文件
请看下面我的代码
filename = f"{CSV}"
path = f"downloadable/{filename}"
data = [titles, prices, sizes, sold]
data = [list(map(lambda element: element.text, arr)) for arr in data]
with open(CSV, 'w') as file:
writer = csv.writer(file)
j = 0
while j < len(titles):
row = []
for i in range(len(data)):
row.append(data[i][j])
writer.writerow(row)
j += 1
uri = path
#df = pd.read_csv(CSV, names=['Title', 'Sold Price', 'Size', 'Sold Date'])
#df = df.to_csv(CSV)
return [build_download_button(uri)]
else:
raise dash.exceptions.PreventUpdate
@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(debug=True)
app.run_server(host=os.getenv('IP', '0.0.0.0'),
port=int(os.getenv('PORT', 1112)))
您是否考虑过使用 Download
component?语法更简单,它支持更大的文件。这是文档中的一个小示例,它从磁盘下载文件,
import dash
import dash_html_components as html
from dash.dependencies import Output, Input
from dash_extensions import Download
from dash_extensions.snippets import send_file
app = dash.Dash(prevent_initial_callbacks=True)
app.layout = html.Div([html.Button("Download", id="btn"), Download(id="download")])
@app.callback(Output("download", "data"), [Input("btn", "n_clicks")])
def func(n_clicks):
return send_file("/home/emher/Documents/Untitled.png")
if __name__ == '__main__':
app.run_server()
如果您愿意,也可以下载数据