-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-shell.js
More file actions
36 lines (34 loc) · 1.17 KB
/
python-shell.js
File metadata and controls
36 lines (34 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
module.exports = function(RED) {
function PythonShellNode(config) {
RED.nodes.createNode(this,config);
this.name = config.name;
this.python = config.python;
this.pypath = config.pypath;
this.pyshellpath = config.pyshellpath;
var node = this;
node.on("input", function(msg) {
try {
var {PythonShell} = require(node.pyshellpath);
let options = {
pythonPath: node.pypath,
pythonOptions: ['-u']
};
PythonShell.runString(node.python, options, function (err, result) {
if (err) {
msg.payload = err;
} else {
if (result.length > 0) {
msg.payload = result[0];
} else {
msg.payload = "";
}
}
node.send(msg);
});
} catch(err) {
node.error(err.message);
}
});
}
RED.nodes.registerType("python-shell",PythonShellNode);
}