从 Flask 视图创建和下载 CSV 文件
Create and download a CSV file from a Flask view
我试图让用户下载一个 CSV 文件,其中包含由他们的操作定义的数据。该文件不存在,它是动态创建的。我如何在 Flask 中执行此操作?
使用 csv.writer
and stream the response. Use StringIO 生成数据以写入内存缓冲区而不是生成中间文件。
import csv
from datetime import datetime
from io import StringIO
from flask import Flask
from werkzeug.wrappers import Response
app = Flask(__name__)
# example data, this could come from wherever you are storing logs
log = [
('login', datetime(2015, 1, 10, 5, 30)),
('deposit', datetime(2015, 1, 10, 5, 35)),
('order', datetime(2015, 1, 10, 5, 50)),
('withdraw', datetime(2015, 1, 10, 6, 10)),
('logout', datetime(2015, 1, 10, 6, 15))
]
@app.route('/')
def download_log():
def generate():
data = StringIO()
w = csv.writer(data)
# write header
w.writerow(('action', 'timestamp'))
yield data.getvalue()
data.seek(0)
data.truncate(0)
# write each log item
for item in log:
w.writerow((
item[0],
item[1].isoformat() # format datetime as string
))
yield data.getvalue()
data.seek(0)
data.truncate(0)
# stream the response as the data is generated
response = Response(generate(), mimetype='text/csv')
# add a filename
response.headers.set("Content-Disposition", "attachment", filename="log.csv")
return response
如果generate
函数需要来自当前request
的信息,它应该用stream_with_context
装饰,否则你会得到"working outside request context"错误。其他一切都保持不变。
from flask import stream_with context
@stream_with_context
def generate():
...
Flask-Excel 库使用 PyExcel 生成 CSV 或其他电子表格格式并生成 Flask 响应。文档列出了如何生成其他格式以及可以使用哪些数据的完整 API。
pip install flask-excel
import flask_excel as excel
@app.route('/download', methods=['GET'])
def download_data():
sample_data=[0, 1, 2]
excel.init_excel(app)
extension_type = "csv"
filename = "test123" + "." extension_type
d = {'colName': sample_data}
return excel.make_response_from_dict(d, file_type=extension_type, file_name=filename)
我试图让用户下载一个 CSV 文件,其中包含由他们的操作定义的数据。该文件不存在,它是动态创建的。我如何在 Flask 中执行此操作?
使用 csv.writer
and stream the response. Use StringIO 生成数据以写入内存缓冲区而不是生成中间文件。
import csv
from datetime import datetime
from io import StringIO
from flask import Flask
from werkzeug.wrappers import Response
app = Flask(__name__)
# example data, this could come from wherever you are storing logs
log = [
('login', datetime(2015, 1, 10, 5, 30)),
('deposit', datetime(2015, 1, 10, 5, 35)),
('order', datetime(2015, 1, 10, 5, 50)),
('withdraw', datetime(2015, 1, 10, 6, 10)),
('logout', datetime(2015, 1, 10, 6, 15))
]
@app.route('/')
def download_log():
def generate():
data = StringIO()
w = csv.writer(data)
# write header
w.writerow(('action', 'timestamp'))
yield data.getvalue()
data.seek(0)
data.truncate(0)
# write each log item
for item in log:
w.writerow((
item[0],
item[1].isoformat() # format datetime as string
))
yield data.getvalue()
data.seek(0)
data.truncate(0)
# stream the response as the data is generated
response = Response(generate(), mimetype='text/csv')
# add a filename
response.headers.set("Content-Disposition", "attachment", filename="log.csv")
return response
如果generate
函数需要来自当前request
的信息,它应该用stream_with_context
装饰,否则你会得到"working outside request context"错误。其他一切都保持不变。
from flask import stream_with context
@stream_with_context
def generate():
...
Flask-Excel 库使用 PyExcel 生成 CSV 或其他电子表格格式并生成 Flask 响应。文档列出了如何生成其他格式以及可以使用哪些数据的完整 API。
pip install flask-excel
import flask_excel as excel
@app.route('/download', methods=['GET'])
def download_data():
sample_data=[0, 1, 2]
excel.init_excel(app)
extension_type = "csv"
filename = "test123" + "." extension_type
d = {'colName': sample_data}
return excel.make_response_from_dict(d, file_type=extension_type, file_name=filename)