Skip to main content Skip to footer

Python Bridge Documentation

Version: 6.2.0

Retrieved: 2025-10-09 15:16:12


Python Bridge

Execute Python 3 code to process message data and send new or updated messages to the next module(s). All standard packages are available and third-party packages can be installed by listing them in the Packages section.

Settings

Name Requirements Purpose Default
Source Property string The property containing the data to pass to python. If left empty the whole message will be available to the Python code.  
Target Property string The property to write the result into, by calling the module.next() function. Note that only dictionaries can be returned.  
Packages string A list of packages that will be installed before running the flow. Separate packages with a comma or newline: package1, package2, package2.  
Package installation timeout in seconds. integer A maximum timeout for the installation of packages in seconds. 300
Code string The property containing the code to execute including the required def msg_handler(msg, module):. import json...

Using print() to debug

By using print or another way to write to stdout you can write messages that will show up in the browser during a remote sessions. Note that you need to enable debug on the module to catch data written to stdout.

Third-party packages

Third-party packages can be installed by listing them in the Packages setting. Before starting the flow the Node will run pip install on each item in this list. You can enter anything supported by that command, e.g. you can specify a specific version using SomePackage==version syntax or you can add command line switches to reference a private repository with --index-url <url>. The first time you run a flow that needs third-party packages startup times will be longer since the Node needs to download and install the packages before the flow can start.

Example 1 - Basic usage

In this first example we calculate the square of a received number and return the result on the same property we received the number on, i.e. we overwrite the value on the output. Note how the use of Sourceand Target properties affect how message data is referenced.

Settings

Source Property = `data`
Target Property = `data`
Code =
def msg_handler(msg, module):
    result = msg['value'] * msg['value']
    module.next({'value' : result})

Input

{ "data" : { "value" : 5 } }

Output

{ "data" : { "value" : 25 } }

Example 2 - Internal looping

The initialize method (started on a separate thread) allows you to loop with whatever logic you might have, without requiring any input messages. Then when you want to send a message to the next module(s) in the flow you just call module.next().
In this example we deliver a counter value on the output, i.e. the module will act like a data source.

Settings

Source Property =
Target Property =

Code =
import time
def initialize(module):
    print('initialize')
    counter = 0
    msg = {}
    while(True):
        msg['count'] = counter
        counter += 1
        module.next(msg)
        time.sleep(1)

def msg_handler(msg, module):
    print('msg handler')

Output

1st: `{ "count" : 0 }`
2nd: `{ "count" : 1 }`
3rd: `{ "count" : 2 }`
4th: `{ "count" : 3 }`
...
9th: `{ "count" : 8 }`

Example 3 - Using state

Any code outside the pre-defined functions will run once on flow start. You can use this to instantiate variables to hold state between calls to the msg_handler function.
In this example we use global variables to calculate the accumulating average of the numbers received.

Settings

Source Property =
Target Property =
Code =
sum = 0
count = 0
def msg_handler(msg, module):
    global count
    global sum
    sum += msg['value']
    count += 1
    result = {'avg' : sum/count}
    module.next(result)

Input

1st: `{ "value" : 5 }`
2nd: `{ "value" : 20 }`
3rd: `{ "value" : 15 }`
4th: `{ "value" : 45 }`

Output

1st: `{ "avg" : 5 }`
2nd: `{ "avg" : 8 }`
3rd: `{ "avg" : 13 }`
4th: `{ "avg" : 21 }`

Example 4 - Conditional output

In this example we only call the next module if the value is less than 25 or greater than 75.

Settings

Source Property =
Target Property =
Code =
def msg_handler(msg, module):
    if msg['value'] > 75:
        module.next({'message':'Value is greater than 75'})
    if msg['value'] < 25:
        module.next({'message':'Value is less than 25'})

Input

1st: `{ "value" : 18 }`
2nd: `{ "value" : 72 }`

Output

1st: `{"message": "Value is less than 25"}`
2nd: `No output`