Python function

Hi,I have been using nodered for a short time and i have a question about python function. When i insert a python function, in the debug this sentence appears: " s = s.decode(detect_encoding(s), 'surrogatepass')
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.1776.0_x64__qbz5n2kfra8p0\lib\encodings\utf_32_le.py", line 11, in decode
return codecs.utf_32_le_decode(input, errors, True)
UnicodeDecodeError: 'utf-32-le' codec can't decode bytes in position 16-19: code point not in range(0x110000) "
I don't know exactly the problem, can anybody explain me? and can anybody make me and example with the use of a python function?

Which node are you using to put your python code in? The standard function node is for javaScript but there are a couple python nodes out there

The error you're dealing with is a somewhat infamous Python error when it comes to dealing with Unicode. I say somewhat infamous as most of the time it's caused by a user error. Start reading here: https://docs.python.org/3.7/howto/unicode.html
As the error path shows you're running Python 3.7 on Windows, and the encoding it has detected is utf-32. The exact line shown in the error is the line on which it is crashing is s = s.decode(detect_encoding(s), 'surrogatepass'). This is located in the json library, specifically in the json.loads function. This function takes a string in json format, and parses it to a python dictionary. In order to load the string it will first run the tokenize.detect_encoding() to figure out what encoding it is likely stored in. In this case, the tokenize library concluded it was UTF-32, however, it failed to parse resulting in this UnicodeDecodeError: it could not decode the unicode with the given codec.

Since this error is coming in through json.loads, can you explain more about what you're trying to do inside this python function node? Parsing JSON strings can be done easier in Node-RED through the core JSON node, which will either go from javascript objects (compare to python dictionaries) to a JSON string, or the other way around. And, as @zenofmud requested, can you specify which python node you're using, as there's several of them. Furthermore, can you post a sample of your input to the function node, as well as your python code?

I'm using either Python Function node and Python 3 Function

I'm using either Python function and Python 3 function.
I tried this with a simple function node:
-Inject node with a payload numer that is 4
-Function:
msg.payload='hello' + msg.payload
return msg;
-Debug
Then i deploy and it works. I obtain 'hello4'.
Since i have to use an algorithm written in Python i need to use Python function but i started with something that is easy. So i want to do what i do with the simple function.
So my python function becomes:
a=3
msg.payload=msg.payload+a
print('hello',msg.payload)
In this way i expect to obtain 'hello4'. Now, I don't know if it is correct. If you have an example with the use of a python function i will thank you, because then i will put a more difficult function.

I don't have an example, because usually if I need to execute code in Python, I run it through the exec node or set it up to communicate with Node-RED over MQTT. I am however quite experienced with Python as it is/was my primary programming language of the last decade so I've no problem debugging it.

I'm afraid I need a little more than this. Please tell me which of these nodes you're using that gives this response.

For node-red-contrib-python-function, json.loads is used when loading the msg object that comes into the node. First, the incoming msg object is stringified in JavaScript, then forwarded in plain text to the python process running the script, where it is parsed again. Somewhere in that line the error from your first post would be caused.

What does the message object look like that you're outputting to the python function?

The output will depend on what you input as msg.payload to the function. If that is an integer 1, the result would be 'hello4'.

However, since the json.loads call failed upon it having trouble figuring out the encoding of the msg object stringified, can you post what you're actually inserting? Next, can you explain what this algorithm written in Python is/looks like? It might be easily translated to javascript instead, and don't forget to take a a look at the caveats in the readme files of the different python function nodes; they all say they're not production ready. GitHub - arnauorriols/node-red-contrib-python-function: Write Python functions in Node-RED!

I'm using these:
npm install node-red-contrib-python-function
npm install node-red-contrib-python3-function
Now i've installed also npm install node-red-contrib-pythonshell.
I put my algorithm (that is a script) in the pythonsell and it works. My algorithm takes a vector in input and throught a for cycle and if condition, it generates other vectors that are updated in every cycle. If you want, if you haven't understood i can pass you my algorithm. Now i want to give in input a vector ( so i want to remove my input vector from the algorithm and i want to give it from external), how can i do with nodered? And then i want to save my results that i read on the debug in order to plot them in a dashboard. Is it possible? Sorry for the answers but I am novice with nodered and sorry for the English if it isn't correct. Thank you.

Did you read the node's description: node-red-contrib-pythonshell (node) - Node-RED

Share your flow correctly and you will get best possible help
Also list what external nodes you have installed


My flow is this.There is a "pythonshell in" node that is an algorithm and it produces those arrays media1,sigma1, etc.. in the debug. I also connected the output of the pythonshell to a chart that appears in a dashboard because i want to plot those arrays. How can i do this operation?

The output is one large string, with as you can see those arrow symbols in between, also known as linebreaks. Your first step would be formatting the output to a format you can easily use in Node-RED, preferably on the python side. For example, a dictionary that you print as json.dumps(...), then parse through a JSON node back in. Or to CSV format, followed by a CSV node if that's more useful for your situation.



Thank you. I got this thing in the python code and it still works when it runs.I also added the json parser node. Now i have no idea of what i can do to plot my array in a chart of the dashboard.

Since you opened a new thread about the chart question, please mark this one solved so people won't spend any more time on it.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.