简体   繁体   English

Flask如何处理“flask run”cli命令?

[英]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 .我一直试图了解如何通过运行flask run来启动 flask 项目。

What actually happens behind the scenes?幕后究竟发生了什么? How is it possible to actually launch the app using the flask keyword?如何使用flask关键字实际启动应用程序? 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).据我所知,它基于 Click 库( https://palletsprojects.com/p/click/ ),但我仍然不明白一步一步发生了什么(内部)。

If someone could explain that would be appreciated.如果有人可以解释,将不胜感激。 Thank you!谢谢!

To launch a flask application, you can use the command flask run .要启动flask应用程序,您可以使用命令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.在运行任何 flask 应用程序之前,需要通过设置某些环境变量来告诉flask如何导入它。 On your terminal, you run these commands in their order:在您的终端上,您按顺序运行这些命令:

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

What is app.py ?什么是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.flask run的 output 表明服务器运行在 IP 地址127.0.0.1上,始终是您自己计算机的地址。 This address is so common that is also has a simpler name that you may have seen before: localhost .这个地址非常常见,它还有一个您之前可能见过的更简单的名称: 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.部署在生产 web 服务器上的应用程序通常侦听端口 443,如果不实施加密,有时侦听端口 80,但访问这些端口需要管理权限。 Since this application is running in a development environment, Flask uses the freely available port 5000.由于此应用程序在开发环境中运行,因此 Flask 使用免费可用的端口 5000。

To access you application on your web browser, paste this URL:要在 web 浏览器上访问您的应用程序,请粘贴此 URL:

 http://localhost:5000/

There are other environment variables that flask can use: flask 可以使用的其他环境变量:

  • FLASK_ENV (sets your environment, either to development or production) FLASK_ENV(设置您的环境,开发或生产环境)
  • FLASK_DEBUG (enables/disables debugging) FLASK_DEBUG(启用/禁用调试)

Before running flask run , you will run need to add the other environment variables in your terminal:在运行flask run之前,您需要在终端中添加其他环境变量:

(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.现在,您将指定您的应用程序在development服务器上运行,并且您已启用 flask 调试功能。

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.从版本 1.0 开始,Flask 允许您注册希望在运行 flask 命令时自动导入的环境变量。

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.建议您将 flask 环境变量(这些是运行应用程序所需的)存储在项目根目录中名为.flaskenv的文件中。

(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 :要实现此选项,您将需要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:要使用此选项运行 flask 应用程序,您只需:

(venv)$ flask run

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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