Blueprints in Flask API Development

Upasana | October 29, 2019 | 2 min read | 298 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.

Note

We will be using Blueprint instead of Flask and will also give a name to applications.

app_a.py
from flask import Blueprint, jsonify

app_a_api = Blueprint("app_a", __name__)


@app_a_api.route("/a")
def home():
    return "Hello, World!"


@app_a_api.route("/health_a.json")
def health():
    return jsonify({"status": "UP"}), 200
app_b.py
from flask import Blueprint, jsonify

app_b_api = Blueprint("app_b", __name__)


@app_b_api.route("/b")
def home():
    return "Hello, World!"


@app_b_api.route("/health_b.json")
def health():
    return jsonify({"status": "UP"}), 200

Now we will be registering both applications in another script

app.py
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_a_api)
app.register_blueprint(app_b_api)
app.run(host="0.0.0.0", port = '8000', debug=True, threaded=True)

Now, when we want to start application then we shall run app.py 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/app.py
Output
 * 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 http://127.0.0.1:8000/ (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. Part 2: Deploy Flask API in production using WSGI gunicorn with nginx reverse proxy
  3. Part 3: Dockerize Flask application and build CI/CD pipeline in Jenkins
  4. Part 1: Creating and testing Flask REST API
  5. Configure Logging in gunicorn based application in docker container
  6. Named Entity Recognition using spaCy and Flask
  7. Deploying Keras Model in Production using Flask

Recommended books for interview preparation:

Find more on this topic: