从Node API中的python脚本获取响应

时间:2018-06-05 06:10:27

标签: python node.js

我有一个python脚本,用于处理图像,从图像中读取和提取文本。现在我有一个要求,我必须从节点js调用该python脚本。我的问题是如何从节点中的python脚本获得响应?

Python脚本: -

response = image_to_dict('test.jpg', bucketname)
detections = dict_to_detections(response)
entities = list_to_entities(detections.lines)
print("Payer is {payerName}".format(payerName=extractPayer(entities.orgs, 
response)))
print("Member name is 
{memberName}".format(memberName=extractMemberName(entities.persons)))
print("Member ID is 
{memberId}".format(memberId=extractMemberId(detections.lines)))

节点js: -

    var PythonShell = require('python-shell');
 PythonShell.run('test.py', function (err) {
  if (err) throw err;
  console.log('finished');
});

现在我需要我在python脚本中打印的内容,例如付款人名称作为节点js中的响应。 任何输入都会很棒。 提前致谢

2 个答案:

答案 0 :(得分:0)

the doc中所述,您只需要在回调中提供另一个参数

PythonShell.run('script.py', function (err, results) {
   if (err) throw err;
   console.log(results);
});
  

运行Python脚本并使用结果调用回调。该   callback包含执行错误(如果有的话)以及一个数组   从Python脚本发出的消息。

答案 1 :(得分:0)

如果出现错误,您需要在函数中传递另一个参数,该参数将为您提供在shell脚本中打印的消息数组。

PythonShell.run('test.py', options, function (err, results) {
  if (err) throw err;
  // results is an array consisting of messages collected during execution
  console.log('results: %j', results);
});

更多:https://www.npmjs.com/package/python-shell