Skip to content

Instantly share code, notes, and snippets.

@rodened
Created January 26, 2020 19:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save rodened/778859ca2503db35ff0e12341508efef to your computer and use it in GitHub Desktop.
Save rodened/778859ca2503db35ff0e12341508efef to your computer and use it in GitHub Desktop.
Install python and create virtual environments directly from the node-red editor

With the node-red-contrib-pythonshell node installed we can start to execute python scripts in node-red. The node is configured with the path to the python file and a path to a virtual python environment which is optional but we recommend to create a virtual environment for each project to make it easier to handle dependencies, library updates and so on. The first step is to create a virtual environment, then install extra libraries. After that, we can create a python script file and execute it and show the output in a debug node, in the dashboard or do other things like sending an email, store it in Google sheets or send it to other service APIs. The first flow here is to create a bash file that will be stored in the data folder, executed and create a virtual environment called envtest. This flow is made to work on the https://www.rodened.com platform and will not work on a windows machine but could be modified to create a bat file for windows instead.

Copy the flow and use the import from the menu, and then deploy it with the Save button. To create the envtest virtual environment we execute each step by clicking on the button on the timestamp nodes. In the debug window to the right, we can see information about the execution and if there were any errors.

Install Python libraries

In this flow, we will create a bash file that executes a python script that installs the sys library. If more than one library has to be installed we can add the next one after each other with a space between. If we add the Numpy library then the last row in the template file would be: pip3 install sys numpy.

flow for the python libs

When we execute the timestamp nodes we can see the status from the debug nodes in the debug window. The information is the same as if the command line where used.

debug info

Create and execute the Python script

To create a python script we will use the template node and save it to the data folder. This python script is a simple function that returns the absolute number of the entered value which is inserted from the inject node.

execute python file

template node with the python script

On row 15 to 18 we read the information from the inject node and execute the function absolute_value which is printed and shown with the debug node.

