TypeError:视图函数未返回有效响应

Hen*_*Hub 1 python flask pandas

我正在查看SO 帖子,但答案不起作用。

这是我的 Flask 应用程序代码,其中 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)
Run Code Online (Sandbox Code Playgroud)

这是简单的.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>
Run Code Online (Sandbox Code Playgroud)

非常感谢任何提示...当我运行代码(flask 有效)时,在浏览过程中选择文件时会抛出错误。

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'
Run Code Online (Sandbox Code Playgroud)

ngS*_*.py 6

发生这种情况是因为您的路线\transform确实返回了有效的响应。该路由必须有一个 return 语句。我假设你想simple.html在调用此路由时加载,你可以执行以下操作:

  1. 进口render_template
from flask import render_template
Run Code Online (Sandbox Code Playgroud)
  1. 在路由中使用 return 语句,如下所示:
return render_template('simple.html')
Run Code Online (Sandbox Code Playgroud)

或者只是出于虚拟目的,您希望执行此路由而不会出现任何错误,然后您可以像下面一样返回:

return 'Transformed!'
Run Code Online (Sandbox Code Playgroud)

注意:这取决于执行此路由时您实际想要返回的内容或想要呈现的内容。您可以阅读文档。