2

I have a Flask app which has a Flask-RestPlus API as well as a "/" route. When I try to access "/" however, I get a 404. If I remove the Flask-RestPlus extension, the route works. How do I make both parts work together?

from flask import Flask
from flask_restplus import Api

app = Flask(__name__)
api = Api(app, doc="/doc/")  # Removing this makes / work

@app.route("/")
def index():
    return "foobar"
davidism
  • 121,510
  • 29
  • 395
  • 339
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958

2 Answers2

4

This is an open issue in Flask-RestPlus. As described in this comment on that issue, changing the order of the route and Api solves the issue.

from flask import Flask
from flask_restplus import Api

app = Flask(__name__)

@app.route("/")
def index():
    return "foobar"

api = Api(app, doc="/doc/")
davidism
  • 121,510
  • 29
  • 395
  • 339
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
0

flask-restplus defines a different way of assigning routes according to their docs:

@api.route('/')
class Home(Resource):
    def get(self):
        return {'hello': 'world'}

Notice that the api variable is used instead of the app. Moreover, a class is used although I am not 100% sure it is required.

AdamGold
  • 4,941
  • 4
  • 29
  • 47
  • But I don't want the main page to contain the API. I want the main page to use `render_template` (or, to keep it simple and minimal here - "foobar") – Martin Thoma Jun 11 '19 at 08:45
  • I see. I think you should mention it in your question. Anyway, have you tested the following? https://stackoverflow.com/a/44780748/695377 – AdamGold Jun 11 '19 at 08:47