1. Blueprints

A profile,

As you can see from the previous points, all of our view functions are written in one file, as our business gets more complex. View functions also become more numerous, making them difficult to read and maintain. Like this:

from flask import Flask
from flask_script import Manager

app = Flask(__name__)


@app.route('/')
def index(a):
    return 'index page'

# home view
@app.route('/home')
def home(a):
    return 'home page'

# center view
@app.route('/center')
def center(a):
    return 'center page'

# order view
@app.route('/order')
def order(a):
    return 'order page'

# cart view
@app.route('/cart')
def cart(a):
    return 'cart page'


if __name__ == '__main__':
    # 0.0.0.0 means that any address that represents the machine is accessible
    app.run(host='0.0.0.0', port=5000, debug=True)  # run the program

Copy the code

As you can see, when our business has new requirements, we also add view functions to this.

Two, use decorators to solve

For example, we pull out the home view above and put it in a separate home.py file. Write only one view function in this file that is not decorated by the decorator.

def home(a):
    return 'home page'
Copy the code

Then, in Flask’s launcher file, I re-decorate the Home view with decorators.

from flask import Flask
from home import home

app = Flask(__name__)

# Decorate the home view
app.route('/home')(home)


@app.route('/')
def index(a):
    return 'index page'


if __name__ == '__main__':
    # 0.0.0.0 means that any address that represents the machine is accessible
    app.run(host='0.0.0.0', port=5000, debug=True)  # run the program

Copy the code

Let’s debug in the browser:

Three, blueprints,

  • Flask blueprints are a bit like Django framework apps. Modularize a project, putting the views, static files, and templates used by each module into a box.

  • Before using the blueprint, we need to create the blueprint. Continuing with our example of the home view function above, modify the code in the home.py file as follows:

from flask import Blueprint

# app_HOME specifies the name of the blueprint,
# __name__ refers to the module where the blueprint resides
app_home = Blueprint('app_home', __name__)


# Register blueprint routing
@app_home.route('/home')
def home(a):
    return 'home page'

Copy the code

Flask startup file, import blueprint and register blueprint:

from flask import Flask
# import blueprint
from home import app_home

app = Flask(__name__)


# Registration Blueprint
app.register_blueprint(app_home)


@app.route('/')
def index(a):
    return 'index page'


if __name__ == '__main__':
    # 0.0.0.0 means that any address that represents the machine is accessible
    app.run(host='0.0.0.0', port=5000, debug=True)  # run the program

Copy the code

Let’s debug in the browser:

app.register_blueprint(app_home, url_prefix='/user')
Copy the code

Let’s debug in the browser:

4. Import with blueprint module

We can also manage blueprints in Flask using the Python concept of packages. As in the above example, we create a home folder (package) :

from flask import Blueprint

# app_HOME specifies the name of the blueprint,
# __name__ refers to the module where the blueprint resides
app_home = Blueprint('app_home', __name__)

from .views import home

Copy the code

To register blueprint routes in views.py:

from . import app_home


# Register blueprint routing
@app_home.route('/home')
def home(a):
    return 'home page'

Copy the code

Start importing blueprints in Flask:

from flask import Flask
# import blueprint
from home import app_home

app = Flask(__name__)

# Registration Blueprint
app.register_blueprint(app_home, url_prefix='/user')


@app.route('/')
def index(a):
    return 'index page'


if __name__ == '__main__':
    # 0.0.0.0 means that any address that represents the machine is accessible
    app.run(host='0.0.0.0', port=5000, debug=True)  # run the program

Copy the code

Let’s debug in the browser:

from . import app_home
from flask import render_template


# Register blueprint routing
@app_home.route('/home')
def home(a):
    return render_template('home.html')

Copy the code

Let’s debug in the browser:

Welcome to follow my official account: