Run Python script from Node.js using child process spawn() method
Node.js is one of the most adopted web development technologies but it lacks support for machine learning, deep learning and artificial intelligence libraries. Luckily, Python supports all these and many more other features. Django Framework for Python can utilize this functionality of Python and can provide support for building new age web application using machine learning and Artificial Intelligence.
For those developers who are not familiar with Django Framework but use Node JS framework can also benefit from Python using child process module for Node JS.
child_process.spawn(): This method helps us to spawn child process asynchronously.
Let’s create a simple Python script that will take two command line arguments as a first name and last name then display them. Later we’ll run that script from Node JS application and display output in the browser window.
Python script :
Node JS server code :
After saving the Python script and server script code, run the code from its source folder by following command :
Access the application through link :
localhost:3000/name?firstname="Enter first name"&lastname="Enter last name" For e g. : localhost:3000/name?firstname=Ram&lastname=Sharma
- This method can be used in alternative to REST-APIs.
- Machine learning modules can be implemented in Python and then utilize them in web app using this method.
Please Login to comment...