当前位置:网站首页>Get started quickly with flask (I) understand the framework flask, project structure and development environment
Get started quickly with flask (I) understand the framework flask, project structure and development environment
2022-07-28 09:11:00 【Xijing swordsman】
List of articles
Quick start Flask( One ) Understanding framework Flask、 Project structure 、 development environment
Web Develop lightweight frameworks Flask
github: https://github.com/pallets/flask
Official website : https://flask.palletsprojects.com/en/2.1.x/
Official documents :https://flask.net.cn/installation.html#installation
Python The most famous frames are Django, Besides, there are Flask、Tornado Other framework . although Flask Not the most famous frame , however Flask It should be one of the most flexible frameworks , This is also Flask The reason why developers like it .
Flask It's a comparison with Django For lightweight Web frame .
Flask Itself is equivalent to a kernel , Almost all other functions need to be extended ( Mail extension Flask-Mail, User authentication Flask-Login), All need to be implemented with third-party extensions . Its WSGI The tool box uses Werkzeug( Routing module ), The template engine uses Jinja2. These two are also Flask The core of the framework .
Extended list :http://flask.pocoo.org/extensions/
Flask is a web framework for Python, based on the Werkzeug toolkit.
Flash History and team
flask Born in 2010 April Fool's Day , Originally, it was just a small toy written by the author inadvertently , Unexpectedly, it has quietly become popular . The long 8 Year time ,flask No serious official version has been released , But it can't stop it from becoming github The most acclaimed Python Web frame .
flask The kernel has built-in two most important components , All other components are integrated through an extensible plug-in system . The two built-in components are werkzeug and jinja2.
Pallets project
official :https://palletsprojects.com/
The Pallets Projects are a collection of Python web development libraries that are associated with the Flask microframework as originally developed by Armin Ronacher. Today the Pallets Projects are a community-driven organization with the goal to maintain and improve these libraries.
Pallets The project is Python Web A collection of development libraries , These libraries and Armin Ronacher Originally developed Flask Frame related . Now ,Pallets The project is a community driven organization , The goal is to maintain and improve these libraries .
Pocoo The members of the team are from Python Community , Unify with Pocoo In the name of participating in multiple Python Library and application development . The team consists of Georg Brandl and Armin Ronacher Leader .
Pocoo The team has developed many popular projects , These include :
- Flask Micro network development framework
- Jinja2 template engine
- Pygments Syntax highlight package
- Sphinx Document processor
- Werzeug WSGI Toolset

