Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swagger docs not showing up when using gunicorn #781

Open
rafa-acioly opened this issue Feb 22, 2020 · 4 comments
Open

Swagger docs not showing up when using gunicorn #781

rafa-acioly opened this issue Feb 22, 2020 · 4 comments
Labels

Comments

@rafa-acioly
Copy link

rafa-acioly commented Feb 22, 2020

Code

# The run.py file

from decouple import config
from flask import Blueprint

from api import app, application
from api.routes import configure_routes


if __name__ == "__main__":
    blueprint = Blueprint('api', __name__, url_prefix='/api')
    configure_routes(application)
    app.register_blueprint(blueprint)

    app.run(
        host=config('HOST', default='0.0.0.0'),
        debug=config('DEBUG', default=False, cast=bool),
        port=config('PORT', default=5000, cast=int)
    )

Repro Steps (if applicable)

  1. run gunicorn -b localhost:8080 -w 4 run:app
  2. open localhost:8080/docs

Expected Behavior

When i start the app using python run.py the endpoints in docs url are correct.

Captura de Tela 2020-02-22 às 11 12 16

Actual Behavior

When i start the ap using gunicorn the docs url do not show the endpoints

Captura de Tela 2020-02-22 às 11 10 03

Environment

  • Python version: 3.7.2
  • Flask version: 1.1.1
  • Flask-RESTPlus version: 0.13.0
  • Flask-RESTful: 0.3.8
@hsweif
Copy link

hsweif commented Oct 15, 2020

Any update?

@andreykurilin
Copy link

Python interpreter uses __name__ variable to identify whether python file should be executed as a script or as a module. Everything under __name__ == "__main__" block will be executed only when you call app.py directly from the command line (i.e as a script).
When you are calling gunicorn to execute an application, it imports the app.py as a module and all your blueprint configuration is not applied.

@SmartManoj
Copy link

Not works even if it is outside

@andreykurilin
Copy link

Please check the following code of the simplified application that covers the whole workflow:

# filename app.py


import flask
import flask_restx


APP = flask.Flask("some-app")
api_bp = flask.Blueprint("v1_api", __name__, url_prefix="/api/v1")

API = flask_restx.Api(api_bp)
APP.register_blueprint(api_bp)


NAMESPACE = flask_restx.Namespace("info")


@NAMESPACE.route("/")
class InfoAPI(flask_restx.Resource):
    def get(self):
        return "something"


API.add_namespace(NAMESPACE)

Should be executed as gunicorn -b localhost:8080 -w 4 app:APP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants