我可以从一个简单的脚本成功加载TextBlob模块,但不能从Flask应用程序加载。我将向您展示代码和错误。
在一个简单的脚本中加载:
from textblob import TextBlob
text = 'purfect kitten'
blob = TextBlob(text)
print blob.correct()
从Flask app加载会引发错误:
from textblob import TextBlob
from flask import (
Flask,
request
)
app = Flask(__name__)
@app.route('/parse', methods=['GET', 'POST'])
def parse():
b = TextBlob(request.json['text'])
b.correct()
return b
if __name__ == '__main__':
print 'running app on port 5000'
app.debug = True
app.run()
错误:
127.0.0.1 - - [18/Nov/2015 14:54:25] "POST /parse HTTP/1.1" 500 - Traceback (most recent call last): File "/Users/peter/if/root/venv/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__ return self.wsgi_app(environ, start_response) File "/Users/peter/if/root/venv/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app response = self.make_response(self.handle_exception(e)) File "/Users/peter/if/root/venv/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception reraise(exc_type, exc_value, tb) File "/Users/peter/if/root/venv/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app response = self.full_dispatch_request() File "/Users/peter/if/root/venv/lib/python2.7/site-packages/flask/app.py", line 1478, in full_dispatch_request response = self.make_response(rv) File "/Users/peter/if/root/venv/lib/python2.7/site-packages/flask/app.py", line 1577, in make_response rv = self.response_class.force_type(rv, request.environ) File "/Users/peter/if/root/venv/lib/python2.7/site-packages/werkzeug/wrappers.py", line 841, in force_type response = BaseResponse(*_run_wsgi_app(response, environ)) File "/Users/peter/if/root/venv/lib/python2.7/site-packages/werkzeug/wrappers.py", line 57, in _run_wsgi_app return _run_wsgi_app(*args) File "/Users/peter/if/root/venv/lib/python2.7/site-packages/werkzeug/test.py", line 867, in run_wsgi_app app_rv = app(environ, start_response) TypeError: 'TextBlob' object is not callable
我正在使用与pip一起安装的textblob版本0.11.0。
答案 0 :(得分:2)
TextBlob
工作正常。这就是您在TextBlob
应用中Flask
使用问题的方法。
Flask的app.route
要求返回Response
或str
,但您的归档b
是TextBlob
。您需要将b
作为str
@app.route('/parse', methods=['GET', 'POST'])
def parse():
b = TextBlob('hello world')
b.correct()
return str(b)