Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor how to run server side python script

I have a Meteor app that needs to call a python script to do some stuff in the background. How can I get this to work? I've tried using child_process and exec, but I can't seem to get it to execute properly. Where should the script even be located?

Thanks

like image 916
Joshin Avatar asked Nov 01 '22 07:11

Joshin


1 Answers

I have same problems and its solved use python-sheel an npm packages to run Python scripts from Node.js Install on meteor :

meteor npm install --save python-shell

There is simple usage :

var PythonShell = require('python-shell');

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

If you want run with arguments and options :

var PythonShell = require('python-shell');

var options = {
  mode: 'text',
  pythonPath: 'path/to/python',
  pythonOptions: ['-u'],
  scriptPath: 'path/to/my/scripts',
  args: ['value1', 'value2', 'value3']
};

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

here detail about python-shell https://github.com/extrabacon/python-shell Thanks extrabacon :)

like image 86
Pamungkas Jayuda Avatar answered Nov 13 '22 12:11

Pamungkas Jayuda