[{"id":"a6398238.dbb24","type":"tab","label":"example create python env and install packages","disabled":false,"info":""},{"id":"f4319015.f829c","type":"inject","z":"a6398238.dbb24","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":100,"y":820,"wires":[["b8d5a5ee.27a048"]]},{"id":"ba14283e.921b28","type":"file","z":"a6398238.dbb24","name":"example.py","filename":"/data/example.py","appendNewline":true,"createDir":false,"overwriteFile":"true","encoding":"none","x":530,"y":820,"wires":[["45fc3c1b.2cdf44"]]},{"id":"b8d5a5ee.27a048","type":"template","z":"a6398238.dbb24","name":"","field":"payload","fieldType":"msg","format":"python","syntax":"plain","template":"import sys\n\ndef absolute_value(num):\n\t\"\"\"This function returns the absolute\n\tvalue of the entered number\"\"\"\n\n\tif num >= 0:\n\t\treturn num\n\telse:\n\t\treturn -num\n\n# Output: 2\n#print(absolute_value(2))\n\nwhile True:\n num = sys.stdin.readline() # read the stdin from the inject node\n num = int(num)\n print(absolute_value(num))","output":"str","x":300,"y":820,"wires":[["ba14283e.921b28"]]},{"id":"13717224.4e8bfe","type":"comment","z":"a6398238.dbb24","name":"create a python script file and save to disk","info":"","x":340,"y":760,"wires":[]},{"id":"a41dee1f.ceba1","type":"inject","z":"a6398238.dbb24","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":100,"wires":[["58b5c2a.d8e9f3c"]]},{"id":"b5798004.7515c","type":"file","z":"a6398238.dbb24","name":"","filename":"/data/create_env.sh","appendNewline":true,"createDir":false,"overwriteFile":"true","encoding":"none","x":530,"y":100,"wires":[[]]},{"id":"58b5c2a.d8e9f3c","type":"template","z":"a6398238.dbb24","name":"","field":"payload","fieldType":"msg","format":"python","syntax":"plain","template":"#!/bin/bash \n\ncd /data\nls -l\npython3 -m venv envtest\nsource envtest/bin/activate\nls -l\nwhich python","output":"str","x":300,"y":100,"wires":[["b5798004.7515c"]]},{"id":"675e87af.f0a968","type":"inject","z":"a6398238.dbb24","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":200,"wires":[["7dd4be3e.84d4d"]]},{"id":"7dd4be3e.84d4d","type":"exec","z":"a6398238.dbb24","command":"chmod u+x /data/create_env.sh","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"make create_env.sh file executable","x":380,"y":200,"wires":[["62d946f1.440b28"],["62d946f1.440b28"],["62d946f1.440b28"]]},{"id":"62d946f1.440b28","type":"debug","z":"a6398238.dbb24","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":610,"y":200,"wires":[]},{"id":"bd5f6d44.e7d4b","type":"inject","z":"a6398238.dbb24","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":100,"y":320,"wires":[["3edc17c3.75cb18"]]},{"id":"3edc17c3.75cb18","type":"exec","z":"a6398238.dbb24","command":"bash /data/create_env.sh","addpay":false,"append":"","useSpawn":"true","timer":"","oldrc":false,"name":"execute create_env.sh","x":340,"y":320,"wires":[["9b94a230.939e5"],["9b94a230.939e5"],["9b94a230.939e5"]]},{"id":"9b94a230.939e5","type":"debug","z":"a6398238.dbb24","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":590,"y":320,"wires":[]},{"id":"4e8765d8.507ddc","type":"inject","z":"a6398238.dbb24","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":100,"y":540,"wires":[["61a0c56a.052f5c"]]},{"id":"61a0c56a.052f5c","type":"exec","z":"a6398238.dbb24","command":"chmod u+x /data/install_packages.sh","addpay":false,"append":"","useSpawn":"true","timer":"","oldrc":false,"name":"make install_packages.sh file executable","x":400,"y":540,"wires":[["13c2c87a.b73bc8"],["13c2c87a.b73bc8"],["13c2c87a.b73bc8"]]},{"id":"13c2c87a.b73bc8","type":"debug","z":"a6398238.dbb24","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":670,"y":540,"wires":[]},{"id":"e39cb324.19715","type":"inject","z":"a6398238.dbb24","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":100,"y":680,"wires":[["fbbbbe17.cfbef"]]},{"id":"fbbbbe17.cfbef","type":"exec","z":"a6398238.dbb24","command":"bash /data/install_packages.sh","addpay":false,"append":"","useSpawn":"true","timer":"","oldrc":false,"name":"execute /install_packages.sh","x":360,"y":680,"wires":[["70a58ad1.947204"],["70a58ad1.947204"],["70a58ad1.947204"]]},{"id":"70a58ad1.947204","type":"debug","z":"a6398238.dbb24","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":590,"y":680,"wires":[]},{"id":"7e36e3ea.d2ee8c","type":"comment","z":"a6398238.dbb24","name":"create bash file that creates and activates venv","info":"In the template node we create a bash file that will create a virtual python environment and activate the environment.\n\n#!/bin/bash \n\ncd /data\nls -l\npython3 -m venv envtest\nsource envtest/bin/activate\nls -l\nwhich python","x":340,"y":60,"wires":[]},{"id":"10bf703d.477d3","type":"comment","z":"a6398238.dbb24","name":"make the bash file executable","info":"","x":280,"y":140,"wires":[]},{"id":"8ffba3c3.5413","type":"comment","z":"a6398238.dbb24","name":"make the bash file executable","info":"","x":300,"y":480,"wires":[]},{"id":"ffe32ec1.4f824","type":"comment","z":"a6398238.dbb24","name":"execute the bash file","info":"","x":250,"y":260,"wires":[]},{"id":"f0066900.4db1f8","type":"inject","z":"a6398238.dbb24","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":100,"y":420,"wires":[["34f6f7c4.23b168"]]},{"id":"255ca056.4e641","type":"file","z":"a6398238.dbb24","name":"","filename":"/data/install_packages.sh","appendNewline":true,"createDir":false,"overwriteFile":"true","encoding":"none","x":610,"y":420,"wires":[[]]},{"id":"34f6f7c4.23b168","type":"template","z":"a6398238.dbb24","name":"file to install packages","field":"payload","fieldType":"msg","format":"python","syntax":"plain","template":"#!/bin/bash \n\ncd /data\nsource envtest/bin/activate\nls -l\nwhich python\npip3 install sys","output":"str","x":340,"y":420,"wires":[["255ca056.4e641"]]},{"id":"39f56651.847cca","type":"comment","z":"a6398238.dbb24","name":"create bash file that installs packages","info":"","x":270,"y":380,"wires":[]},{"id":"fa6a51ee.d9239","type":"comment","z":"a6398238.dbb24","name":"execute the bash file to install packages","info":"","x":330,"y":620,"wires":[]},{"id":"e757b049.aa745","type":"debug","z":"a6398238.dbb24","name":"","active":true,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","x":680,"y":920,"wires":[]},{"id":"9b520682.5cbbe8","type":"pythonshell in","z":"a6398238.dbb24","name":"example","pyfile":"/data/example.py","virtualenv":"/data/envtest","continuous":true,"stdInData":true,"x":400,"y":920,"wires":[["e757b049.aa745"]]},{"id":"4bd2821f.13aaac","type":"inject","z":"a6398238.dbb24","name":"inject number","topic":"","payload":"2","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":920,"wires":[["9b520682.5cbbe8"]]},{"id":"317f7a8.2b1ed86","type":"comment","z":"a6398238.dbb24","name":"inject a number to the python script and display it in the debug window","info":"","x":430,"y":880,"wires":[]},{"id":"45fc3c1b.2cdf44","type":"debug","z":"a6398238.dbb24","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":690,"y":820,"wires":[]}]
@Shaheen1906
Copy link

How can I return msg.payload from python code to debug console

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment