How to call a Python function from Node.js How to call a Python function from Node.js python python

How to call a Python function from Node.js


Easiest way I know of is to use "child_process" package which comes packaged with node.

Then you can do something like:

const spawn = require("child_process").spawn;const pythonProcess = spawn('python',["path/to/script.py", arg1, arg2, ...]);

Then all you have to do is make sure that you import sys in your python script, and then you can access arg1 using sys.argv[1], arg2 using sys.argv[2], and so on.

To send data back to node just do the following in the python script:

print(dataToSendBack)sys.stdout.flush()

And then node can listen for data using:

pythonProcess.stdout.on('data', (data) => {    // Do something with the data returned from python script});

Since this allows multiple arguments to be passed to a script using spawn, you can restructure a python script so that one of the arguments decides which function to call, and the other argument gets passed to that function, etc.

Hope this was clear. Let me know if something needs clarification.


Example for people who are from Python background and want to integrate their machine learning model in the Node.js application:

It uses the child_process core module:

const express = require('express')const app = express()app.get('/', (req, res) => {    const { spawn } = require('child_process');    const pyProg = spawn('python', ['./../pypy.py']);    pyProg.stdout.on('data', function(data) {        console.log(data.toString());        res.write(data);        res.end('end');    });})app.listen(4000, () => console.log('Application listening on port 4000!'))

It doesn't require sys module in your Python script.

Below is a more modular way of performing the task using Promise:

const express = require('express')const app = express()let runPy = new Promise(function(success, nosuccess) {    const { spawn } = require('child_process');    const pyprog = spawn('python', ['./../pypy.py']);    pyprog.stdout.on('data', function(data) {        success(data);    });    pyprog.stderr.on('data', (data) => {        nosuccess(data);    });});app.get('/', (req, res) => {    res.write('welcome\n');    runPy.then(function(fromRunpy) {        console.log(fromRunpy.toString());        res.end(fromRunpy);    });})app.listen(4000, () => console.log('Application listening on port 4000!'))


The python-shell module by extrabacon is a simple way to run Python scripts from Node.js with basic, but efficient inter-process communication and better error handling.

Installation:

With npm:npm install python-shell.

Or with yarn:yarn add python-shell

Running a simple Python script:

const PythonShell = require('python-shell').PythonShell;PythonShell.run('my_script.py', null, function (err) {  if (err) throw err;  console.log('finished');});

Running a Python script with arguments and options:

const PythonShell = require('python-shell').PythonShell;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);});

For the full documentation and source code, check out https://github.com/extrabacon/python-shell


matomo