[英]getting error 'function' object has no attribute 'as_view' while trying to run flask app
I started writing flask app after a long time more than a year, guess I have forgot something.经过一年多的时间,我开始写 flask 应用程序,我想我忘记了什么。 This below code results in an error:
下面的代码会导致错误:
from flask import Flask
from flask import jsonify
from flask_restplus import Resource, Api
from home_iot.config import reader
from download_audio.ydla import download
app = Flask(__name__)
_api = Api(app, catch_all_404s=True, version=0.1,
title="REST HTTP API's Gateway",
descrition="REST API gateway")
api_ns = _api.namespace("iot", description="API.")
@api_ns.route("/tcpserver", methods=["GET"])
def advertise_tcpserver():
ip = reader.get_server_ip()
return jsonify({"tcpserver": ip})
if __name__ == "__main__":
app.run(host='127.0.0.1')
Error is:错误是:
$ python app.py $ python app.py
Traceback (most recent call last):
File "app.py", line 29, in <module>
@api_ns.route("/tcpserver", methods=["GET"])
File "/Users/ciasto/pyenvs/flaskrestplusiot/lib/python2.7/site-packages/flask_restplus/namespace.py", line 98, in wrapper
self.add_resource(cls, *urls, **kwargs)
File "/Users/ciasto/pyenvs/flaskrestplusiot/lib/python2.7/site-packages/flask_restplus/namespace.py", line 87, in add_resource
api.register_resource(self, resource, *ns_urls, **kwargs)
File "/Users/ciasto/pyenvs/flaskrestplusiot/lib/python2.7/site-packages/flask_restplus/api.py", line 264, in register_resource
self._register_view(self.app, resource, namespace, *urls, **kwargs)
File "/Users/ciasto/pyenvs/flaskrestplusiot/lib/python2.7/site-packages/flask_restplus/api.py", line 287, in _register_view
resource_func = self.output(resource.as_view(endpoint, self, *resource_class_args,
AttributeError: 'function' object has no attribute 'as_view'
Don't think that's the correct way to define the namespace with flask_restplus
.不要认为这是用
flask_restplus
定义命名空间的正确方法。 Have a look at scaling docs .看看缩放文档。
You're probably looking for something like:您可能正在寻找类似的东西:
iot.py物联网
from flask_restplus import Namespace
api_ns = Namespace("iot", description="API.")
@api_ns.route("/tcpserver", methods=["GET"])
def advertise_tcpserver():
ip = reader.get_server_ip()
return jsonify({"tcpserver": ip})
Then in your main app.py :然后在你的主app.py 中:
# other imports
from .iot import api_ns
app = Flask(__name__)
_api = Api(app, catch_all_404s=True, version=0.1,
title="REST HTTP API's Gateway",
descrition="REST API gateway")
_api.add_namespace(api_ns, path='/some/prefix')
Also you appear to be using Python 2.7 which has been discontinued.此外,您似乎正在使用已停产的 Python 2.7。 I'd suggest upgrading to the latest version, using either a virutal environment or docker so as not to mess with your system's python.
我建议使用虚拟环境或 docker 升级到最新版本,以免弄乱系统的 python。
Hope this can helps those who have this same error and have not found the solution希望这可以帮助那些有同样错误但没有找到解决方案的人
To complete the answer given by @v25 you must provide ressources to your namespace by inheriting from Ressource class in flask_restplus.要完成@v25给出的答案,您必须通过从 flask_restplus 中的 Ressource class 继承来为您的命名空间提供资源。
The following example works for me以下示例适用于我
Environment:环境:
python requirements: python 要求:
Source code: iot.py源代码:iot.py
from flask_restplus import Namespace,Resource
api_ns = Namespace("iot", description="API.")
@api_ns.route("/tcpserver")
class AdvertiseTcpserver(Resource):
def get(self):
#TODO return the correct ip value
return {"tcpserver": "ip"}
app.py应用程序.py
from .iot import api_ns
from flask import Flask
from flask_restplus import Api
app = Flask(__name__)
_api = Api(app, catch_all_404s=True, version=0.1,
title="REST HTTP API's Gateway",
descrition="REST API gateway")
_api.add_namespace(api_ns, path='/some/prefix')
app.run()
Test command:测试命令:
#!/bin/sh
wget localhost:5000/some/prefix/tcpserver
Please let me know if this helped.请让我知道这是否有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.