How do I integrate with Python Scripts?
1. Use the following code in your Viabl.ai Platform knowledge-base
let result = xpertrule.onceAsync(function(completeCB) {
let script = "../../_scripts/test.py";
let inputObject = {
name: "Mr Magoo",
age: 42,
mugshot: "http://images.com/xyz123.png"
};
const child_process = require("child_process");
let result = child_process.spawnSync("python", [script, JSON.stringify(inputObject)], {encoding: "utf8", cwd: __dirname});
if (result.stderr) {
completeCB({ok: false, error: result.stderr});
} else if (result.error) {
completeCB({ok: false, error: JSON.stringify(result.error)});
} else {
completeCB({ok: true, data: result.stdout});
}
});
#python_result.val(JSON.stringify(result));
2. Include the following code in your Python script (test.py) on the server
import sys
import json
inputObject = json.loads(sys.argv[1])
inputObject['age'] = inputObject['age'] + 1
print json.dumps(inputObject)
sys.stdout.flush()