0

I am trying to split up my app that uses Flask, Blueprint and REST in different components. But I cannot get the api to work.

The structure is as follows:

restml
|-> __init__.py
|-> app.py
\-> resources
    |-> __init__.py
    \-> hello.py

My restml/__init__.py is:

from flask import Flask
from flask_restful import Api

from restml.resources import hello

app = Flask(__name__)


app.register_blueprint(hello.blueprint)

if __name__ == '__main__':
    app.run(debug=True)

My restml/resources/hello.py is:

from flask_restful import Api, Resource, url_for
from flask import Blueprint

blueprint = Blueprint('hello', __name__)
api = Api()

class Hello(Resource):
    def get(self):
        return {
           "hello world"
        }

api.add_resource(Hello, '/api/hello', endpoint='hello')

When I run the the app everything starts but curl fails to retrieve from the url http://localhost/api/hello.

How should I adjust the files so that the REST api can be found?

Update

After adding

import restml.resources.hello import blueprint

I get the following error

web_1    | Traceback (most recent call last):
web_1    |   File "/usr/local/lib/python3.5/site-packages/gunicorn/arbiter.py", line 557, in spawn_worker
web_1    |     worker.init_process()
web_1    |   File "/usr/local/lib/python3.5/site-packages/gunicorn/workers/base.py", line 126, in init_process
web_1    |     self.load_wsgi()
web_1    |   File "/usr/local/lib/python3.5/site-packages/gunicorn/workers/base.py", line 136, in load_wsgi
web_1    |     self.wsgi = self.app.wsgi()
web_1    |   File "/usr/local/lib/python3.5/site-packages/gunicorn/app/base.py", line 67, in wsgi
web_1    |     self.callable = self.load()
web_1    |   File "/usr/local/lib/python3.5/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
web_1    |     return self.load_wsgiapp()
web_1    |   File "/usr/local/lib/python3.5/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
web_1    |     return util.import_app(self.app_uri)
web_1    |   File "/usr/local/lib/python3.5/site-packages/gunicorn/util.py", line 357, in import_app
web_1    |     __import__(module)
web_1    |   File "/usr/src/app/restml/__init__.py", line 4, in <module>
web_1    |     from web.restml.resources.hello import blueprint
web_1    | ImportError: No module named 'web'
Stereo
  • 1,148
  • 13
  • 36

1 Answers1

2

You need to import your restml/resources/hello.py file in restml/__init__.py

from restml/resources/hello.py import blueprint

Should work.

Ani
  • 710
  • 7
  • 19
  • I am now getting the following error, `ImportError: No module named 'web'` – Stereo Oct 21 '16 at 10:16
  • You need python-webpy package. – Ani Oct 21 '16 at 10:42
  • I am new to web frameworks, but isn't that a competitor of `flask`? – Stereo Oct 21 '16 at 10:45
  • Yes. It's a different framework. Web.py But there wouldn't be any reason for there to show that error otherwise. Could you show a more detail view of your error logs to help better debug. – Ani Oct 21 '16 at 10:47
  • done, would be great if you can spot what goes wrong. – Stereo Oct 21 '16 at 11:04
  • ```from web.restml.resources.hello import blueprint``` This line.. Change to ```from restml.resources.hello import blueprint``` – Ani Oct 21 '16 at 11:09
  • sweet! I got confused because the files all reside within the web parent directory. – Stereo Oct 21 '16 at 11:11