简体   繁体   中英

How does Flask process the “flask run” cli command?

I've been trying to understand how it's possible to launch a flask project by running flask run .

What actually happens behind the scenes? How is it possible to actually launch the app using the flask keyword? I got as far as understanding that it is based on the Click library ( https://palletsprojects.com/p/click/ ) but I still don't understand what happens step by step (the internals).

If someone could explain that would be appreciated. Thank you!

To launch a flask application, you can use the command flask run . But how does it work?

Before running any flask application, flask needs to be told how to import it by setting certain environment variables. On your terminal, you run these commands in their order:

(venv) $ export FLASK_APP=app.py
(venv) $ flask run

What is app.py ? This is the entry point of your application. In this file, you probably have:

from app import app

# Here, you application instance is being imported. 
# The application instance is where you defined your flask app.

Alternatively, this file may have:

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True)

After the server initializes it will wait for client connections. The output from flask run indicates that the server is running on IP address 127.0.0.1 , which is always the address of your own computer. This address is so common that is also has a simpler name that you may have seen before: localhost .

Applications deployed on production web servers typically listen on port 443, or sometimes 80 if they do not implement encryption, but access to these ports require administration rights. Since this application is running in a development environment, Flask uses the freely available port 5000.

To access you application on your web browser, paste this URL:

 http://localhost:5000/

There are other environment variables that flask can use:

  • FLASK_ENV (sets your environment, either to development or production)
  • FLASK_DEBUG (enables/disables debugging)

Before running flask run , you will run need to add the other environment variables in your terminal:

(venv) $ export FLASK_APP=app.py
(venv) $ export FLASK_ENV=development
(venv) $ export FLASK_DEBUG=True
(venv) $ flask run

Now, you will have specified that your application is running on a development server, and you have enabled flask debugging features.

Every time you want to see the changes in your application, you will need to restart your server by running the same commands in your terminal.

Starting with version 1.0, Flask allows you to register environment variables that you want to be automatically imported when you run the flask command.

It is recommended that you store flask environment variables (these are the ones needed to run your application) in a file called .flaskenv in your project's root directory.

(venv) $ touch .flaskenv

Then update this file with your environment variables:

# .flaskenv

FLASK_APP=app.py
FLASK_ENV=development
FLASK_DEBUG=True

To implement this option, you will need the packege python-dotenv :

(venv)$ pip3 install python-dotenv

This is optional, but it makes it a lot easier rather than you having to memorize all environment variables which you pass via the terminal.

To run your flask app using this option, you will only need:

(venv)$ flask run

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM