Chapter 82 – Segment Web App Structure Using Flask Blueprint
To make it done efficiently, I would walk through Flask blueprint for beginners and you can learn how to structure at the very early stage of Web App or DApp development, which streamline all work afterwards.
In most cases, we need to apply the same set of structure, content or data with just different language and localization settings. The familiar samples are subfolders, localization.
To make it done efficiently, I would walk through Flask blueprint for beginners and you can learn how to structure at the very early stage of Web App or DApp development, which streamline all work afterwards.
Table of Contents: Segment Web App Structure Using Flask Blueprint
- What is Flask Blueprint
- Some benefits of using Flask Blueprint
- Layer and Folder Structure
- Branch Blueprint Config
- Main app config
- Python Script Sample of Web App Structure Using Flask Blueprint (Including Database, user authentication using Blueprint)
What is Flask Blueprint
Flask Blueprint is a feature provided by the Flask web framework, which allows you to organize your Flask application into reusable components or modules. It helps you create modular and scalable applications by providing a way to define and group related views, templates, static files, and other application components.
A Flask Blueprint acts as a blueprint or a template for defining a set of routes and related functionalities. It provides a way to organize different parts of your application, such as routes, templates, static files, and error handlers, into logical units.
By using Flask Blueprints, you can divide your application into smaller, more manageable components. Each Blueprint can have its own set of routes and associated functions. These Blueprints can then be registered with the Flask application to make them accessible.
Some benefits of using Flask Blueprint include:
- Modular organization: Blueprints enable you to break down your application into smaller, reusable modules, making it easier to manage and maintain.
- Collaborative development: Multiple developers can work on different Blueprints simultaneously, allowing for better collaboration and separation of concerns
- URL prefixing: Blueprints can be assigned a URL prefix, making it easier to define routes with a common base URL.
- Template and static file inheritance: Blueprints can define their own templates and static files, which can be inherited by the main application or other Blueprints.
- Error handling: Blueprints can define their own error handlers, allowing for custom error handling within specific parts of the application.
Layer and Folder Structure
Compared to Blueprint structure, basically normal practice requires developers to add three must-have docs in the root directory, which are app.py, and html script in templates folder and rest of materials, CSS or JS in statics. Two folders and app,py is located in the root folder.
Conversely, when using Blueprint, please be sure any prefix layer or subfolder scripts can not be put into the root folder. Only keep app.py with create_app() function, so Flask server can detect the activation script correctly.
For each Blueprint, it requires the three must-have elements as mentioned above (static folder, templates folder and the branch script.
Branch Blueprint Config
Once the folders and paths are ready, we can start configuring the blueprint. On the branch side, we need to import flask and blueprint to create an object with optional modules and information. Here is the sample as follows:
From flask import Blueprint
JPSiteScript = Blueprint('japaneseSite', __name__, template_folder="templates", static_folder='static/assets', static_url_path='/static/assets')
Then, we can continue to set up the routes as follows using the japaneseSite instead of app without Blueprint
# Define routes and functions
@japaneseSite.route('/login')
def login():
return 'Login page'
Main app config
In the main app.py, we also need to tell Flask how many Blueprint we have and what they are. So here is a way to register each blueprint
From abc import JPSiteScript
app.register_blueprint(JPSiteScript, url_prefix='/jp')
I like to elaborate on the url_prefix particularly here as it’s a method to set up mulitp country site structure using it. We don’t need to set up one by one in each Blueprint. Url_prefix means all the routes from that specific Blueprint would use /jp as the subfolder in the URL path. Super convenient.
Python Script Sample of Web App Structure Using Flask Blueprint (Including Database integration, user authentication integration using Blueprint)
If you are interested in Chapter 82 – Segment Web App Structure Using Flask Blueprint, please subscribe to our newsletter by adding the message ‘Chapter 82+ Full scripts of Flask Blueprint settings. We would send you the script when the up-to-date app script is live.
I hope you enjoy reading Chapter 82 – Segment Web App Structure Using Flask Blueprint. If you did, please support us by doing one of the things listed below, because it always helps out our channel.
- Support and Donate to our channel through PayPal (paypal.me/Easy2digital)
- Subscribe to my channel and turn on the notification bell Easy2Digital Youtube channel.
- Follow and like my page Easy2Digital Facebook page
- Share the article on your social network with the hashtag #easy2digital
- You sign up for our weekly newsletter to receive Easy2Digital latest articles, videos, and discount codes
- Subscribe to our monthly membership through Patreon to enjoy exclusive benefits (www.patreon.com/louisludigital)