Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Flask Framework Cookbook

You're reading from   Flask Framework Cookbook Enhance your Flask skills with advanced techniques and build dynamic, responsive web applications

Arrow left icon
Product type Paperback
Published in Jul 2023
Publisher Packt
ISBN-13 9781804611104
Length 318 pages
Edition 3rd Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Shalabh Aggarwal Shalabh Aggarwal
Author Profile Icon Shalabh Aggarwal
Shalabh Aggarwal
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Preface 1. Part 1: Flask Fundamentals
2. Chapter 1: Flask Configurations FREE CHAPTER 3. Chapter 2: Templating with Jinja 4. Chapter 3: Data Modeling in Flask 5. Chapter 4: Working with Views 6. Part 2: Flask Deep Dive
7. Chapter 5: Web Forms with WTForms 8. Chapter 6: Authenticating in Flask 9. Chapter 7: RESTful API Building 10. Chapter 8: Admin Interface for Flask Apps 11. Chapter 9: Internationalization and Localization 12. Part 3: Advanced Flask
13. Chapter 10: Debugging, Error Handling, and Testing 14. Chapter 11: Deployment and Post-Deployment 15. Chapter 12: Microservices and Containers 16. Chapter 13: GPT with Flask 17. Chapter 14: Additional Tips and Tricks 18. Index 19. Other Books You May Enjoy

Creating a modular web app with blueprints

A blueprint is a feature in Flask that helps make large applications modular. This keeps application dispatching simple by providing a central place to register all components in an application. A blueprint looks like an application object but is not an application. It also looks like a pluggable app or a smaller part of a bigger app, but it is not. A blueprint is a set of operations that can be registered on an application and represents how to construct or build an application. Another benefit is that it allows us to create reusable components between multiple applications.

Getting ready

In this recipe, we’ll take the application from the previous recipe, Composition of views and models, as a reference and modify it so that it works using blueprints.

How to do it...

The following is an example of a simple Hello World application using Blueprint. It will work like it did in the previous recipe but will be much more modular and extensible.

First, we will start with the following flask_app/my_app/__init__.py file:

from flask import Flask
from my_app.hello.views import hello
app = Flask(__name__)
app.register_blueprint(hello)

Next, we will add some code to the views file, my_app/hello/views.py, which should look as follows:

from flask import Blueprint
from my_app.hello.models import MESSAGES
hello = Blueprint('hello', __name__)
@hello.route('/')
@hello.route('/hello')
def hello_world():
    return MESSAGES['default']
@hello.route('/show/<key>')
def get_message(key):
    return MESSAGES.get(key) or "%s not found!" % key
@hello.route('/add/<key>/<message>')
def add_or_update_message(key, message):
    MESSAGES[key] = message
    return "%s Added/Updated" % key

We have now defined a blueprint in the flask_app/my_app/hello/views.py file. We no longer need the application object in this file, and our complete routing is defined on a blueprint named hello. Instead of @app.route, we use @hello.route. The same blueprint is imported into flask_app/my_app/__init__.py and registered on the application object.

We can create any number of blueprints in our application and complete most of the activities that we would usually do, such as providing different template paths or different static paths. We can even have different URL prefixes or subdomains for our blueprints.

How it works...

This application will work in just the same way as the last application. The only difference is in the way the code is organized.

You have been reading a chapter from
Flask Framework Cookbook - Third Edition
Published in: Jul 2023
Publisher: Packt
ISBN-13: 9781804611104
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image