私は決して専門家ではありませんが、最近、設計図で少し遊んでいます。
You can only register a blueprint on your flask app if the blueprint code has been imported into the current Python instance, for example:
from my_blueprint import blueprint_object
app.register_blueprint(blueprint_object, url_prefix='/my_blueprint')
meaning that all of the memory required for blueprint_object has been allocated, the __init__.py
file associated with my_blueprint
has been evaluated and everything is ready to go.
I experimented briefly with trying to load a blueprint in the app.before_first_request
method, but flask doesn't allow this, blueprints all need to be loaded before anything makes a request (and before_first_request
effectively runs just after that, but before any other code gets to play).
Blueprints are designed to provide extensible functionality to flask web-apps. I'm not sure why loading them in would be a waste of resources, unless you're only ever going to use a particular piece of functionality once, in which case, you might want to re-factor your application into a number of different apps that you can spin up as required.