Search code examples
pythonflaskswagger

TypeError: 'dict' object is not callable on flask-swagger


Im trying to set up swagger docs with a flask API im using and keep getting the error

TypeError: 'dict' object is not callable

I have set up as so :

My wsgi.py:

from application import init_app
from flask_swagger_ui import get_swaggerui_blueprint
from flask_swagger import swagger

app = init_app()
swagger = swagger(app)


@app.route('/swagger')
def get_swagger():
    swag = swagger(app)
    return swag


# Swagger UI route
SWAGGER_URL = '/swagger-ui'
API_URL = '/swagger'
swaggerui_blueprint = get_swaggerui_blueprint(
    SWAGGER_URL,
    API_URL,
    config={
        'app_name': "TAGR API"
    }
)
app.register_blueprint(swaggerui_blueprint, url_prefix=SWAGGER_URL)

if __name__ == "__main__":
    app.run(host='0.0.0.0', debug=True)

and then I have an init.py that creates the app flask object in a application folder where I register blueprints and such

def init_app():
    app = Flask(__name__, instance_relative_config=False)
    app.config.from_object(Config)

        with app.app_context():
        # Blueprints
        from application.users import users_blueprint
        app.register_blueprint(answers_blueprint)

    return app

here is the full traceback

Traceback (most recent call last):
  File "C:\Users\graha\Documents\GitHub\tagr-api-py\venv\Lib\site-packages\flask\app.py", line 1478, in __call__
    return self.wsgi_app(environ, start_response)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\graha\Documents\GitHub\tagr-api-py\venv\Lib\site-packages\flask\app.py", line 1458, in wsgi_app
    response = self.handle_exception(e)
               ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\graha\Documents\GitHub\tagr-api-py\venv\Lib\site-packages\flask_cors\extension.py", line 176, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
                                                ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\graha\Documents\GitHub\tagr-api-py\venv\Lib\site-packages\flask\app.py", line 1455, in wsgi_app
    response = self.full_dispatch_request()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\graha\Documents\GitHub\tagr-api-py\venv\Lib\site-packages\flask\app.py", line 869, in full_dispatch_request
    rv = self.handle_user_exception(e)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\graha\Documents\GitHub\tagr-api-py\venv\Lib\site-packages\flask_cors\extension.py", line 176, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
                                                ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\graha\Documents\GitHub\tagr-api-py\venv\Lib\site-packages\flask\app.py", line 867, in full_dispatch_request
    rv = self.dispatch_request()
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\graha\Documents\GitHub\tagr-api-py\venv\Lib\site-packages\flask\app.py", line 852, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\graha\Documents\GitHub\tagr-api-py\wsgi.py", line 11, in get_swagger
    swag = swagger(app)
           ^^^^^^^^^^^^


Solution

  • Have a look at this code snippet and you'll find the error yourself:

    from flask_swagger import swagger
    # first swagger
    
    app = init_app()
    swagger = swagger(app)
    # second "swagger"
    
    @app.route('/swagger')
    def get_swagger():
        swag = swagger(app)
        # third swagger
        return swag
    

    Hint: you have overwritten swagger twice. Choose another variable name and you'll be fine.