Build a Flask Application with Blueprints
Tags: python, flask, beginners, webdev
Published: 2020-12-10 00:25:34

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
```shell
$ pip3 install flask
```

we then move to create our first flask app with a simple view function
```py
from flask import Flask

app = Flask(__name__)

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

if __name__ == "__main__":
    app.run(debug=True)
```

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

```shell
user:~/Desktop/myapp$ ls
app.py
```

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
```

Now we create our main blueprint named main.py
```py
from flask import Blueprint

main = Blueprint('main', __name__)

@main.route('/')
def index():
    return "Hello from the main blueprint"
```

and our app.py file becomes
```py
from flask import Flask
from main import main

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

if __name__ == "__main__":
    app.run(debug=True)
```

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
```py
from flask import Blueprint

auth = Blueprint('auth', __name__)

@auth.route('/login')
def login():
    pass
```

and our app.py file becomes
```py
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)
```

With this done, our working direction looks more like
```shell
myapp
|
|--app.py
|--main.py
|--auth.py
```

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.

```shell
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
```

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.