Python equivalent of flow.set() and flow.get()

Hi. I'm new to Node Red. Sorry, I'm using Python... I want to be able to share variables between nodes and all I can find is how to get data from sys.argv (so sent as a trigger from another node). My triggers to various nodes will contain data specific to objects I want to status or control and I can pass that using sys.argv but I want to access other variables that could be common to all nodes in the flow (e.g. API access token). In some nodeJS examples I found flow.set() and flow.get() methods but I could not find any Python equivalent. I really don't want to have to store these in a bunch of text files....

Welcome to the forum @Ouagadougou.

No need to apologise for using python.
But how are you planning to use it with Node-red?

The most obvious way is to call your python script[s] via the Exec node of Node-red.
That does allow you to pass values to the script as command line parameters.
For static data like API keys you might consider Environment variables.

I think you may have some Node-red terminology confused, and as a newcomer to Node-red you might find these introductory videos useful: Node-RED Essentials. The videos are made by the developers of node-red.

Note that Node-red is built upon passing a collection of related data as a complete "message". Although context variables are available, it might be a mistake to leap straight to using them rather than message properties.

Thanks for the quick reply and for the introductory videos. I thought I could use a change node to give me access to the variables I needed and tried to add this and another trigger to a node block but I could see variables were only passed by the triggering node. I will have a look through the videos and take you up on your suggestion to use environment variables.

Your description of what you are trying to do really does not make any sense, so it is quite possible that we don't understand at all what you are trying to do. If you want further help please explain what you are trying to do with python in relation to node-red.

Hi Colin
I have APIs that I need to access and they need a valid token. The APIs will be triggered using data such as the workload being controlled and the parameters to send to it. I don't want to have to request a token every time I make an API call. I can get a token that is valid for 24 hours and valid for any node making API calls on that platform so I want to put the token information somewhere that it can be accessed by any node accessing APIs on that platform.

So what has that got to do with Python?

I'm using Python, I don't know nodeJS and Python doesn't have a simple way to access flow variables or flow variables (e.g. you can't say flow.get() or flow.set()).

So what has the question got to do with Node Red? You did not answer questions about how you are using python with node red.

I already have a number of Python scripts that I use in Jupyter notebooks to do various API actions. Each time I need to modify the script to get it to control a specific workload within the platform I am controlling. I thought of using Node Red as a more graphic way of doing this where I can have pre-prepared configurations that I can pass to the nodes containing the scripts rather than modifying scripts each time.

What node are you using that contains a python script?

I'm using node-red-contrib-pythonshell. In any case, I resolved my issue by using a database to store any data that needs to be shared between nodes.

1 Like

Have you seen the example Jupyter Tools on flows.nodered.org ?

It seems to offer python scripting within Node-red, may be relevant to your requirements.

Thanks jbudd. I'll give it a try.

Had you told us that in the first post it would have been much easier to understand your question.

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