#structureflask
Explore tagged Tumblr posts
Text
How to Organize Your Flask Apps Using Blueprints
Flask is a simple, easy-to-use micro web framework for Python that can help you build scalable and secure web applications.
There are occasions when programmers will place all of their logic in a single file named app.py. Many tutorials can be found online that use the same format. But for a big app, it's not a good approach.
You are obviously going against the Single Duty Principle by doing this, which states that each component of your application should only be handling one responsibility.
If you've used Django, your project may have been broken up into many modules. Additionally, you may organise your Flask applications using Blueprints, a Python module-like built-in idea.
How a Flask Application Appearance?
Your project structure will like this if you construct a simple application using the Flask documentation:
/myapp
└── app.py
Isn't the folder wonderfully organised? The only thing you have is an app.py file that contains all of the application functionality.
Let's look at the app.py file:
from flask import Flask
app = Flask(__name__)
# Some Models Here
# Routes related to Products Page
# Routes related to Admin Page
if __name__ == '__main__':
app.run(host=’0.0.0.0’,port=5000,debug=True)
When you imagine that you have created a large-scale programme, doesn't that look messy? You have scattered your models and different routes throughout the same file.
How Do Blueprints Address the Issue?
Flask Blueprints now come into play in this scenario. By grouping the logic into subdirectories, blueprints aid in the structure of your application. Additionally, you can put the logic and static files all in the same subdirectory.
Consequently, your similar application will now look like this because to Blueprints:
/blueprint-tutorial
├── /myapp_with_blueprints
│ ├── __init__.py
│ ├── /admin
│ │ └── routes.py
│ ├── /products
│ │ └── routes.py
├── app.py
You can see that your concerns are well separated at this point. The logic for admin is located in the admin folder, the logic for products is located in the products folder, and so on.
Use of Blueprints
Now that you understand what problems Blueprints solves, let's see how we can use Blueprints in our applications.
Defining a Blueprint
In the admin/routes.py file, let's define our very first blueprint for the admin functionality:
from flask import Blueprint
# Defining a blueprint
admin = Blueprint(
'admin', __name__,
template_folder='templates',
static_folder='static'
)
You can import Blueprint from the Flask library because it is a built-in Flask concept. The first parameter when creating an object of the Blueprint class is the name you wish to give your Blueprint. Internal routing will later use this name, as we'll see below.
The second option is the Blueprint package's name, which is often __name__. This aids in locating the blueprint's root path.
Optional keyword arguments are supplied as the third and fourth parameters. You declare that you'll use blueprint-specific templates and static files by defining the template folder and static folder options.
How to Create Blueprint Routes
You may now use the blueprint you developed for the admin-related functionality when establishing routes for the admins.
from flask import Blueprint
# Defining a blueprint
admin = Blueprint(
'admin', __name__,
)
@admin.route('/admin') # Focus here
def admin_home():
return "Hello Admin!"
Concentrate on the line where the route is specified in the given snippet. We have used @admin bp.route('...') rather than the typical @app.route('...'). This is how a route is connected to a certain blueprint.
Ways for Registering Your Blueprints
You currently have a route registered to a blueprint. But will your app be aware of this blueprint automatically?
So let's get going. We will construct a Flask app and register our blueprints there in the __init__.py file:
from flask import Flask
app = Flask(__name__)
from .admin.routes import admin
# Registering blueprints
app.register_blueprint(admin)
Using the register_blueprint() method and the blueprint's name, we register the blueprint. For even more customisation, you can also supply the procedure other parameters. One of these is url prefix, which you could need.
app.register_blueprint(admin, url_prefix='/admin')
Similar to that, if you have other blueprints, you can register them.
1 note
·
View note