flask Operation process
django And flask It's all implemented WSGI agreement
flask With werkzeug Come on wsgi It was packaged ,werkzeug Yes, it is run_simple(‘localhost’, 4000, hello) The first is ip, The second is the project startup port , The third is the executable object , When requested wsgi, The third parameter will be called
Web The server will hand over all requests from the client to Flask Program instance .
Program instance use Werkzeug To do routing distribution (URL Correspondence between request and view function ).
According to each URL request , Find the specific view function and call it .
Flask After calling the view function , You can return two types of content :
- String content : Take the return value of the view function as the content of the response , Return to the client ( browser ).
- HTML Template content : After getting the data , Put data in HTML In the template file , The template engine is responsible for rendering HTTP The response data , Then return the response data to the client ( browser ).
Use flask Scene
It's mainly for requirements, simple , Small applications with short project cycles .Flask The introduction is very simple , And can be extended to add other functions .
Use PyCharm Quickly create one Flask project
How to use PyCharm Quickly create one Flask project
Reference resources URL: https://blog.csdn.net/xili1342/article/details/125610684
1) Create a new Flask project
File–>New Project choice flask:
The created folder automatically contains the following :
At the beginning Flask There are many configuration values and conventions with reasonable defaults . By convention , Templates and static files are stored in the application Python In the subdirectory of the source tree , The names are templates and static .
static Catalog : Static files
templates Catalog : Templates
venv Catalog : fictitious Python Environmental Science , Use... In a virtual environment pip The installed packages will be installed into the current virtual environment
app.py file :
A minimal application app.py
from flask import Flask
# Flask The function takes a parameter name, It will point to the module where the program is located
app = Flask(__name__)
# The purpose of the decorator is to map routes to view functions
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
# Flask Of the application instance run Method start up WEB The server
app.run()
First we import Flask class . Examples of this class will become our WSGI application .
Then we create an instance of this class . The first parameter is the name of the application module or package . If you use The first mock exam ( Like this example ), Then you should use name , Because the name will be based on this Whether a module is used as an application or imported as a module changes ( May be ‘main’ , It could also be the name of the actual import ). This parameter is required , such Flask To know where to Find templates, static files, etc . More details can be found in Flask file .
And then we use route() Decorator to tell Flask Trigger function URL .
Function names are used to generate associated URL . The function finally returns the information that needs to be displayed in the user's browser .
2) Project start
Pycharm Click on run function 
Use the command line to run app.py
python app.py
Use flask The command runs
Now by setting the environment variable FLASK_APP=app.py To indicate which application is going to run , And then run flask run To start the application .
flask run
Appoint IP And port :
flask run --host=0.0.0.0 --port= 8001
or :
flask run -h 0.0.0.0 -p 8001
边栏推荐
- Deployment of kubernetes
- Vrrp+mstp configuration details [Huawei ENSP experiment]
- Recommend an artifact to get rid of the entanglement of variable names and a method to modify file names in batches
- 关闭页面时向后台发送消息
- Detailed explanation of MSTP protocol for layer 3 switch configuration [Huawei ENSP experiment]
- Digital signatures and Ca certificates
- CSV file storage
- Prometheus TSDB analysis
- Argocd Web UI loading is slow? A trick to teach you to solve
- 修改虚拟机IP地址
猜你喜欢

Marketing play is changeable, and understanding the rules is the key!

Network interface network crystal head RJ45, Poe interface definition line sequence

Introduction to official account
![[opencv] generate transparent PNG image](/img/0a/4afc9bda411634562f4b0f3915a7ba.png)
[opencv] generate transparent PNG image
![Detailed explanation of DHCP distribution address of routing / layer 3 switch [Huawei ENSP]](/img/9c/b4ebe608cf639b8348adc1f1cc71c8.png)
Detailed explanation of DHCP distribution address of routing / layer 3 switch [Huawei ENSP]

VS2015使用dumpbin 查看库的导出函数符号
![[advanced drawing of single cell] 07. Display of KEGG enrichment results](/img/77/0a9006743734c1993785d087f957f0.png)
[advanced drawing of single cell] 07. Display of KEGG enrichment results

Huid learning 7: Hudi and Flink integration

Completion report of communication software development and Application

The chess robot pinched the finger of a 7-year-old boy, and the pot of a software testing engineer? I smiled.
随机推荐
IP protocol of network layer
1299_ Task status and switching test in FreeRTOS
NPM and yarn use (official website, installation, command line, uploading your own package, detailed explanation of package version number, updating and uninstalling package, viewing all versions, equ
Basic syntax of jquey
C #, introductory tutorial -- debugging skills and logical error probe technology and source code when the program is running
Implementation principle of golang synergy
Detailed explanation of the basic use of express, body parse and express art template modules (use, route, path matching, response method, managed static files, official website)
Go channel
How to execute the SQL assembled in ODPs SQL function and get the return value?
台大林轩田《机器学习基石》习题解答和代码实现 | 【你值得拥有】
Principle of line of sight tracking and explanation of the paper
Digital signatures and Ca certificates
Explain cache consistency and memory barrier
训练一个自己的分类 | 【包教包会,数据都准备好了】
10. Learn MySQL like clause
Design for failure常见的12种设计思想
推荐一个摆脱变量名纠结的神器和批量修改文件名方法
Let me teach you how to assemble a registration center?
Deconstruction assignment of ES6 variables
Train your own classification [Bao Jiaobao, the data are ready]