Flask AttributeError: 'Blueprint' object has no attribute 'response_class'

Flask AttributeError: 'Blueprint' object has no attribute 'response_class'

我正在尝试让我的 Flask 路由 return 从日志逐行输出到我的 AJAX 调用的文本流,如下所示:

my_app/auth/views.py:

@bp.route('/output_stream', methods=['GET', 'POST'])
def output_stream():
    def generate():
        if request.method == "POST":
            ...
            ...
            for line in iter(lambda: stdout.readline(2048), ""):
                data_buffer += line
                print(line, end="")
                yield line + '\n'
                if re.search(r'Done', line):
                    print('No more data')
                    break

            print('finished.')

            client.close()

    return bp.response_class(generate(), mimetype='text/plain')

我正在 __init__.py 中注册我的 Flask 应用,如下所示:

from flask import Flask

...
app = Flask(__name__)

...
...

from my_app.auth.views import bp
app.register_blueprint(bp)

但是,由于某种原因,它一直抛出 Flask AttributeError: 'Blueprint' object has no attribute 'response_class'

我的 Flask 是最新的:

# pip install --upgrade Flask
Requirement already up-to-date: Flask in /usr/lib/python3.5/site-packages (1.0.2)

有谁知道这可能是什么问题?

而不是:

return bp.response_class(generate(), mimetype='text/plain')

你可能想要:

from flask import Response, stream_with_context
# in output_stream:
return Response(stream_with_context(generate()), mimetype='text/plain')

stream_with_context 是必需的,因为您在生成器内部访问 request