Flask Blueprint Demonstrated in 2 Minutes

Chuan Zhang
2 min readMay 31, 2021

Flask blueprint is a great way to structure flask application by organizing views and other code into separate and reusable functionality components. It is conceptually similar to the applications in Django framework, and makes Flask applications easier to scale up.

There has been a lot of tutorials online explaining how flask blueprint works together with examples. However, many of them explain/demonstrate blueprints in more realistic examples involving practical functionalities, such as authentication, blogs, etc. which make the explanation/demonstration not very obvious. In this short post, how flask blueprint can be used to organize code of an application into separated modules is demonstrated in a straightforward example without including any other code.

Assume we have three functionally separated views (code which handles and responds to user requests), app1, app2, and app3. We can organize our application as follows:

.
├── app1
│ ├── app1.py
│ ├── __init__.py
│ ├── static
│ └── templates
│ └── app1.html
├── app2
│ ├── app2.py
│ ├── __init__.py
│ ├── static
│ └── templates
│ └── app2.html
├── app3
│ ├── app3.py
│ ├── __init__.py
│ ├── static
│ └── templates
│ └── app3.html
├── main.py
├── static
└── templates
└── main.html
11 directories, 11 files

The main module include its logic in main.py and its UI in templates/main.html. Each of the three features is imported and registered with the application.

The three blueprints are created and implemented as separated modules respectively. The code snippets below shows how blueprint app1 and its UI are implemented. The other two blueprints are implemented similarly.

--

--