To create a blueprint, we have to create a new package in your application that is named after the functionality that it will contain.
Lets say you want to create a blueprint for the users and authentication functionality, you can create a new directory in your project and call it users. Same way if you want to create a blueprint for posts functionality you can create a new directory and call it posts.
Now, you have to make sure that all the directories you have created are also packages as well.
In order to tell python that those are packages, you have to create a __init__.py file inside
the directory. It does’nt even have to contain anything per say as long as the file exists in the
directory python automatically recognizes it as a package.
Now within each of the packages that you just created, you also want to add a routes.py file that will only contain the routes that have anything to do with that functionality. For instance, the routes.py that you add to your users package will only contain routes that are related to the users and authentication functionality.
After all this is done, now we can go ahead and create a blueprint. Let’s assume we are creating the blueprint for our users package.
First import the Blueprint class from flask,
from flask import Blueprintand then create an instance of the Blueprint
users = Blueprint('users', __name__) # its the same as creating an instance for a flask object but here we are adding the name of our blueprint as an argument too.Now you can start adding your routes.
@app.route('/logout')def logout(): logout_user() return redirect(url_for('home'))Now if you look at this example route above, you can see that it is using the global app variable to create the routes. We arent going to do that in our users blueprint, instead, we are going to create routes specifically for this users blueprint and then register these with our application later. So to do this, instead of using “app.route” we will instead user “users.route”. Take note that “users” is whatever that variable name is that you used for your blueprint. see below for an example route inside the users package
@users.route('/logout')def logout(): logout_user() return redirect(url_for('home'))After that just fix your imports and put them where they belong, and delete the previous packages since theyre all empty now
Now that the above is done. Open your main application’s __init__.py file, in this specific
scenario we can see that the routes that we were importing from our prevous packages are no
longer available. that is; see below
from flasky import routesWe can import all the files(the routes file in this case) you need by importing those blueprint objects from each of the new packages you’ve created and then register them with your route. See below:
from flasky.users.routes import usersapp.register_blueprint(users)You can repeat this process for all the files/packages that you need
One more thing to do before you run your app to see if the blueprint worked, In our app so far, we’ve been using the url_for function to link to all of our routes, and now we need to change those to become the urls for the relative blueprints. So you need to replace the name of the route function in the url_for function with the blueprint name followed by the route function name. So you’ll need to perform a search on the whole application and replace them one by one.
for example if you have a “url_for(‘new_post’)”, you need to replace it with “url_for(‘posts.new_post’)” where posts is the blueprint name and new_post is the name of the route function.