【发布时间】:2020-09-16 03:58:46
【问题描述】:
我正在查看SO post,但答案无效。
这是我的烧瓶应用程序代码,HTML 中有一个按钮可以浏览 csv 文件。最终在 pandas 中处理 CSV 文件并打印在名为 kW 的列中找到的最大值
from flask import Flask, make_response, request
from werkzeug.utils import secure_filename
import pandas as pd
app = Flask(__name__)
@app.route('/')
def form():
return """
<html>
<body>
<h1>Analysis</h1>
<form action="/transform" method="post" enctype="multipart/form-data">
<input type="file" name="data_file" />
<input type="submit" />
</form>
</body>
</html>
"""
@app.route('/transform', methods=["POST"])
def transform_view():
# data_file is the name of the file upload field
f = request.files['data_file']
# for security - stops a hacker e.g. trying to overwrite system files
filename = secure_filename(f.filename)
# save a copy of the uploaded file
f.save(filename)
# And then use it ...
df = pd.read_csv(filename, index_col='Date', parse_dates=True)
maxy = df.kW.max()
print(maxy)
这是simple.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% for table in tables %}
{{titles[loop.index]}}
{{ table|safe }}
{% endfor %}
</body>
</html>
非常感谢任何提示...当我运行代码(烧瓶工作)时,在浏览过程中选择文件时会引发错误。
TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.
完整的追溯:
File "C:\Users\benb\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\benb\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\benb\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\benb\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Users\benb\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\benb\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py", line 1816, in full_dispatch_request
return self.finalize_request(rv)
File "C:\Users\benb\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py", line 1831, in finalize_request
[Open an interactive python shell in this frame] response = self.make_response(rv)
File "C:\Users\benb\AppData\Local\Continuum\anaconda3\lib\site-packages\flask\app.py", line 1957, in make_response
'The view function did not return a valid response. The'
【问题讨论】: