CoderMJ
CoderMJ

Reputation: 17

Send JavaScript variable as input to python code

In JavaScript code declared a variable, that variable should be passed as input to python child process which is called inside JavaScript code, where python code executes its output by taking input from JavaScript variable

JavaScript code

    var input="onepiece";
    var spawn = require("child_process").spawn;
    var python_process = spawn('python3',["/home/centos/python.py",input] );
    python_process.stderr.on('data',function(data) {
    console.log(data.toString());
    });

import sys
input1=sys.argv[1]
print(input1)
count=0;
for i in range(0, len(input1)):
    if(input1[i] != ' '):
        count = count + 1;
print(  str(count));

when I execute javscript code i should get onepiece 8

Upvotes: 0

Views: 284

Answers (1)

Martin Tovmassian
Martin Tovmassian

Reputation: 1468

Made it work by connecting the stream listener to stdout rather than stderr:

python_process.stdout.on('data',function(data) {
   console.log(data.toString());
});

Upvotes: 1

Related Questions