Search code examples
javascriptpythonnode.jsspawn

JavaScript Spawn: How to pass variable to FLAG in Spawn python script


I have a python script which has two FLAGs --server and --image.

For now, in JavaScript, I can only assign the fixed value to FLAGS using spawn. For example: (This does yield the output)

var pyProg = spawn('python', ['./MLmodel/inception_client.py', '--server=30.220.240.190:9000', '--image=./testImage/DSC00917.JPG']);

pyProg.stdout.on('data', function (data) { console.log('This is result ' + data.toString());});

However, I want to assign a string variable and pass the string to the FLAG. For example: (This is wrong, it does not yield any output)

var imagePath = './testImage/DSC00917.JPG'

var pyProg = spawn('python', ['./MLmodel/inception_client.py', '--server=30.220.240.190:9000', '--image=imagePath']);

pyProg.stdout.on('data', function (data) { console.log('This is result ' + data.toString());});

How should I make it work? Thank you in advance!


Solution

  • You use string concatenation as you would anywhere else in JavaScript. If you would want console.log to print a variable you would do it like this:

    console.log('image path is ' + imagePath);
    

    or if you are using ES6 string interpolation:

    console.log(`image path is ${imagePath}`);
    

    The same works for your code example:

    var imagePath = './testImage/DSC00917.JPG'
    var pyProg = spawn('python', ['./MLmodel/inception_client.py', '--server=30.220.240.190:9000', '--image=' + imagePath]);