Index_blueprint = Blueprint('index', index():Īpplication. Views/index.py from flask import Blueprint, render_template Make it easier for others to maintain the application.įolder Structure of Large Application /app A basic flask blueprints exampleĪ minimal Flask application looks something like this: from flask import FlaskĪ large Flask application can separate one file into multiple files by blueprints. In this article we will learn how to use Blueprint to structure a Flask project. You can reuse Blueprint applications however you cannot run a blueprint on its own as it has to be registered on your main application. Flask Blueprint or Blueprint for short, is a way to organize the related code base between python modules or packages. They make administration of very large Flask applications easier and as such can be used to scale Flask applications. There are many other features of blueprints that need to be considered when nesting that make a general implementation significantly more complicated. Is there any way to group them in a more hierarchical manner, like how the templates go under the 'blueprint1/photos' sub-directory? Of course I can put all the photo views in their own module to keep them organized separately, but what if I want to change the parent 'blueprint1/photos' path to something else? I'm sure I can invent a function or decorator that groups related routes under the same root path, but then I still have to name all the functions with the photos_ prefix and reference them like url_for('blueprint1.photos_show') It seems like blueprints are the answer when a Flask app gets large and you need to group and compartmentalize similar parts together, but you cannot do the same thing when the blueprints themselves get large.įor reference, in Laravel you can group related "views" under a Controller class where the views are methods.Blueprints are a powerful concept in Flask application development that allow for flask applications to be more modular and be able to follow multiple patterns. Adding nestable blueprints into Flask is not as trivial as automatically appending a prefix to routes. The problem here is that all the views related to the photos section of blueprint1 are located at the "top level," right with maybe blueprints for videos or audio or whatever (named videos_index().). Rather it is a blueprint of how to construct or extend an application. A Blueprint object works similarly to a Flask application object, but it is not actually an application. Return render_template('photos/show.html', methods=) Blueprints can greatly simplify how large applications work and provide a central means for Flask extensions to register operations on applications. I can't think of a better way of doing it than: # app/blueprints/blueprint1/_init_.pyīlueprint1 = Blueprint('blueprint1', _name_, photos_index(): But what if I need to group routes and logic further under a blueprint?įor example, I have blueprint1 with url_prefix='/blueprint1' and maybe under that I want to have a collection of views revolving around a user sharing photos and other users commenting on them. I think this is pretty much exactly what blueprints are for. My app is divided into several mostly disparate parts that share things like users/sessions/security and base template and everything but mostly do not interact much, and should be routed under different paths like /part1/. I'm still new to Flask, so there may be an obvious way to accomplish this, but I haven't been able to figure it out so far from the documentation.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |