Specifying the Flask reload watch list (extra_files) in code or the `flask run` command

廉价感情. 提交于 2019-12-11 02:20:47

问题


I have some Flask applications which I run by setting the FLASK_APP environment variable and invoking flask run. Since FLASK_DEBUG is also set, the application conveniently reloads whenever I change some code.

But not all code. There are other files, namely config files that I load with app.config.from_pyfile, that I want the app to watch too, so it reloads if I change those. How can I do that?

I know I can specify extra_files in the built-in Werkzeug server if I invoke it from code. But as I mentioned, I'm actually using the built-in flask run command. I have multiple apps in this project, so being able to choose which one to run with FLASK_APP has proven convenient... except that there doesn't seem to be a way to specify extra_files. I could write some bootstrap code that does both, but I'd prefer to use some built-in way, if it exists.

What would be especially convenient is if I could simply specify the files in the app itself, adding them to a watch list as I load them. Sadly, extra_files doesn't seem to be a member of the App object, even though it's a parameter in app.run().

I can't imagine this being an uncommon use case. Does Flask provide a way to do what I want?


回答1:


I've just tried the following command in manage.py:

@manager.option('-w', '--wsgi_server', dest='server', default='flask',
            help='[flask|gevent|tornado]')
@manager.option('-p', '--port', dest='port', default=5000,
            help='Port to listen')
@manager.option('-d', '--debug', dest='debug', action="store_true", default=False,
            help='Show debugging information')
def run(server, port, debug):
    app.connexion_app.run(
        port=int(port),
        server=server,
        debug=debug,
        extra_files=[
            './proj/oauth2/swagger.yaml',
            './proj/api/swagger.yaml',
        ])

and extra_files seems to be picked up fine:

 * Debugger is active!
 * Debugger PIN: 336-632-033
 * Detected change in '<-snip->/proj/api/swagger.yaml', reloading


来源:https://stackoverflow.com/questions/43688777/specifying-the-flask-reload-watch-list-extra-files-in-code-or-the-flask-run

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!