Blueprints in Flask help you organize your application into modular, reusable components. They let you group related routes, templates, and static files together, which is especially useful for large projects. With blueprints, you can develop, test, and maintain different parts of your app separately, and then register them with your main application.
How to Use Blueprint in Flask Applications
The main idea behind Blueprints is to organize a Flask application into separate modules, each handling a specific feature with its own routes, templates, and static files. This helps keep the application structured and reduces complexity. Additionally, these modules can not only be integrated into the main Flask app but also reused in other applications if needed.
Steps to Use Blueprints:
- Create a blueprint in a separate module.
- Define routes inside that blueprint.
- Register the blueprint in the main application.
Project Structure
A typical Flask project using blueprints looks like this:
/flask_app
│── /app
│ │── /routes
│ │ │── __init__.py
│ │ │── user_routes.py
│ │── __init__.py
│── run.py
- user_routes.py - Contains routes related to users.
- __init__.py (inside /routes) - Allows importing different route files.
- __init__.py (inside /app) - Initializes the Flask app.
- app.py - The entry point of the application.
Let's create a simple User Blueprint to handle user-related routes.
1. Create a Blueprint
Create a new file user_routes.py inside the routes folder. For example:
Python from flask import Blueprint # Create a blueprint instance user_bp = Blueprint('user', __name__) @user_bp.route('/users') def get_users(): return {"message": "List of users"}
This creates a Blueprint called 'auth' for handling user authentication.
2. Register the Blueprint in the Main App
Now, modify __init__.py inside the /app folder:
Python from flask import Flask from app.routes.user_routes import user_bp # Import blueprint def create_app(): app = Flask(__name__) # Register blueprint app.register_blueprint(user_bp, url_prefix='/api') return app
app.register_blueprint(user_bp, url_prefix='/api') - Registers the user_bp blueprint with a URL prefix /api.
3. Run the Application
Now, create an app.py file to start the Flask app.
Python from app import create_app app = create_app() if __name__ == '__main__': app.run(debug=True)
Now, the routes in the auth Blueprint will work when werun your app and visit the URL- http://127.0.0.1:5000/api/users, we will get:
{"message": "List of users"}
Now, let's build a complete Flask app using blueprints, templates, and static files.
Flask Application with Blueprints
We’ll build a structured Flask application using Blueprints, templates, and static files. Instead of keeping all routes in a single file, we will organize different features into separate modules, making the application easier to manage and scale.
This app will have:
- A user profile route handled by a separate User Blueprint.
- A template (index.html) to display a dynamic welcome message.
- A modular project structure that keeps the code clean and reusable.
File Structure
The file structure of our application is should be like this:
Blueprint file structureStep 1: Create a User Blueprint
Modify user_routes.py to return an HTML template.
Python from flask import Blueprint, render_template user_bp = Blueprint('user', __name__, template_folder='../templates') @user_bp.route('/profile') def profile(): return render_template('index.html', name="Geek")
- template_folder='../templates' - Tells Flask where to look for templates.
- render_template('index.html', name="Geek") - Passes data to the template.
Step 2: Create an HTML Template
Inside the templates folder, create index.html.
Python <!DOCTYPE html> <html> <head> <title>User Profile</title> </head> <body> <h1>Welcome, {{ name }}</h1> </body> </html>
Step 3: Register the Blueprint
Modify __init__.py in the /app folder.
Python from flask import Flask from app.routes import blueprints # Import the list of blueprints def create_app(): app = Flask(__name__) # Register all blueprints for blueprint in blueprints: app.register_blueprint(blueprint) return app
Add this code to __init__.py in the /app/routes folder.
Python from flask import Blueprint # Import all route blueprints from app.routes.user_routes import user_bp # Create a list of blueprints to register in the app blueprints = [user_bp]
Step 4: Run the App
Modify app.py.
Python from app import create_app app = create_app() if __name__ == '__main__': app.run(debug=True)
Testing the App
Run python run.py and visit http://127.0.0.1:5000/profile. We can see that we have accessed the '/profile' route from the routes flderin the Below is the snapshot of the live app:
/profile route Similar Reads
Flask - Templates In this article, we are going to learn about the flask templates in Python. Python is a high-level, open-source, object-oriented language, consisting of libraries, used in many domains, such as Web Development, Machine Learning, and so on. In web development, Python is used for building REST APIs, s
8 min read
Building Flutter Apps in Python In this article, Here we will use Flutter apps in Python, for this library Flet will help to Build the Application. What is Flet? Flet is a Python Library using which developers can build real-time web apps, Mobile Apps, and Desktop apps without directly using Flutter. As to building apps using Flut
9 min read
Python Flask Projects with Source Code (Beginners to Advanced) Flask, a Python web application framework, was created by Armin Ronacher. Known for its lightweight and efficient nature, Flask is designed for quick starts and accommodates complex applications. It is based on the Werkzeug WSGI toolkit and Jinja2 template engine.In this article, weâve curated a lis
4 min read
BlueFlame-Labs Interview Experience Blue Flame Labs is looking for talented individuals proficient in C++, Java, C#, JavaScript, and other related technologies to join their team. If you're interested in a dynamic work environment and a competitive salary ranging from 4.5 to 12 LPA, read on to learn about their hiring process. Applica
2 min read
Introduction to Aeromodelling Someone has ever wondered how airplanes, drones fly? If yes then let's deep dive into the science of airplanes and drones. Among the most appealing aspects of the course has been the challenge of building planes out of pieces of wood and having the propellers rotate with real engines. Even though Ae
8 min read