gpt4 book ai didi

python - 尝试创建 Excel 电子表格以从 Flask 应用程序下载时出现 SystemError

转载 作者:太空宇宙 更新时间:2023-11-04 00:10:19 31 4
gpt4 key购买 nike

我正在尝试创建一个临时 Excel 文件,可通过在 PythonAnywhere 上运行的 Flask (v0.12) Web 应用程序下载,如下所示:

from io import BytesIO
from openpyxl import Workbook
from flask import send_file
from datetime import date

# ... Flask app here

# inside function/route serving Excel file
buffer = BytesIO()
wb = Workbook()
ws = wb.active
ws.title = "Sample"

# populate sample data
ws["A1"] = "some data"
ws["A2"] = str(date.today())

# send file to requestor
wb.save(buffer)
buffer.seek(0)
return send_file(
buffer,
as_attachment=True,
attachment_filename="{}_workbook.xlsx".format(str(date.today())),
mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
)

我遇到了这个异常:

SystemError: <built-in function uwsgi_sendfile> returned a result with an error set

我不知道如何解决这个问题。

完整回溯:

2018-09-10 18:00:43,816: Error running WSGI application
2018-09-10 18:00:43,845: SystemError: <built-in function uwsgi_sendfile> returned a result with an error set
2018-09-10 18:00:43,846: File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1994, in __call__
2018-09-10 18:00:43,846: return self.wsgi_app(environ, start_response)
2018-09-10 18:00:43,846:
2018-09-10 18:00:43,846: File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1985, in wsgi_app
2018-09-10 18:00:43,846: response = self.handle_exception(e)
2018-09-10 18:00:43,847:
2018-09-10 18:00:43,847: File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1540, in handle_exception
2018-09-10 18:00:43,847: reraise(exc_type, exc_value, tb)
2018-09-10 18:00:43,847:
2018-09-10 18:00:43,847: File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 33, in reraise
2018-09-10 18:00:43,847: raise value
2018-09-10 18:00:43,847:
2018-09-10 18:00:43,848: File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1982, in wsgi_app
2018-09-10 18:00:43,848: response = self.full_dispatch_request()
2018-09-10 18:00:43,848:
2018-09-10 18:00:43,848: File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1614, in full_dispatch_request
2018-09-10 18:00:43,848: rv = self.handle_user_exception(e)
2018-09-10 18:00:43,848:
2018-09-10 18:00:43,848: File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1517, in handle_user_exception
2018-09-10 18:00:43,849: reraise(exc_type, exc_value, tb)
2018-09-10 18:00:43,849:
2018-09-10 18:00:43,849: File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 33, in reraise
2018-09-10 18:00:43,849: raise value
2018-09-10 18:00:43,849:
2018-09-10 18:00:43,849: File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1612, in full_dispatch_request
2018-09-10 18:00:43,849: rv = self.dispatch_request()
2018-09-10 18:00:43,850:
2018-09-10 18:00:43,850: File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1598, in dispatch_request
2018-09-10 18:00:43,850: return self.view_functions[rule.endpoint](**req.view_args)
2018-09-10 18:00:43,850:
2018-09-10 18:00:43,850: File "/usr/local/lib/python3.6/dist-packages/flask_login/utils.py", line 228, in decorated_view
2018-09-10 18:00:43,850: return func(*args, **kwargs)
2018-09-10 18:00:43,851:
2018-09-10 18:00:43,851: File "/home/ecwhitetailranch/public_html/app.py", line 1560, in inventory_report
2018-09-10 18:00:43,851: return send_file(
2018-09-10 18:00:43,851:
2018-09-10 18:00:43,851: File "/usr/local/lib/python3.6/dist-packages/flask/helpers.py", line 553, in send_file
2018-09-10 18:00:43,851: data = wrap_file(request.environ, file)
2018-09-10 18:00:43,851:
2018-09-10 18:00:43,852: File "/usr/local/lib/python3.6/dist-packages/werkzeug/wsgi.py", line 726, in wrap_file
2018-09-10 18:00:43,852: return environ.get('wsgi.file_wrapper', FileWrapper)(file, buffer_size)

最佳答案

这里的问题是 PythonAnywhere 的 uwsgi 配置涉及 Python 3.5+ 中的 send_file 文件包装器。 This is a known issue. This issue正是您尝试过的结果。

要解决此问题,您需要将 wsgi-disable-file-wrapper=true 标志添加到 uwsgi.ini 文件(但我没有相信您有权在 PythonAnywhere 上执行此操作)。

关于python - 尝试创建 Excel 电子表格以从 Flask 应用程序下载时出现 SystemError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52713100/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com