How to create blueprint in flask

What is the blueprint?

  • Blueprint is a way to organize Flask applications by grouping its functionality into reusable components.
  • Blueprints can arrange all functionality, such as views, templates, and other resources. 
  • It can have its own views, forms, models, static files, and templates.

How to create a blueprint?

Let's take an example of product management application. So let's assume In this application we have many module packages and we have one main module which will run our whole application.
So let's start the implementation suppose we have two file with the names products.py and main.py.
Now we will try to create product blueprint.

products.py
from flask import Blueprint,render_template

product = Blueprint("products",__name__)

@product.route('/grid')
def grid():
    return render_template("grid.html")
We can also give our template and assets details like this -
product = Blueprint("products",__name__,template_folder='templates',
    static_folder='static', static_url_path='assets')
Now we have to register our blueprint in our main module. 
main.py
from flask import Flask
from products import product

app = Flask(__name__)

app.register_blueprint(product)

if __name__=="__main__":
	app.run(debug=True)
Now run the application -
flask --app main run
After run the application you can test your application with the http://127.0.0.1:5000/products/grid URL.

Comments