Python Friday #42: Using Blueprint to Clean-up Your View Functions File

View functions are the main part of a Flask application. If we keep adding them to the same file, we quickly will be unable to maintain our application. Luckily for us, Flask comes with a solution to that problem called Blueprint.

This post is part of my journey to learn Python. You can find the other parts of this series here.

 

What is the problem?

If we keep adding functions to the application file, it gets harder and harder to find the function we need. Working with a smaller file where all functions belong to the same concept or topic is much simpler.

If we move all the view functions for the contact form to a file /views/contact_views.py, we make a big step into a better structured application:

However, if we now run our application and go to the /contacts page, we get a 404 error:

Instead of the contact form we only get an error

Just moving view functions around is unfortunately not enough. Flask now no longer finds our functions because it does not know of their existence.

 

Blueprint to the rescue

Flask offers us with Blueprint an out-of-the-box solution to organise our view functions. This feature will do all the heavy lifting and we can concentrate on a few little changes that will glue everything together.

 

Create a Blueprint and use it

First, we need to create a Blueprint for our view file /views/contact_views.py. For that we need to give it a name, tell it in which file the functions are and the name of our templates folder:

In the same file we replace all calls to @app with @blueprint:

The full /views/contact_views.py looks at the end like this:

 

Register the Blueprint

In our file app.py (where we have our Flask application) we need to register our blueprint.

Depending on how you start your Flask app, you need to call this method from different places. I want to run my application from the command line and from VS Code and ended up with this little hack:

If you start your application, you may end up with an error about Python being unable to load your module. In this case you can add these lines on top of your app.py file to add your application folder to the sys.path:

 

The result

Moving all the view functions for the contact form to its own file improves the structure of my Flask application. I now no longer need to go through the whole application to find the parts for the contact form – instead I open the file with the appropriate name in the views folder.

For the users nothing has changed, and they can use the contact form as before:

The contact form looks the same as before

With this refactoring done I can start moving my application to the new structure as explained in the last Python Friday post.

The biggest benefit of Blueprint for me is that you can refactor your application step by step. You do not need to switch to Blueprint and clean-up everything at once. That makes it a great help for existing applications in which you can move to a better structure at your own timetable.

 

Next

With everything in place to grow my application, it is now time to think about security.

2 thoughts on “Python Friday #42: Using Blueprint to Clean-up Your View Functions File”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.