DEV Community

Cover image for Build a Flask Application with Blueprints
Damilare Agba
Damilare Agba

Posted on

Build a Flask Application with Blueprints

I'll start by introducing us to Flask, a python based web application microframework as it allows us to determine a major part of the developmental process for our web application. Flask is a lightweight framework that is easy to get started with and can be used to build small projects. That being said, flask can also be used to build very large projects. To do this, we have to learn to organize our projects well. This here brought about the idea of blueprints.

We will be learning what blueprints are, why to use them, how they work, and how to use them. But first, let us start by setting up a basic flask app without a blueprint and then go on to talk more about blueprints.

We start by installing flask

$ pip3 install flask
Enter fullscreen mode Exit fullscreen mode

we then move to create our first flask app with a simple view function

from flask import Flask

app = Flask(__name__)

@app.route("/")
def index():
    return "Hello World"

if __name__ == "__main__":
    app.run(debug=True)
Enter fullscreen mode Exit fullscreen mode

This is our basic flask app named app.py in our myapp directory.

user:~/Desktop/myapp$ ls
app.py
Enter fullscreen mode Exit fullscreen mode

Now let's talk about blueprints. I am assuming we are already familiar with flask.

A blueprint (generally) can be seen as a model or a plan for representing an idea. With flask, a blueprint is a representation of a flask application i.e it performs its actions in a similar manner to that of the flask application. However, it is not a flask application. This is because a blueprint has to be registered in a flask application for its features or functions to be made available or useful in the entire project.

Blueprints are majorly used to organize our project and group related functions into separate python packages. For instance, if we were building an e-commerce web app, we can create two different blueprints i.e one for the authentication and the other for the logic. This helps to maintain a clean directory layout.

For a blueprint to work, it must be registered on the application to make available its functionalities. A blueprint uses resources from the main application in carrying out its tasks. These resources include static files, templates, application configurations, e.t.c. This shows that blueprints can't work on their own. However, they serve as a library of functions for the application to make use of.

We will proceed by creating an authentication and the main blueprint to demonstrate how they are used. So far, our working directory looks like

myapp
|
|--app.py
Enter fullscreen mode Exit fullscreen mode

Now we create our main blueprint named main.py

from flask import Blueprint

main = Blueprint('main', __name__)

@main.route('/')
def index():
    return "Hello from the main blueprint"
Enter fullscreen mode Exit fullscreen mode

and our app.py file becomes

from flask import Flask
from main import main

app = Flask(__name__)
app.register_blueprint(main)

if __name__ == "__main__":
    app.run(debug=True)
Enter fullscreen mode Exit fullscreen mode

Here we can see that our main.py file now has the view function and our app.py file holds a registration of the blueprint. With this, we can define more view functions related to the running of our application in the main blueprint without tampering with our app.py file. This helps to group together related view functions and foster good layouts.

We can also create our authentication blueprint then register it

from flask import Blueprint

auth = Blueprint('auth', __name__)

@auth.route('/login')
def login():
    pass
Enter fullscreen mode Exit fullscreen mode

and our app.py file becomes

from flask import Flask
from main import main
from auth import auth

app = Flask(__name__)
app.register_blueprint(main)
app.register_blueprint(auth)

if __name__ == "__main__":
    app.run(debug=True)
Enter fullscreen mode Exit fullscreen mode

With this done, our working direction looks more like

myapp
|
|--app.py
|--main.py
|--auth.py
Enter fullscreen mode Exit fullscreen mode

This brings together different view functions into packages to make sure our application is well arranged. For a full project, however, we would be creating our blueprints as python packages instead of files. This is to further create a better architecture for our application, make our application more pythonic and make the working directory look better.

We are not limited to a certian number of blueprints so we can create as much as possible. After creating a blueprint package, I have a directory like the one below.

myapp
|
|--app
|    |--main
|    |    |--__init__.py
|    |    |--main.py
|    |
|    |--auth
|    |    |--__init__.py
|    |    |--auth.py
|    |
|    |--__init__.py
|    |--errors.py
|    |--models.py
|    |-- templates/
|    |--static/
|
|--tests/
|--config.py
|--myapp.py
|--requirements.txt
Enter fullscreen mode Exit fullscreen mode

With this, our view functions are grouped into related packages know here as blueprints, our working directory looks better and a major advantage is that we can edit a part of the application without tampering with other parts.

This here will be my breakpoint for today.
I hope we've learnt a thing or two from this.

Cheers.

Discussion (3)

Collapse
nanumichael27 profile image
Nanu Oghenetega

It was hard locating a developer that uses flask like you. Please I'll love to know you and always communicate with you about the best practices

Collapse
blankgodd profile image
Damilare Agba Author

It'll be my pleasure. You can hit me up on Twitter twitter.com/agba_dr3

Collapse
yoanbello profile image
yoanbello

Great post. Is flask better than Express or Django? How are the Jobs oportunities with flask?