marshmallow-code / flask-smorest

DB agnostic framework to build auto-documented REST APIs with Flask and marshmallow
https://flask-smorest.readthedocs.io
MIT License
664 stars 73 forks source link

Fail to register blueprint #224

Closed ChenHaolinOlym closed 3 years ago

ChenHaolinOlym commented 3 years ago

I want to use init_app to initialize the application and I want to put all my api part of code into a separate .py file. So I do something below: interface.py:

from flask_smorest import Api
api = Api()
blp=Blueprint()
# Some settings
api.register_blueprint(blp)

__init__.py

from .interface import api
def create_app():
    app=Flask(__name__)
    api.init_app(app)

I would get an error: AttributeError: 'NoneType' object has no attribute 'register_blueprint' This is because I register blueprint before I init_app I really want to organize my code like this so can flask-smorest support this?

lafrech commented 3 years ago

Blueprints must be registered after app init (because the process depends on app parameters like OpenAPI version for the docs).

from flask_smorest import Api
api = Api()
blp=Blueprint()
# Some settings
def register_blueprints():
    api.register_blueprint(blp)
from .interface import api, register_blueprints
def create_app():
    app=Flask(__name__)
    api.init_app(app)
    register_blueprints()
ChenHaolinOlym commented 3 years ago

Thanks for your help