+

Run Python script from Node.js using child process spawn () method

Developers who are not familiar with the Django Framework but are using the Node JS Framework can also benefit from Python using a child process module for Node JS .

Child module Process for Node JS provides functionality to run scripts or commands in languages ​​other than JavaScript (such as Python). We can embed machine learning algorithms, deep learning algorithms, and many of the functionality provided through the Python library into a Node JS application. The child process allows us to run a Python script in a Node JS application and transfer / output data to / from the Python script.

  child_process.spawn ():  This method helps us to spawn child process asynchronously. 

Let`s create a simple Python script that accepts two command line arguments as name and surname then display them. Later, we will run this script from a Node JS application and display the output in a browser window.

Python script:

import sys

# Accepts first and last name via command
# string arguments and then display them

print ( "Output from Python" )

print ( "First name:" + sys.argv [ 1 ])

print ( " Last name: " + sys.argv [ 2 ])

  
# save script as hello.py

Host JS server code:

// import express JS module into the application
// and creates its own variable.

var express = require ( `express` ); 

var app = express (); 

 
// Creates a server that runs on port 3000 and
// can be accessed via localhost: 3000

app.listen ( 3000 , function () {

console.log ( `server running on port 3000` );

})

 
// The callName () function is executed whenever
// The URL is localhost: 3000 / name

app.get ( `/ name` , callName); 

 
function callName (req, res) {

 

// Use the method child_process.spawn from

// child_process module and assign it

// to the spawn variable

var spawn = require ( "child_process" ). spawn; 

 

// Parameters passed to spawn -

  // 1. type_of_script

// 2. a list containing the script path

// and arguments for the script

 

// For example: http:// localhost: 3000 / name? Firstname = Mike & amp; lastname = Will

// so first name = Mike and last name = Will

var process = spawn ( ` python` , [ "./ hello.py" ,

req.query.firstname,

req.query.lastname]); 

 

// takes stdout data from the script that executed

  // with arguments and send this data into a res object

process.stdout.on ( `data` , function (data) {

  res.send (data.toString ());

})

}

 
// save the code as start.js

After saving the Python script and server-side script code, run the code from its source folder by running the following command:

 node start. js 

The application can be accessed here:

 localhost: 3000 / name? firstname =  "Enter first name"  & amp; lastname =  "Enter last name"   For e g. :  localhost: 3000 / name? firstname = Ram & amp; lastname = Sharma 

Output:

Applications:

  1. This method can be used as an alternative to the REST API.
  2. This method can help our web application take advantage of special features in other languages ​​that are not currently available in javascript
  3. Machine learning modules can be implemented in Python and then used in the web -app using this method.

Link:

Get Solution for free from DataCamp guru