Python 文件使用 python 命令运行但不使用 Flask

Python file runs using python command but not using Flask

我有一个使用 Flask 和 Flask-restx 构建的小型应用程序。我可以 运行 使用 python app.py 的应用程序和 运行 端口 8888 上的烧瓶服务器。我尝试 运行 使用 set FLASK_APP=app.py 和 运行 的文件] 使用 flask run 似乎 运行,但不会打开我的 swagger 页面。请指教

app.py

import logging.config

import os
from flask import Flask, Blueprint
from flask_cors import CORS
from werkzeug.middleware.proxy_fix import ProxyFix
from src.config import default
from src.api.controllers.endpoints.users import ns as users_namespace
from src.api.controllers.endpoints.statuses import ns as status_namespace
from src.api import api
from src.database import db

app = Flask(__name__)
CORS(app)
app.wsgi_app = ProxyFix(app.wsgi_app)
logging_conf_path = os.path.normpath(os.path.join(os.path.dirname(__file__), '../logging.conf'))
logging.config.fileConfig(logging_conf_path)
log = logging.getLogger(__name__)


def configure_app(flask_app):
    flask_app.config['SERVER_NAME'] = default.FLASK_SERVER_NAME
    flask_app.config['SQLALCHEMY_DATABASE_URI'] = default.SQLALCHEMY_DATABASE_URI
    flask_app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = default.SQLALCHEMY_TRACK_MODIFICATIONS
    flask_app.config['SWAGGER_UI_DOC_EXPANSION'] = default.RESTPLUS_SWAGGER_UI_DOC_EXPANSION
    flask_app.config['RESTPLUS_VALIDATE'] = default.RESTPLUS_VALIDATE
    flask_app.config['RESTPLUS_MASK_SWAGGER'] = default.RESTPLUS_MASK_SWAGGER
    flask_app.config['ERROR_404_HELP'] = default.RESTPLUS_ERROR_404_HELP


def initialize_app(flask_app):
    configure_app(flask_app)

    blueprint = Blueprint('CovidAPI', __name__, url_prefix='/')
    api.init_app(blueprint)
    api.add_namespace(users_namespace)
    api.add_namespace(status_namespace)
    flask_app.register_blueprint(blueprint)

    db.init_app(flask_app)


def main():
    initialize_app(app)
    log.info('>>>>> Starting development server at http://{}/ <<<<<'.format(app.config['SERVER_NAME']))
    app.run(debug=default.FLASK_DEBUG)


if __name__ == "__main__":
    main()

烧瓶设置:

# Flask settings
FLASK_SERVER_NAME = 'localhost:8888'
FLASK_DEBUG = True  # Do not use debug mode in production

# Flask-Restplus settings
RESTPLUS_SWAGGER_UI_DOC_EXPANSION = 'list'
RESTPLUS_VALIDATE = True
RESTPLUS_MASK_SWAGGER = False
RESTPLUS_ERROR_404_HELP = False

使用python命令输出:

2020-04-29 10:25:42,519 - __main__ - INFO - >>>>> Starting development server at http://localhost:8888/ <<<<<
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
2020-04-29 10:25:42,610 - werkzeug - INFO -  * Restarting with stat
2020-04-29 10:25:45,398 - __main__ - INFO - >>>>> Starting development server at http://localhost:8888/ <<<<<
2020-04-29 10:25:45,426 - werkzeug - WARNING -  * Debugger is active!
2020-04-29 10:25:45,458 - werkzeug - INFO -  * Debugger PIN: 258-749-652
2020-04-29 10:25:45,530 - werkzeug - INFO -  * Running on http://localhost:8888/ (Press CTRL+C to quit)

运行 通过烧瓶 运行:

 * Serving Flask app "src\app.py" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 313-115-045
 * Running on http://localhost:6000/ (Press CTRL+C to quit)

flask run 命令通过从您的 app.py 模块导入一个 app 对象来工作。

因此在这种情况下永远不会执行此函数中的内容:

def main():
    initialize_app(app)
    log.info('>>>>> Starting development server at http://{}/ <<<<<'.format(app.config['SERVER_NAME']))
    app.run(debug=default.FLASK_DEBUG)

当 运行 是 python app.py 命令时,因为它被执行了:

if __name__ == "__main__":
    main()

我不确定运行在你的情况下如何完全使用 python,因为 configure_appinitialize_app 都没有 return应用程序对象。但是,您可能希望将代码更改为:

# ...

def configure_app(flask_app):
    # ...
    return flask_app

def initialize_app(flask_app):
    # ...
    return flask_app

app = initialize_app(app)

现在(完全配置)app 对象可供 flask run 使用。您不需要设置 FLASK_ENV 环境变量,因为它默认查找名为 app 的对象。

如果您仍希望使用解释器运行,则将此块添加到末尾,尽管如果使用 flask 命令,这有点失效。

if __name__ == "__main__":
    log.info('>>>>> Starting development server at http://{}/ <<<<<'.format(app.config['SERVER_NAME']))
    app.run(debug=default.FLASK_DEBUG)