【发布时间】:2019-07-28 12:22:47
【问题描述】:
在 Python 中的 Flask REST API 路由中,返回类型是 list
@app.route('/ent', methods=['POST'])
def ent():
"""Get entities for displaCy ENT visualizer."""
json = request.get_json()
nlp = MODELS[json['model']]
doc = nlp(json['text'])
return [
{"start": ent.start_char, "end": ent.end_char, "label": ent.label_}
for ent in doc.ents
]
此错误:
TypeError: The view function did not return a valid response. The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a list.
如何让/ent 上的 API 路由正确返回 JSON 数组?
【问题讨论】:
-
您不能将列表更改为像元组这样的可散列类型,然后将其转换回列表吗?就这么简单。
标签: python python-3.x rest flask