I followed the official docs of flask-RESTful
and I'm trying to implement the first hello world demo.
At first, I put all the example code in a single file and every thing works fine.
However, when I split the code in three separated files (trying to make the project more structured), I always got a 404 error.
file stucture
.
├── app.py
├── app
├── __init__.py
├── __api__.py
└── venv
__init__.py
# -*- coding: utf-8 -*-
#initialization
from flask import Flask
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
api.py
# -*- coding: utf-8 -*-
from app import app, api
from flask_restful import Resource, Api
class HelloWorld(Resource):
def get(self):
return {'hello': 'world'}
api.add_resource(HelloWorld, '/')
app.py
# -*- coding: utf-8 -*-
from app import app
if __name__ == '__main__':
app.run(debug=True)
In python console:
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger pin code: 250-643-552
127.0.0.1 - - [03/May/2016 22:35:20] "GET / HTTP/1.1" 404 -
127.0.0.1 - - [03/May/2016 22:35:24] "GET / HTTP/1.1" 404 -
127.0.0.1 - - [03/May/2016 22:38:15] "GET / HTTP/1.1" 404 -
What's wrong?
EDIT
I can now get the expected result by move api.add_resource(HelloWorld, '/')
to app.py
app.py (edited)
# -*- coding: utf-8 -*-
from app import app, api
from app.api import HelloWorld
api.add_resource(HelloWorld, '/')
if __name__ == '__main__':
app.run(debug=True)
Can't figure out why?