Pass function and arguments from node.js to Python
Prerequisites: How to run python scripts in node.js using the child_process module.
In this article, we are going to learn how to pass functions and arguments from node.js to Python using child_process.
Although Node.js is one of the most widely used web development frameworks, it lacks machine learning, deep learning, and artificial intelligence libraries. Python, fortunately, supports all of these and many more. Fortunately, those who are unfamiliar with the Django Framework but who utilize the Node.js framework can run Python using the child_process module for Node.js.
As you might be aware, what child_process basically does is sort of under the hood run the command you provide it with, along with the command line arguments. We can use this very fact to pass functions and arguments from node.js to python.
Syntax: python3 -c <command>
So basically there is a -c option that can be used to directly run commands provided as a string without having to create a python file.
python3 -c "print('Hello World');"
Step 1: Here we have created a file name compliment.py, whose job is to output a gender specific compliment as soon as a call to compliment.giveMe() is made. It takes a Boolean argument isHe which is used to identify the gender of the person asking for the compliment. The following is the code:
Step 2: Here, we have created a file named index.js which is used to provide the Boolean argument to call the function compliment.giveMe() from node.js. to run the code.