404background / node-red-contrib-python-venv

Node for python virtual environment
https://flows.nodered.org/node/@background404/node-red-contrib-python-venv
MIT License
10 stars 4 forks source link

Pass Node-RED message to Python script #6

Closed Alkarex closed 5 months ago

Alkarex commented 5 months ago

Allow something like print(msg['payload']) fix https://github.com/404background/node-red-contrib-python-venv/issues/4 image

404background commented 5 months ago

Thank you @Alkarex ! I read your code. I think it is a good idea to use python exec to read the msg.payload value.

I installed the repository you Forked into Node-RED and got some errors. The first error was a missing python file error, which was caused by the missing tmp folder. This was my mistake, I have fixed this with a .gitignore. image

The second error was a syntax error when executing the command. Have you encountered this error? image copy 3

I will try to solve this error and write the Python code that was written in the command in the new file.

Alkarex commented 5 months ago

Ah, this is probably due to running on Windows. Let me try something

Alkarex commented 5 months ago

Please try again

404background commented 5 months ago

Thank you @Alkarex ! I had the same error after that, but it was solved.

I tried it with a short command. It seems that the error occurs depending on the type of quotation:

image

I fixed it for line 19 in the node/venv.js file.:

const command = `${pythonPath} -c 'import base64;import json;msg=json.loads(base64.b64decode(r"${message}"));exec(open(r"${filePath}").read())'`

const command = `${pythonPath} -c "import base64;import json;msg=json.loads(base64.b64decode(r'${message}'));exec(open(r'${filePath}').read())"`

I can run it in my environment. Could you try it?

Alkarex commented 5 months ago

Could you show the error message you get in Node-RED when running with the original quotes?

404background commented 5 months ago

@Alkarex It was the same as the second error message when running with the original quotes.

image

After fixing the quotations, I was able to run it!

image

Alkarex commented 5 months ago

Your suggestion also works for me, so fine

404background commented 5 months ago

Thank you @Alkarex ! I will merge this pull request, and release as a new version.