Flask - introduction

1. Introduction to flask

  • WSGI(Web Server Gateway Interface)

It is designed based on the existing [CGI] standard, but the layer of WSGI is lower than CGI, it has strong scalability and can run in a multi-threaded or multi-process environment.

WSGI is used as a low-level interface between a Web server and a Web application or application framework to enhance the common ground of portable Web application development.

  • Introduction

The author is Armin Ronacher, born in 2010. Originally, this project was just a joke made by the author on April Fool's Day. Later, due to its popularity, it became an official project.

Features: 1. Micro-framework, concise, and only do what he needs to do, which provides developers with great scalability; 2. Flask and the corresponding plug-ins are well written and very cool to use. For example, use SQLAlchemy's ORM to operate the database; 3. Flask is very flexible, he will not help you make too many decisions, some of which you can change according to your wishes. For example: when using Flask to develop a database, whether to use SQLAlchemy or MongoEngine, the choice is entirely in your own hands, which is different from Django. It is very easy to replace the default Jinija2 template engine with other template engines.

2. Use of flask

  • flask installation
激活虚拟环境
cd Virtualenv\flask-env\Scripts
activate

安装flask
pip install flask

查看版本
1.输入python 
2.输入import flask
3.输入flask.__version__
  • Create project

  • The first flask project

from flask import Flask


# 传入__name__初始化一个Flask实例

app = Flask(__name__)

# http://127.0.0.1:5000/    -->请求hello_world()函数,并将结果返回给浏览器
# app.route装饰器映射URL和执行的函数。这个设置将根URL映射到了hello_world函数上
@app.route('/')
def hello_world():
    a = 1
    b = 0
    c = a/b
    return "Hello World"

if __name__ == '__main__':
    app.run(debug=True)  # 默认配置
    
    # 运行本项目,host=0.0.0.0可以让其他电脑也能访问到该网站,port指定访问的端口。默认的host是127.0.0.1,port为5000
    #app.run(host='0.0.0.0',port=9000,debug=True)

3. Debug mode

1、在运行edit configurations中勾选Flask_debug
2、4种类开启debug模式的方法
  • effect

It is convenient to locate the problem on the web page.
Save the code directly, without re-run

# debug_demo.py
from flask import Flask


app = Flask(__name__)

# debug方式一
# app.debug = True

# debug方式二--配置文件
# app.config.update(DEBUG=True)

# debug方式三--加载文件
# import configs
# app.config.from_object(configs)

@app.route('/house/<id>')
def hello_world(id):
    return '您请求的参数是:%s'%id

if __name__ == '__main__':
    # 启动一个应用服务器,并监听
    # debug方式四
    app.run(debug=True)

4. Use of configuration files

# configs.py

# 开启app debug模式
DEBUG=True

# SECRET_KEY
···

# SQLALCHEMY
···

Guess you like

Origin blog.csdn.net/qq_25672165/article/details/112803058