Python flask with blueprint

reza andriyunanto
May 21, 2018 · 2 min read

Hello everyone, as we know, micro services is paradigm for software architecture that awesome, because less dependency about one into another services. Python flask have one that is blueprint, that is we can create blueprint of our micro services.

First, let decide what thing that we will create, imagine that we will create content management system that need two module, login and register. Here the structure of project.

Structure of project

From there we separated two service, login and register. That is different service but same implementation, so I just explain in one service, for example we use login_service.

First, we create app.py to return our app, fyi, I use docker compose to run this project, so I need return the apps to run the app, so here the code.

from login_service.modules import app


def run_app():
return app

Then, let create initialisation for our module ini __init__.py file.

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from login_service.modules import settings

app = Flask(__name__)
app.config.from_pyfile('settings.py')
db = SQLAlchemy(app)

from login_service.modules.login.views import login

app.register_blueprint(login, url_prefix='/api/login')

In here, we register our views into blueprint, in here, we have submodule login to handle login process. And settings.py is our setting for function that need configuration like connect into database or else.

Then what happen in views? here the code views.py

from flask import Blueprint, jsonify, json
from flask_restful import Api, Resource

login = Blueprint(
'login', #name of module
__name__,
template_folder='templates' # templates folder
)
api = Api(login)


class LoginApi(Resource):
def post(self):
// here login process happen

api.add_resource(LoginApi,'/')

There is views of login submodule, we create new Blueprint in views, then create api from flask_restful. Then, we create new method that run login process. After that, we need add resource the class and register the endpoint.

Oya! If you ask about the model, that is optional, to get data from database.
So, we can call our new api using this path url:port/api/login/

Get it? Okay I will explain more about correlation with micro service. In that process, we can create every endpoint with less dependency with another services. and we can register our new endpoint using api.add_resource in every views. Then in __init__.py we can register the module, without knowing what endpoint that added in views.

Aaaaand, this implementation will more powerful when implement using docker-compose. I think I will explain in the next article.

If you want learn the code, you can check here.

reza andriyunanto

Written by

Backend Engineer, Tech Enthusiasm, Love data and sometime curious about anything.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade