python flask uses blueprint

python flask uses blueprint


preface
Project address: https://github.com/SEN-Wanted/BackEnd
Blueprint is a very useful way to modularize applications. Almost all large applications will adopt this mode. Is it necessary to adopt this mode for the development of some small projects? My opinion is that if the project is a team collaboration project, it is better to adopt the development method of blueprint at the beginning. Take this project as an example (it is expected to implement a back-end of an order app, and the project is small). The initial file structure is as follows:

Appserver/ 
├── App  
│   ├── static/      # Static resource folder  
│   ├── templates/   # Template folder  
│   ├── __init__.py    
│   └── view.py    #  Master file, without blueprint
├── run.py   
├── config.py    # configuration file
├── requirements     # Requirements document  
└── README.md  

At first, there was no problem. However, when multiple people cooperated, although each person was responsible for writing different parts, they wrote in the same file (view.py). The problem was that they ignored the submission of others when updating the file, which might overwrite the code of others. This is why we decided to use blueprint to solve this problem. Each person only needs to focus on the code file they are responsible for, After use, the documents are organized as follows:

Appserver/
├── app
│   ├── static/      # Static resource folder
│   ├── templates/   # Template folder
│   ├── json_test/   # Used to store test files
│   ├── __init__.py  # Initialization file
│   ├── store_info.py # blueprint
│   ├── user_info.py # blueprint
│   ├── order_info.py # blueprint
│   ├── store_by_id.py # blueprint
│   ├── orders_by_user_id.py # blueprint
│   ├── type_search.py # blueprint
│   ├── user_login.py # blueprint
│   ├── models.py # data format
│   ├── createdb.py # Create database
│   └── view.py # Call blueprint
├── config.py    # configuration file
├── run.py # Main program file
├── requirements     # Requirements document
└── README.md

Although the file structure has become more complex, there are two benefits I have personally experienced:

1. convenient maintenance
2. easy to add requirements

blueprint use

blueprint is easy to use. Take the following file structure as an example:

Appserver/
├── app
│   ├── static/      # Static resource folder
│   ├── templates/   # Template folder
│   ├── __init__.py  # Initialization file
│   └── test_blueprint.py # blueprint
└── run.py

1. statement blueprint
To put it simply, declare a variable as blueprint and give it a unique__ name__, After that, we can view PY
test_blueprint.py

# -*- coding: utf-8 -*-
from flask import Blueprint, request

test_blueprint = Blueprint('fir_blueprint',__name__)

@test_blueprint.route('/test', methods=['GET'])
def test():
	return "test success"

2. use blueprint
After declaring blueprint, you need to_ init_ py
_init_.py

# -*- coding: utf-8 -*-
from flask import Flask
from test_blueprint import test_blueprint

app = Flask(__name__)
app.register_blueprint(test_blueprint)

Other documents:
run.py

# -*- coding: UTF-8 -*-
from app import app
if __name__ == '__main__':
    '''
    open debug mode
    # Set host='0.0.0.0'
    '''
    app.run(debug=True, host='0.0.0.0')

Run run PY , and then access it in the browser http://0.0.0.0:5000/test You can see the returned success message "test success"

 

 

 

Tags: Python

Posted by Hartley on Tue, 31 May 2022 14:27:00 +0530