Node.py - Python for Node-RED?

thought the exec node works fine for small things inluding in/output handling. For larger things I would vote up for janvda's solution.
Didn't know about FastApi. Tornado workes best for me for websockets and long running tasks. Flask seems to be also nice on the python side.

Thanks to ChZeman, janvda & vkuehn,

I'll check out FastApi. I don't mind using Java-Script, it's just that I'm programming ESP8266 microcontrollers using microPython that will be integrated with Node-RED via MQTT; it would be nice to maintain the same language throughout the project. Also Java-Scipts () & {} complications invite errors. I can see the Node-RED developers attraction to Java-Script, given that Node-RED is browser based. Even though Java-Script has extensive tools for web based application, it would seem that a Node.py could serve reasonably well as a back-end processor.

The point of node-red is a low-code environment, if you work with mqtt you donā€™t need to code, many tasks can be handled by the build-in nodes without writing a single line of code.

Perhaps you should give it a shot, because the python you are promoting is not the same python you are using on your microcontroller.

Yes, but unfortunately it doesn't exist.
To write a node.py to replace node.js would be a massive task. Node.js is written in C/C++ in case you didn't realise.

I understand that microPython is a subset of Python, but at leas they're related.

Actually, Colin it may come as a shock to you, but I do know that Node.js is written in C/C++. I also know that Node.py doesn't exist; I would have thought from my posts that would be obvious. Further, if you don't start taking a less confrontational tone I will refuse to respond to your posts; there's no need for your holier than thou attitude!

Not sure what you are referring to here. But clearly every language has syntactical nuance that can help or hinder developers who aren't familiar with itm that's hardly unique to JS.

Just to set the record straight, that simply isn't the case. Node-RED came into existence, in part, as an exercise to learn more about node.js - the server side javascript runtime. That is where the 'node' part of its name comes from. And part of its success has been down to being able to build upon the huge ecosystem of node modules that are available via npm.

On the broader topic of python support, we do have established patterns for how nodes can interact with python scripts - allowing them to bridge between the two. That gives the best of both worlds - where there are particular modules that have been implemented in python and don't have equivalent node.js alternatives.

Creating a pure python runtime would be an interesting exercise - but it would always struggle to be compatible with the core Node-RED, with over 3500 nodes to reimplement in python.

4 Likes

It's just that Java-Script requires a lot of embedding; where Python only uses indention.

I must confess I don't know the motivation IBM had in developing Node-RED, but all the descriptions of Java-Script I've seen portray it as a web-centric language.

To help fill in the blanks, I created Node-RED alongside @dceejay . So you can take my responses as fairly accurate account of where it came from and the motivation behind it.

Certainly JavaScript has historically been web-centric, but it has long since grown beyond that. It is very widely adopted on the server side with the growth of runtimes such as node.js and deno.

3 Likes

knolleary, Please don't interpret my response as challenging your knowledge of Java-Scrip's historic development. As I've mentioned I've only begun investigating Node-RED and Java-Script for less than a month, as Node-RED appears to be a good fit to replace an archaic architecture I started using years ago.

They are just different, I used to hate all the brackets and semi-colons too. My first languages were things like COBOL, APL (another IBM language), BASIC and FORTH. None of which use brackets and semi-colons.

However, while the semi-colons are now no longer needed in JavaScript, I actually now find the brackets a very useful indicator of the structure and they tend to be rather more robust than the spaces vs tabs issues that used to dog Python.

Again, each have their strengths and weaknesses.

I hear what you're saying. My programming experience runs the gamut from FORTRAN, various Assembly languages, PLM-86, BASIC and Visual-Basic (VB6). What I miss in Python is the richness of Visual-Basic conditional blocks. All you have in Python is the rather bland if, elseif, else; I really miss the CASE statements. If you want to have a Switch-Case statement in Python you need to perform dictionary mapping, which is no where near as powerful as the Select-Case in Visual Basic. Sometimes I feel like Diogenes of Sinope, but instead of searching for an honest man, I'm searching for the perfect programming language. :wink:

?? What do you have in Python that you don't have in JavaScript? JS has if/then/else if/else, it has a variety of loops and also has switch/case. Things get even richer as you start to make use of newer async features such as promises and async/await.

I assume you meant JavaScript there. JS certainly has case statements.

switch - JavaScript | MDN (mozilla.org)

There is absolutely no such thing I'm afraid. I've been looking for that for years or at least something that is "good". I had hoped that JavaScript would be that since you can use it in the front- and back-ends. Having a single language is certainly really helpful for those of us who are no longer full-time developers.

