Blueprints in Flask API Development

Carvia Tech | October 29, 2019 | 2 min read | 217 views | Flask - Python micro web framework

Flask provides blueprints to lets us organize our large application code by breaking it into smaller and reusable modules.

Flask uses concept of blueprints for making application components and supporting common patterns within an application or across applications. Blueprints can simplify how large applications work and also provide a central means for Flask extensions to register operations on applications. It also works in a way to extend existing Flask applications.

A Blueprint object works similarly to a Flask application object, but it is not actually an application.

Blueprint based Flask API

When working with blueprint based application, we have to build different applications in different python code files and then register those applications in blueprint.

Let’s say, we have two different applications i.e. app_a & app_b.

We want to serve both applications as single flask application then we will be creating both applications in different scripts as follows.


We will be using Blueprint instead of Flask and will also give a name to applications.
from flask import Blueprint, jsonify

app_a_api = Blueprint("app_a", __name__)

def home():
    return "Hello, World!"

def health():
    return jsonify({"status": "UP"}), 200
from flask import Blueprint, jsonify

app_b_api = Blueprint("app_b", __name__)

def home():
    return "Hello, World!"

def health():
    return jsonify({"status": "UP"}), 200

Now we will be registering both applications in another script
from flask import Flask
from app_a import app_a_api
from app_b import app_b_api

app = Flask(__name__)
app.register_blueprint(app_b_api)"", port = '8000', debug=True, threaded=True)

Now, when we want to start application then we shall run file only which will start our other applications as well.

Run Blueprint based flask app

To run the flask app, we will be following these steps:

  1. Open terminal

  2. Go to project folder

  3. Activate virtual environment

  4. Run the main file

cd project_directory
source venv/bin/activate
python src/
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 874-012-700

Top articles in this category:
  1. Flask Interview Questions
  2. Top 100 interview questions on Data Science & Machine Learning
  3. Google Data Scientist interview questions with answers
  4. Python coding challenges for interviews
  5. Part 2: Deploy Flask API in production using WSGI gunicorn with nginx reverse proxy
  6. Part 3: Dockerize Flask application and build CI/CD pipeline in Jenkins
  7. Part 1: Creating and testing Flask REST API

Find more on this topic:
Machine Learning image
Machine Learning

Data science, machine learning, python, R, big data, spark, the Jupyter notebook, and much more

Last updated 1 week ago

Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info