Basically, a flask blueprint is a way for you to organize your flask application into smaller and re-usable application
Just like a normal flask application, a blueprint defines a collection of views, templates and static assets.
It should be noted that a blueprint is not a ‘plug and play’ app, it cannot run on it’s own every blueprint must be registered on a real Flask application before it can be used.
Why should you use blueprints
The main reason why you should use blueprints is to de-couple your application into smaller re-usable components. Like i said earlier in this series it would make perfect sense to move all our api calls into a blueprint.
This makes the code more maintainable and easier to debug.
How do you create a blueprint
Well, the choice is really up to you, since flask is very liberal and doesn’t enforce much conventions on you. There are several ways of creating a blueprint, but personally i prefer to treat blueprints as separate python packages with everything they provide in their own folder. That way, if something goes wrong with our api for example, you already know where to start looking.
Lets create a new package and call it api
Your application structure should look like this:
Open api.py and include the following
The Blueprint class takes three basic arguments:
The first argument is the blueprints name
The second argument is very important it’s the import_name. This name has to be set to the name of our package (which is also api) as Flask uses the import_name for some internal operations such as locating the template folder of the blueprint and locating various files and objects of the main application from the blueprint. (This ensures that methods like render_template and send_static_files work properly and give us the actual files that we want)
The third argument is the url prefix of the blueprint. With this we were able to remove the redundancy of prefixing all our api urls with /api.
Let’s register the blueprint in votr.py
That’s all you need to do. Reload the webpage and the application should still function like it did before.
That’s all you need to know about blueprints, they’re really helpful when you want to refactor your flask application and split it into smaller parts. They help you to add extra routes and functions to your application without chunking the code into a single file.
Join me in the next part where I’ll show you how to schedule and run background jobs in Flask with Celery.
We’re going to use celery to implement a feature where users can set the time they want a poll to stay active. after which the poll would be automatically closed.