In either case, even if there’s no common behavior it could be nice if only to categorize and group up related code. This is where our common behavior will live in our example API use case but if you’re not building an API you can do similar things.įor example maybe you have an admin parent Blueprint with a bunch of child Blueprints or you have an about parent Blueprint with us and policies as child Blueprints. That’s now a responsibility of the parent Blueprint. This pattern is nice because it avoids circular dependencies and it also sets the stage that your main app factory function doesn’t need to be updated as you add new API endpoints. Instead we register the parent Blueprint and then we register all child Blueprints to the parent within the parent’s views file.įor example you can imagine the api_v1 Blueprint having many different child Blueprints such as users, todos or whatever API endpoints your app has. The main takeaway here is our app factory function won’t be registering each individual child Blueprint. # Import our parent Blueprint (we'll create this next): from 1 import api_v1 def create_app ( settings_override = None ): #. Let’s start with the code and a few comments and then I’ll add more details afterwards. I’m mainly going to show the new lines or files that were added to the example app with a brief summary so you can use it as a reference. We’re going to use my example Flask / Docker app so we have a baseline app to work with if you want to follow along but nothing here is specific to Docker. Got it, let’s go over a real example of using them in a multi-file project that uses the Flask app factory pattern and write tests too. Maybe you’ve encountered circular dependency errors, other issues or the docs weren’t quite enough to get you going. So you’ve decided nested blueprints are what you need and now you want to use them. Quick Jump: Create App Function | Parent Blueprint (API v1) | Child Blueprint (Todos) | Testing It Out | Demo Video Updated on May 30th, 2023 in #flask Flask Nested Blueprints Example They're handy for adding common behavior to admin pages, versioned APIs, etc.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |