Introduction
This is the English version of day 22 of the Node-RED Advent Calendar 2024.
Japanese Version:
Last year, I wrote about creating nodes for the Node-RED MCU.
This year, I’ve also been working on developing several new nodes. Among them, I focused especially on the development of the python-venv node. As I’ll explain later, this node allows you to execute Python scripts in a Python virtual environment.
When I first developed the node, I didn’t use Python much myself. However, I now use Python with the python-venv node, interact with people worldwide, and improve the node through ongoing development.
In this article, I’ll cover an overview of the node, its development history, and some use cases.
▼GitHub repository is here.
▼The page in the Node-RED flow library is here
Node Mechanism
Overview
This node creates a Python virtual environment, installs packages into it, and executes Python code.
When you install node-red-contrib-python-venv, the following nodes are added: the venv node, the pip node, and the venv-exec node.
You can write and execute Python code in the venv node as shown below. It’s like a Python version of the function node in Node-RED.
In the image below, the pip packages in the virtual environment are different from those in the system pip. This gives the node a unique level of independence.
The pip node allows you to document the package installation process. By sharing the flow, you can reconstruct the Python execution environment.
The internal workings of the node primarily rely on the following mechanisms:
- Specifying paths within the Python virtual environment to utilize it.
- Retrieving absolute paths of the code to be executed.
- Running preinstall scripts via the npm install command.
Python Virtual Environment
While developing desktop applications with Electron, I wanted to use a Python virtual environment without executing the activate script.
After exploring the virtual environment structure and official documentation, I realized that specifying paths would suffice. This mechanism is employed in the node.
Related Article:Pythonの仮想環境を作成する(venv、Windows)
Absolute Path
In JavaScript, you can use __dirname, and in Python, you can use __file__ to retrieve absolute paths. The path to the created virtual environment is stored in a .json file, which can be accessed by both JavaScript and Python.
Related Article:Node-REDのノードを作成してみる その1(python-venv)
package.json Scripts
Using the npm scripts feature, we execute the script to create the virtual environment.
By specifying commands under the preinstall field in the scripts section of package.json, the commands are executed before npm install is run.
Official Documentation:https://docs.npmjs.com/cli/v8/using-npm/scripts
Development History
The development of this node has been conducted openly on GitHub. Writing everything in English helped me interact with international developers.
The first version of the node was released around March this year. I remember developing it during my spring break. Details about the initial implementation are written in the article below:
Node-REDのノードを作成してみる その1(python-venv)
The initial version executed commands using the execSync method from the Node.js child_process module.
Around June, I received a pull request from an overseas developer that enabled the node to access the Node-RED msg object. I had been unsure how to implement this feature, so their contribution was greatly appreciated.
Now, you can access msg.payload or msg.test in Python, as shown below:
I also added a configuration node that allows the creation of multiple virtual environments.
You can choose a virtual environment in the node, install packages, and execute code within that environment.
Packages installed with npm commands are added to the node_modules folder in the execution directory, but you can also specify absolute paths to create virtual environments in other directories.
There was also a time when I added multi-line text editor functionality in the morning, and by afternoon, an improved pull request enhanced it further for Python-specific use cases. Development progressed rapidly.
In July, someone proposed using the spawn method from the child_process module, inspired by the python-shell node. Unlike execSync, this enabled features like stopping execution, sequential output, and displaying execution states.
For example, you can sequentially output processes in a while loop. While this can also be done using the inject node, it can now be achieved with the venv node.
In October, I added a new node called the venv-exec node.
For example, if you install Whisper or gTTS using pip, you can run these commands directly in the node. The venv-exec node enables this functionality.
However, it still cannot stop execution mid-process or output sequentially, leaving room for improvement.
Use Cases
Utilizing Python Packages
Being able to execute Python means you can leverage powerful Python packages.
For instance, I created a flow to control a simple robot using voice commands with a microcontroller.
The overall flow looks like this:
Related Article:音声でロボットを操作してみる(Node-RED、Gemma2、Faster Whisper、XIAO ESP32C3)
The venv node is used to transcribe audio recorded by a microphone into text. I also used the ollama node to interact with a local LLM.
Related Article:Ollamaを使ってみる その1(Gemma2、Node-RED)
Text sent to the local LLM is generated using the template node, allowing AI to perform conditional judgments.
The transcription takes about 2 seconds, and processing with the local LLM takes another 2 seconds, so it’s not suitable for real-time processing. However, it works well for tasks like moving a robotic arm to a specified position.
Generating Code with ChatGPT
With generative AI capable of creating code, the venv node pairs well with AI.
In the following flow, a PDF of a research paper is processed to extract text, translate it, and summarize it using a local LLM.
For large documents, the output of the venv node couldn’t be passed as input directly. Therefore, the data was temporarily saved to a file and processed node by node.
While it’s possible to have ChatGPT generate all the code as a single script, dividing it into nodes makes the flow easier to modify and manage.
Running on Raspberry Pi
Although I don’t have a concrete example, I’ve had discussions with ChatGPT about development on Raspberry Pi, and Python code was often suggested.
The python-venv node works on Raspberry Pi, allowing you to utilize Python resources available on the platform.
However, some operating systems may cause errors. For instance, if Python 2 is the default version or if venv and pip are not pre-installed on Ubuntu, additional setup is required.
Connecting with Unreal Engine 5
As described in my Day 21 article of the Node-RED Advent Calendar 2024, it’s possible to integrate with Unreal Engine 5 (UE5).
By enabling the continuous execution mode in the venv node, you can sequentially output YOLO detection results and send them using the HTTP Request node. This data can be used to control objects in UE5 by converting 2D coordinates from two viewpoints into 3D coordinates.
Additionally, using Pixel Streaming, you can stream the UE5 screen and capture it with the venv node to execute YOLO. While YOLO training can be executed with commands, you can also use the venv-exec node to achieve this.
Related Article:YOLOで物体検出 その2(Python、Node-RED)
Unreal Engine 5を使ってみる その11(Pixel Streaming )
Conclusion
With the python-venv node, you can execute Python in Node-RED without affecting the system Python environment.
Thanks to the collaboration of international contributors, features I couldn’t implement on my own were realized. I plan to continue improving the node step by step.
Finally, I would like to express my gratitude to all the contributors. Thank you, contributors!
If you encounter any issues, have suggestions, or questions, feel free to contact me on the GitHub repository!