independentanna.blogg.se

Flask blueprint injection
Flask blueprint injection







flask blueprint injection

Note: To use Blueprint-specific assets, you can use the Flask-Assets library. To print the blueprint name of the Jinja2 template that the current page belongs to, you can use. The admin_bp that we used above is the name we gave to our blueprint for internal routing while creating the object. Without Blueprints, in order to create links in your templates, you would use something similar to the below: My Linkīut with blueprints in place, now you can define your links as: My Link Similarly, you can register rest of your blueprints if you have more. app.register_blueprint(admin.admin_bp, url_prefix='/admin') One such is url_prefix that you may require. Additionally, you can pass other parameters to the method for more customization. To register the blueprint, we use the register_blueprint() method and pass the name of the blueprint. In the _init_.py file, we will create a Flask app and register our blueprints there: from flask import Flask But, will your app automatically know about this blueprint? No. Now you have a blueprint and a route registered to it. Instead of the usual we have used This is how you bind a route to a particular blueprint. In the above snippet, focus on the line where the route is defined.

flask blueprint injection

Now that you have created a blueprint for the admin related functionality, you can use it while creating routes for the admins. By defining the template_folder and static_folder parameters, you specify that you'll be using blueprint-specific templates and static files. The third and fourth parameters passed are optional keyword arguments. This helps locate the root_path for the blueprint. The second parameter is the name of the Blueprint package, generally _name_. This name will later be used for internal routing (we'll see below). While creating the object of the Blueprint class, the first parameter is the name you want to give to your Blueprint. Since Blueprint is a Flask built-in concept, you can import it from the Flask library. Let's define our very first blueprint for the admin functionality inside the admin/routes.py file: from flask import Blueprint Now that you understand what problems Blueprints solves, let's see how we can use Blueprints in our applications. The logic related to admin resides inside the admin folder, the logic related to products resides inside the products folder and so on.Īdditionally, we also separated the templates and static files in the subdirectories where they are required, so that irrelevant files don't load when going to a page that doesn't require them. Now you can see that you have clear separation of concerns. So, with Blueprints now your same application will look like this: /blueprint-tutorial In addition to that, you can store your templates and static files along with the logic in the same subdirectory. Blueprints help you structure your application by organizing the logic into subdirectories. Now, this is where Flask Blueprints come into picture. Let's look at the app.py file: from flask import Flaskĭoesn't that look messy (imagining that you've built a large-scale app)? You have all your models and different routes inside the same file spread around here and there. If you follow the Flask documentation to create a minimal application, your project structure will look similar to this: /myappĭoesn't the folder look so clean? All you have is an app.py file where you have all your logic for the application, a templates folder to store your HTML files, and a static folder to store your static files. In Flask, also, you can organize your applications using Blueprints, a built-in concept in Flask similar to Python modules. If you have worked with Django, you might have found your project divided into different modules. But it's not a good practice for a large-scale app.īy doing this, you are clearly violating the Single Responsibility Principle, where each piece of your application should handle just one responsibility. You will find a lot of tutorials that follow the same pattern. Sometimes you'll find developers dumping all of their logic into a single file called app.py. Flask is a simple, easy-to-use microframework for Python that can help you build scalable and secure web applications.









Flask blueprint injection