Rajendran bala
Rajendran bala

Reputation: 87

How to run python program with node js

I have an Express Node.js application, but I want to run a machine learning python codw with a following command.

I already find some online solutions to run python machine learning code.With this stackoverflow link How to call a Python function from Node.js

But with this solution I just run run a python code.

var spawn = require("child_process").spawn;
      var process = spawn('python', ["./my_script.py"]);
      process.stdout.on('data', function (data) {
        res.send(data.toString());
      })

With this code I just run a code python hello.py .But I want to run a code with following commands python test.py --model dataset/test.model --image s.jpg

Upvotes: 1

Views: 273

Answers (1)

Shay Moshe
Shay Moshe

Reputation: 482

You can use python-shell

import {PythonShell} from 'python-shell';

const options = {
            args: [
                '--image',
                's.jpg'
            ]
        };

PythonShell.run('script.py', options, (err, results) => {
  // your code 
});

If you want to use spawn you can do somthing like this:

const args = ['script.py', '--image', 's.jpg'];
const process = spawn('python', args);

Upvotes: 1

Related Questions