How to pass flow variables to a Python node?

Hi KNIME Team,

I’m really happy to see this Python-based KNIME extension released with ver. 4.6, thank you all for your great and continuous effort!

From the documentation, I found that it is possible to access flow variables that are passed to a Python node through input table/binary ports, but I’m not sure how to do that. In KAP, I can’t link a the flow variable port of a prior node to the input port of a Python node. Could you maybe provide some sample code on that? That would be very helpful and thanks in advance!

Best,
Ji

@jiqicn welcome to the KNIME forum. You can send Flow Variables to Python.

https://docs.knime.com/latest/python_installation_guide/#usage-examples

var_in_python = knime_io.flow_variables['name_of_flow_variable']

5 Likes

Hi @jiqicn, and welcome!

In addition to @mlauber71’s (as always, useful) links and examples, if the problem you are facing is physically how to make the flow variable connections between a prior node and the python node (because you can’t see them), then you may not have seen that you can right-click on a node and select “show flow variable ports”. This isn’t strictly necessary as you can connect without showing them if you know where to drag the mouse, but it can help to see the flow variable ports.
It’s then simply a case of joining to them from the required node (and if the flow ports aren’t showing on the prior node, then right click that too and “show flow variable ports”).

If a node is passing data into the input port, you don’t need to also join the flow variable ports, as flow variables come “for free” when data ports are connected.

4 Likes

Thank you for the tips! I think I know where is the problem. From the instruction (Create a New Python based KNIME Extension), I found it saying “a Python node is only able to access flow variables that have been propagated to it via a table/binary input port”, this makes me feel that I should link the flow variable port from a prior node to the input port of the Python node on KAP, which is not possible. Now I figure out that I should still link to the flow variable port of the Python node and access the variable in the code like what you suggested.

1 Like

Thank you @takbb! I think I know how to find the ports and link them to other nodes physically. Previously, by reading the instruction, I thought I should link the flow variable port (the red ball) of a prior node to the input port (the black triangles) of the Python node, but then I found that I was wrong. I’m okay now with accessing flow variables.

1 Like

Hi @jiqicn, You are welcome and from what you have said I can understand the confusion. Glad you have it resolved.