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
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.
We will be using Blueprint
instead of Flask
and will also give a name to applications.
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
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:
-
Open terminal
-
Go to project folder
-
Activate virtual environment
-
Run the main file
cd project_directory
source venv/bin/activate
python src/app.py
* 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:
- Flask Interview Questions
- Part 2: Deploy Flask API in production using WSGI gunicorn with nginx reverse proxy
- Part 3: Dockerize Flask application and build CI/CD pipeline in Jenkins
- Part 1: Creating and testing Flask REST API
- Configure Logging in gunicorn based application in docker container
- Named Entity Recognition using spaCy and Flask
- Deploying Keras Model in Production using Flask