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
Now run the application -
from flask import Flask from products import product app = Flask(__name__) app.register_blueprint(product) if __name__=="__main__": app.run(debug=True)
flask --app main runAfter run the application you can test your application with the http://127.0.0.1:5000/products/grid URL.
Comments
Post a Comment