One day, you come up with a brilliant idea for your project using cool libraries in Python. But you just do not know how to integrate it with your existing Node.js server. No worries! Today, I will show you how to read Python scripts from Node.js with ease and provide you a step-by-step guide for implementation so that you can save your time for more important tasks later on.
To make it easy, I will use Glitch to demonstrate how you can make it work but feel free to apply it on other platforms that you like!
First, we need to create a server.js file as the server for our app.
Your server.js file
The idea here is simple, you call your Python script from Node.js using Child Process and spawn() (You don't need to know what they are in advance). Then Node.js will store the output of your Python script and you can do whatever you like with the variable after that. Therefore, we will need to add a new POST method to run our Python script when the form is submitted on the client side:
Add a new POST method to read Python script
Here are some processes that you need to know:
In the declaration of the variable
'python3'is the version of python that is required to run your libraries in your python script. Often,
pythonis sufficient. However, if the error occurs:
cannot import [your libraries]even though you have installed them, then you should consider using
python3and reinstall your libraries using
stderrwill return the error(s) of your Python script (if existed) to your console so that you can fix any of them before moving to the next part.
An example of stderr error
- When your script.py has run successfully without bugs, the output of your Python script will be stored in the variable
dataToSendand the server will return the line
child process exited with code 0. Then, you can use the output variable in Node.js to do whatever you want.
Now, let's say you have a script.py file that prints Hello World to the console.
Your script.py code
Note that sys is the module that allows Python to read parameters from your Node.js server, we will pass parameters from server to Python in the next examples. If nothing wrong happens, you will receive this message from the console after running your script.py using Node.js:
child process exited with code 0, Hello World!.
Then, let's see how we can pass a parameter or more to script.py from our server. Just simply add some new variables next to your script's path in server.js and Python will automatically understand these are parameters and read them by order using
sys.argv[*your_index*] (that's why we need to
import sys earlier).
Update your server.js
Now, let's say we want to say “Hello” + the name of the person as a parameter from Node.js server. We will edit the Python source to the following:
Your new script.py
Here is what we will get after running the code successfully:
child process exited with code 0, Hello Duyen!.
Congrats, you have learned how to pass a parameter from Node.js to a Python file. You can add even more params and increase the index of
sys.argv to get new values.
Finally, I will show you a little trick that no one has ever told you so that your Node.js file only reads a necessary part of your Python code instead of the whole file. That trick is… calling functions from your server as a parameter!
Suppose that you want to greet a person from three different ways instead of just saying “hello”. To do so, just create a function for each greeting styles and pass a function's name as the first parameter in your server. By using this method, **you don't need to create multiple Python files **and different POST methods to get the values that you want for your web application.
Create functions in your script.py
As you can see, when we call script.py from server.js now, it will only run the function that you pass through as the first parameter from Node.js. Therefore, this method will **help you organize your Python script better **and only use the resources that you need!
Then, the last thing you need to do is changing the declaration of the variable python in server.js to:
const python = spawn('python3', ['public/script.py', 'welcome', 'Duyen']);
You have done a great job. When the program runs successfully, you will expect this output:
child process exited with code 0, Welcome, Duyen!.
Here is the GUI for what we have done above: https://glitch.com/~nodewithpython
To access the full code, click the link here.
If you have any questions or cannot fix your bugs while following my tutorial, you can talk to me directly by leaving a comment below. I will try to help as soon as I can!
Happy coding and wish you have a nice day!