But as mentioned, Python and JavaScript as well as Rust, Go, ... all have strengths and weaknesses. I personally find the naming and some of the other conventions of Python irksome and unnecessarily complex. But I also find the inconsistencies in JavaScript equally irksome. Similarly, I gave up on Python when it failed to recognise the importance of operational context (e.g. having a standard method of execution from the web - something that was true for a while and that Node.js dealt with early on). On the other hand, the "standards" development board for JavaScript seem to have been smoking something unusually potent in recent years and the rapid but drip, drip bloat in parts of the language definition have been a pain as it makes it really hard to know what version to target.

Actually, I miss the fact that Python doesn't have the CASE statements that I enjoyed in Visual-Basic. I know about JS has a case statement, but the cases in VB could be constants (numeric, strings, etc) , functions, arithmetic expressions. Python 2 didn't have anything, Py3 added dictionary-mapping that's very limited.

Exactly, why I referenced Diogenes, he never found an honest man - lol

Here's one of many interesting articles comparing Py to J:

Unfortunately, as we agree, there's no such thing as the perfect language. Although one of the things I liked about VB is it was both Interpreted and could be compiled to machine-code; allowing quick development, and fast execution. However, VB is a legacy language (1991), while you could create objects, it wasn't an object oriented language (object oriented VB-net replaced VB in 2002) and with the speed of today's processors compilation is less of an issue.

Yes, you mentioned that already and I commented on it where I said that some of the early part of the comparison is quite inaccurate though I generally agreed with the final conclusions.

Sadly, when I was programming, I had to use VB on a number of occasions. It was OK but had some horrid habbits of its own. Even worse when combined with Access! I well remember some work for Barclays Bank that I did. I was at least the 3rd person to be asked to try and improve an Access/VB combo application. Thankfully, I was the first to actually improve it (lord knows what the other 2 had been doing to it!). I actually don't mind BASIC because of its simplicity but that is also its downfall of course. VB as you say did add some modern features but then hampered itself with its environment. BBC BASIC was about the best version I ever used though nobody other than UK folk will know about it :grinning:

I agree though - one of my criteria for a "perfect" language would be the ability to use it in both immediate and compiled contexts.

The V8 JavaScript engine is approaching the fastest possible immediate execution environment in my view thanks to its JIT compile and optimisation approaches. Traditionally, Python had a very poor first run speed - not really sure how it fares in more recent versions, I've not really looked. I do know that a lot of its current perceved speed for things like data analysis, as previously mentioned, comes from its use of compiled C, C++ and FORTRAN libraries.

TotallyInformation,

I have a question for you. While you appear to have a preference for JS in general, however, in your opinion, which language, Python, or Java-Script would be a better match for programming an ESP8266 microcontroller; and why?

Most of the microcontroller high-level languages are C/C++ or Python based.

I have heard of JS being used somewhere once in my lifetime

Ah, that's easy. If you know C++, then use that. If, like me you just about get by, use the Arduino IDE with its mostly C++.

However, I've switched to using ESPhome so I don't need to do much coding but I can (in C++) when I need to.

I've tried the various MicroPython's and I'm afraid I wasn't impressed. I've also used the embedded JS versions. In both cases, they are too limited in that they often are missing libraries that make everything so easy.

Of course, my ESP coding is generally pretty straightforward given that I will use libraries for everything I can. ESPhome has a great range of libraries available for a framework.

I didn't know C/C++ when I started programming Arduino's and most people would say I still don't! :slight_smile: However, I've managed to get by OK with lots of internet searches.

I was actually thinking about how much work it would be to rewrite the backend of Node-RED in python. Such as, serve the same front-end through Flask instead of express, interpret the flow.json with python and instead of nodes having an "html" and "js" file you'd have an "html" and "py" file. Then you know, just write a wrapper for each module in the entire pip library.

The actual "language" itself doesn't matter, but these pre-written modules have semantics that their users had to learn. To me, this is the strength of NR as the semantics of your familiar NPM modules don't change, just the interface with which you do it has changed. It only makes sense that python users would feel left out :wink:

At the risk of seeming disloyal to Node-RED (though I've now been using it for nearly as many years as it has existed). I would say don't try to duplicate what it is. Instead, think about what it could be to the Python community and use the open source ideas as a jump-off point to do things in a more Pythonistic way.

I, for one, would love to see another flow-based visual programming tool come into existance, I think there is more than enough room for both.

There are now more modern techniques and libraries and Python has a somewhat different approach to many things than JavaScript/Node.js.

A Python-based flow programming tool would be a tremendous thing for many data analysts for example. Something the JavaScript is all too weak at (not because it couldn't do it but rather because of the penetration of Python into data analysis). It would be another great tool in the Python arsenal.

Anyway, just my thoughts. I love Node-RED and have built up a reasonable knowldege of JavaScript and have built a contributed node far more complex and popular than I ever dreamed I would (with an average 2,000 downloads per month over its 4 1/2 year lifespan). But there is certainly room for something new. :grinning: