Node.py - Python for Node-RED?

I'm new to Node-RED; my personal software preferences tend toward Python, not Java script.
Is there a way to integrate Python into Node-RED for use in function blocks in lieu of Java script?

No. You can call a python script in an exec node, but only do that if the code cannot easily be converted for some reason.

If you are proficient at Python you will not have any great difficulty with js. Just leap in and have a go.

However, a major point about node-red is that it minimises the amount of code that you have to write. Very often what you want to do can be handled by the standard nodes.

I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot in about 1 hour. A small investment for a lot of gain.

1 Like

node.py is an intriguing idea but hasn't been updated for 2 years which gives you an idea that it probably hasn't really taken off.

You could also use something like python-shell. Indeed, there used to be a contrib node that ran Python code in a similar fashion to the function node but I don't think that's been maintained.

Colin, Thanks for the reply. I'm just following up on old posts that essentially have made the same request given that Python has becomes so ubiquitous in the software industry. In addition my project will be utilizing microPython in various microprocessor platforms; it would be nice to be able to maintain the same code in the microprocessors and in Node-RED. I have seen several approaches for partial integrations of Python, but having it as the native language would be preferable.

Also thanks for the link to Node-RED Essentials, but I've previously reviewed them. I'm currently reviewing videos on utilizing MQTT with Node-RED and the micros running Python.

This is clearly not possible. The two languages produce completely different code that is incompatible.

But there are many ways to make use of both languages including the ones we've already mentioned.

It's been said "where there's a will, there's a way" :grinning:

Then I look forward to seeing your solution :grinning:

3 Likes

Note that nodejs/javascript is even more ubiquitous. There is a python function node available that lets you write python.

As you already mentioned: mqtt is the way to go and easy to use in both micropython and node-red.

Note that this works by spawning python to run the code. Personally, to run some python code under node-red, I would either run python from an exec node, or for more complex situations, spawn a python process and communicate with it via MQTT, then you will have full control of what is going on.
Also the node referenced has a number of unaddressed issues and, in one, the author says he has not got time to investigate the problem.

I personally do exactly as @Colin suggests. I have many Python scripts running next to NR or in other computers. They all do their dedicated tasks and communicates with NR via MQTT. I also use NR to control the execution of them and that they are running correctly, that they all regularly return heartbeats and correct responses back to NR. If necessary, NR will restart them
Reason for using Python scripts in my case is historical, I have had them since long before, some are very complex and would be hard to migrate. Besides they are all very well tested, ”buggfree”, I know I can trust them as fully reliable. Some simpler ones I have also been able to replace using NR functionality

1 Like

While the link suggested Python Function Node looks like a good stop-gap, but as stated in the link it has issues. I agree that Node-RED is an excellent tool; it would be even better if there were a Node.py. Java script is an archaic language, and functionally it's a subset of the more powerful Python.

One of the many Python/Java-Script comparisons: Python vs Java-Script

And for another point of view - 5 Reasons JavaScript is Still Better Than Python | IEEE Computer Society
But yes - if there are better ways to incorporate python programs without writing python then happy to discuss.

1 Like

:rofl: :rofl: :rofl: :rofl:

No, JavaScript is a newer language than Python :grinning:

And certainly Python isn't more powerful a language. However, in certain domains, there is more support. That's mainly due to the support for "legacy" libraries for engineering, statistics and analytics.

Obviously though Python isn't more powerful in one key area and that is for in-browser apps and web pages. JavaScript is the only option there.

The other area that JavaScript actually outstrips Python is in servicing back-end web services. The use of Node.js well outstrips Python AFAIK. One of the reasons for this is that there was a significant period where serving a Python app to the web was badly fragmented. One of the other main reasons being the desire by many teams to have a single language for front and back-end web programming.


NodeJs VS Python - Framework Technologies Market Share Comparison (similartech.com)

2 Likes

If Java Script is SO MUCH BETTER the Python; why is Python virtually the default language used by Google, Microsoft and most Deep Learning projects.

Just look in book stores, the availability of Python books is huge; very few Java Script books.

The link I posted is just one of many that highlight that power of Python over Java Script; as far as I'm concerned there is no argument.

Node-RED is too valuable, and would do well to adopt a Node.py process, as well as other languages that are sure to be developed in the future to prevent itself from becoming a dinosaur. I'm not advocating abandoning Java-Script, just providing an alternate base language. Launch Node.js and the Node-RED will recognize that, launch Node.py (if it ever exists) and Node-Red will recognize that base language.

I'm not going to be bated into further arguments about Python vs Java-Script; I've said what I have to say - you all are entitled to you opinions.

Have a good life, X

Yes - hopefully one day there will be a great way to handle both - or call from one to the other. Indeed python is the current de facto language for deep learning, but not for the web, so there will need to be a crossover somewhere. Dinosaurs reigned for about 150 million years so if Node-RED is around that long before being wiped out then I'll take it...

5 Likes

Every language has its usage.

Google use any language: the one fits its purpose. For deeplearning, analytics, backend, python may be the way to go, for event driven tasks: nodejs (not even a contest). Then again I doubt Google would use python for their backend where speed is a factor.

The OpenJS foundation board of directors: Google, IBM, Microsoft, Github, Netflix and node-red is a project of that foundation as well.

1 Like

dceejay, loved your comment :grinning:

1 Like

I didn't say it was better, you spoke of power. My take is that they are much of a muchness overall. But Python is better for certain tasks and JavaScript better for others.

The one thing that was done for Python was the amount of integration to C/C++ and Fortran libraries for engineering and data analytics - largely driven by academia. There is no reason why this couldn't happen with Node.js as well - but Node.js is less popular in academic circles. This is because Python excels at being a teaching language for certain basic tasks (lacking all of the brackets that all of the langugages that use a Pascal style have) and because it is excellent for teaching and learning object-based programming that was (and sometimes still is) all the rage at one point.

JavaScript on the other hand was thrown together in a hurry for a specific purpose. It just so happens that it got adopted as the de-facto standard for front-end web programming and from there has slowly but surely moved into other areas. Helped massively by the work that Google themselves (as well as many others) have put into the increadible and now largely standard V8 JavaScript engine. This has brought native JavaScript performance to ever new levels such that it is probably now the leader in raw performance for scripting languages (strictly speaking of course it is a just-in-time compiled language).

That performance and the standardisation on V8 and Node.js along with a fairly strong (if a little weird at times) standards body has helped JavaScript move firmly into the server space and onto the desktop with increasing numbers of cross-platform apps being written using Node.js and Electron or similar.

Google, Microsoft, Facebook, Twitter and many more big names use Node.js as well as front-end JavaScript in the much of their tooling and applications. The figures I shared show the market penetration.

Python tends to be used more (again as shown in the figures) for engineering and data analytics including machine learning for the reasons I've already given. The power behind Python are the compiled libraries not the Python language itself.

I note that the link is from an education site. I also note that its description of JavaScript is significantly skewed. Indeed, it is incorrect in many details. For example mutable vs immutable, modern versions of JavaScript most certainly support immutable data types and this is embodded in object definitions. No support for hash-tables is also incorrect and demonstrates a lack of understanding of the language.

However, most of the rest of the article seems to be correct and I agree with their conclusions which are: a) you can do pretty much anything in either language (e.g. they have much the same "power"), b) Python is the better language to teach beginners with its slightly more formal approach and less visual clutter.

Not at all, why would anyone want a Node.js web application server to be forced to run under a secondary language handler under Python. That makes very little sense. Especially since both can very easily and nicely co-exist in parallel. Firstly performance would almost certainly be dreadful and secondly, it would be dependent on a library already 2-3 years out of date.

Hmm, I have to say that maybe you might want to take a more balanced view of the world if you don't want to be drawn into arguments.

But I agree. This debate has run its course. It was useful to me though to help clarify some arguments so thanks for that.

I didn't have any Javascript experience when I first started playing with Node Red, but had plenty of experience with Python. I installed the Python nodes and started working in Node Red with the Python nodes wherever I could.

I discovered it was far better and easier to use Javascript because I needed to jump through hoops to make certain things work. There were also a couple of roadblocks I couldn't get past. Javascript might be a pain in the ass at first, but it's really pretty good once you get used to it and it won't take long. You WILL find yourself frustrated at times until you get used to the Node Red way of doing things, but it will be completely worth it in the end!

Chris

1 Like

In addition to what others proposed you can extend your python programs with a REST API using the FastApi python library. From node-red you can then call these REST APIs using the node-red http nodes. Your python program and node-red program don't need to run on the same machine or in the same docker container in this case.

MQTT is also an option like others said, but that is an asynchronous interface contrary to FastApi.

1 Like