Michael Scheper
Michael Scheper

Reputation: 7108

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

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?

Upvotes: 5

Views: 5266

Answers (4)

Ben L
Ben L

Reputation: 355

require to set debug=True. Not for production environment.

Upvotes: 0

Jananath Banuka
Jananath Banuka

Reputation: 3953

You can export these variables and specify the file you need.

FLASK_APP=/usr/src/app/server.py

# to enable debug to enable reload on file change
FLASK_DEBUG=1

# here specify the file
FLASK_RUN_EXTRA_FILES="/usr/src/app/banuka.txt"

I have tried this and it is working as expected. But this doesn't guarantee the browser will reload, being said that you have to do the browser refresh manually.

Upvotes: 1

Amnon
Amnon

Reputation: 2320

You could also use FLASK_RUN_EXTRA_FILES environment variable. See https://flask.palletsprojects.com/en/1.1.x/cli/#watch-extra-files-with-the-reloader

Upvotes: 0

sas
sas

Reputation: 7552

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

Upvotes: 6

Related Questions