VOOZH about

URL: https://www.geeksforgeeks.org/python/flask-blueprints/

⇱ Flask Blueprints - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Flask Blueprints

Last Updated : 23 Jul, 2025

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:

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:

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.

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.

  • 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.

Step 3: Register the Blueprint

Modify __init__.py in the /app folder.

Add this code to __init__.py in the /app/routes folder.

Step 4: Run the App

Modify app.py.

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
Comment
Article Tags: