Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Flask Templates
  • Jinja2
  • Flask-REST API
  • Python SQLAlchemy
  • Flask Bcrypt
  • Flask Cookies
  • Json
  • Postman
  • Django
  • Flask Projects
  • Flask Interview Questions
  • MongoDB
  • Python MongoDB
  • Python Database
  • ReactJs
  • Vue.Js
Open In App
Next Article:
Flask Blueprints
Next article icon

Flask Blueprints

Last Updated : 01 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

  1. Create a blueprint in a separate module.
  2. Define routes inside that blueprint.
  3. 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-fs
Blueprint file structure

Step 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:

blueprint-1
/profile route

Next Article
Flask Blueprints

P

prajjqv52
Improve
Article Tags :
  • Python
  • python
  • Python Flask
Practice Tags :
  • python
  • python

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences