当前位置:网站首页>First meet flask

First meet flask

2022-07-23 11:16:00 ZXY_ lucky

1.Flask Introduce

Flask It's lightweight Web Development framework , Than Django It's a lot lighter
Flask The core is Werkzeug( Routing module ),Jinja2 template engine , To realize other functions, you need to install and expand
Flask Though light , But it has strong expansion ability , Rely on extensions to add functionality

Flask Official address

2. Build a virtual environment

First step
 Insert picture description here
The second step
 Insert picture description here

The third step
 Insert picture description here
Step four
 Insert picture description here

2.1 Advance and retreat virtual environment

Exit virtual environment

First step : Enter the virtual environment folder
 Insert picture description here
The second step : Carry out orders , And exit venv file
 Insert picture description here
Enter the virtual environment
First step : Get into venv Under the document scripts Folder
 Insert picture description here
The second step : perform
 Insert picture description here
The third step : sign out venv file
 Insert picture description here

3. install Flask

Import the installation files in the root directory

pip install -r  file name .txt

4. Realization Flask

4.1 Realization Flask Get started with Applications

Created in the root directory app.py file

 First step : Instantiation Flask
from flask import Flask
#  Instantiation flask
# __name__  Current package name 
app = Flask(__name__)

 The second step : Configure function routing 
#  Configure the routing 
@app.route('/hello')
def hello():
    return "<h1> ha-ha 12345</h1>"
    
 The third step : start-up flask
if __name__ == '__main__':
    app.run()

4.2 Factory mode

Factory mode : Responsible for creating other types of objects , The user calls this method with parameters , The factory returns to the client

4.2.1 Example of factory model
#  Write an interface class 
class Animal():
    def do_say(self):
        pass

class Cat(Animal):
    def do_say(self):
        print('miao miao')

class Dog(Animal):
    def do_say(self):
        print('wang wang')

class Forest():
    def sav(self,animal_type):
        return eval(animal_type)().do_say()

if __name__ == '__main__':
    a = input(' Please enter the animal type :Cat or Dog:')
    print('111',a)
    Forest().sav(a)
4.2.2 Factory mode

encapsulation APP : establish py file

from flask import Flask

def create_app():
	flask_app = Flask(__name__)
	print(' Call me ')
	return flask_app

Create a file and call

View routes — Run on the command line : flask routes

 First step : Import the encapsulated function 
from create_app import create_app
from flask import url_for

 The second step : call 
app = create_app()

# endpoint  Used for route reverse generation 
#method  Usage method 
@app.route('/hello',methods=['get','post'],endpoint='aaa')
def hello():
    return 'hello'

# url_for  Reverse query 
@app.route('/path')
def get_path():
    url = url_for('aaa')
    print(url)
    return url
    
 The third step : start-up 
if __name__ == '__main__':
    app.run()

4.3 Flask To configure

  1. Load... From the configuration object
  2. Load from the configuration file
  3. Load from the environment variable
4.3.1 Load... From the configuration object

First step : establish py file

class DefaultConfig:
    #  Property name   It has to be all in capitals 
    NAME = "haha"
    #  Automatically refresh 
    DEBUG = True

The second step : call

from flask import Flask
from settings.config import DefaultConfig
#  Instantiation flask
# __name__  Current package name 
app = Flask(__name__)
app.config.from_object(DefaultConfig)  #  Load configuration from object 
print('1111', app.config)

# "D:\python\p7\pwd.py"
#  Configure the routing 
@app.route('/hello')
def hello():
    return "<h1> ha-ha 12345</h1>"

if __name__ == '__main__':
    app.run()
4.3.2 Load from the configuration file

First step : establish py file

#  Through the configuration file 

AGE = 18

The second step : call

from flask import Flask

#  Instantiation flask
# __name__  Current package name 
app = Flask(__name__)
app.config.from_pyfile('setting.py') #  Load configuration from file 

print('1111', app.config)

# "D:\python\p7\pwd.py"
#  Configure the routing 
@app.route('/hello')
def hello():
    return "<h1> ha-ha 12345</h1>"

if __name__ == '__main__':
    app.run()

4.3.3 Call... From the environment variable

First step : Create files from different directories

PASSWORD = 123456

The second step : Configure environment variables

from flask import Flask

#  Instantiation flask
# __name__  Current package name 
app = Flask(__name__)
app.config.from_envvar('pwd',silent=True) #  Configure from environment variables 
print('1111', app.config)

# "D:\python\p7\pwd.py"
#  Configure the routing 
@app.route('/hello')
def hello():
    return "<h1> ha-ha 12345</h1>"

if __name__ == '__main__':
    app.run()

The third step : Configuration environment

4.4 Starting mode

  1. app.run()
  2. flask.run()
  3. Configure pepper

Method 1 :

if __name__ == '__main__':
    app.run()

Method 2 :

 perform :flask fun  command 

Method 3 :
 Insert picture description here

原网站

版权声明
本文为[ZXY_ lucky]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207230537244254.html