Python Interpreter
Overview
Python Interpreter is a processor that runs a custom Python script that can process the block input and produce any output JSON.
Python version 3.10 is used to interpret the script. The interpreter support the standard library, but be aware that file system access is read-only.
Workflows containing Python Interpreter blocks can only be published in asynchronous mode.
Input
A Python Interpreter block accepts any JSON object as its input, without the need to declare the object structure.
However, explicitly declaring the structure of the input object by mapping its top-level keys to keys of the output of upstream blocks or to keys of the workflow input—if its format has been defined—has two effects:
- The input to the block can simply be the output of the previous block, if any, but also a mix of the output keys of different upstream sources, including the workflow input.
-
The block has an explicit declaration of its input which is useful:
- In the validation panel inside the Functional tab of the properties pop-up to have a template of the input object.
- When testing the entire workflow, to have a template of the request when the block is the first in the workflow.
The declaration of the structure and the sources of the input object is done in the Input tab of the block properties pop-up.
Block properties
The block properties are accessed by editing the block.
They are divided into these groups:
-
Basic properties:
- Block name
- Component version (read only)
- Block ID (read only)
-
Functional
The functional properties of the block coincide with the code of the script which is written with the code editor.
- To copy the script to the clipboard select Copy code to clipboard .
- To restore the default code select Clear .
When writing the code, consider that the
process
function must be defined and:- It must have an argument corresponding to the block input.
- It's return value will be the output of the block.
-
Input
The optional declaration of the structure and the sources of the input JSON object can be done in the Input tab of the properties pop-up.
-
To declare a new top-level key:
- Click the plus button: a new row is added below the list of existing properties.
- In the left field enter the name of the key.
-
Using the drop-down on the right, first select the type of the key choosing between number, string, boolean, object and array. After that, the list below will only show source keys of the same type.
The list is divided in groups, one group for each possible source.
Select the expand and collapse icons to the right of the source name or select the name of the source multiple times to show or hide the sub-list of output keys for that source.
If the type of the key is object, the first entry in the sub-list is the source name.By choosing it, you indicate that the value of the key will coincide with the entire output JSON of that source.
Note
The drop-down list shows all the first-level keys that can, potentially, be available, but the keys that are actually there when executing the workflow may be less. If you map a key that's not actually produced at runtime, the corresponding input key will be set to
null
.You can filter the list by typing at the top of it: the list will show only keys with matching names.
To cancel the mapping process, select No mapping from the list or, if you already chose a key, select Clear .
-
To modify an existing mapping:
- Edit the left field to change the name of the key.
- Use the drop-down as described above to choose a different source.
-
To remove an existing mapping, select Remove to the right of it.
-
-
Output
The Output tab of the properties pop-up is where you optionally declare the structure of the output object.
-
Basic
In this tab you can declare the top-level keys of the output object.
-
To declare a new key:
- Click the plus button: a new row is added below the list of existing keys.
- In the left field enter the name of the key.
- Using the drop-down on the right, choose the type of the key.
-
To modify an existing key:
- Edit the left field to change the name of the key.
- Use the drop-down on the right to choose a different type.
-
To remove an existing key, select Remove to the right of it.
-
-
Advanced
In this sub-tab of the Output tab you can declare the structure of the output object to the desired level of detail by writing a corresponding JSON template. To indicate the type of a property use these conventional values:
Value Type 0
Number true
orfalse
Boolean ""
String {}
Object []
Array To add a portion of JSON taken from a possible input source:
- Choose the source name from the first drop-down menu on the right, the one that shows the names of the blocks .
- Choose the top-level key from the second drop-down list, the one with the key icon .
To reset the JSON select Clear text : the template is set to an empty object.
-
Output
A Python interpreter block must produce a JSON object.
You can optionally declare, at the desired level of detail, the structure of the output object using the Output group of the the block properties.
This declaration allows to use the Python Interpreter block output as a source of input for any downstream block.
Inventory
To have a view of the possible data sources, use the Inventory panel to the left of the Functional tab of the properties pop-up.
To expand and collapse the panel select the expand and collapse icons.
The panel shows, grouped by type, all the possible sources, that is the upstream blocks plus, possibly, the input format of the workflow, labeled Input.
For each source, the structure of the output object in displayed as a navigable tree.
Code validation
To validate the script, select Code validator in the Functional tab of the properties pop-up. The extended view opens.
-
To specify the input JSON to test with:
- Write the JSON inside the Test input pane.
Or:
- Select Upload to load an input JSON from file.
-
To reset the input JSON select Clear .
- To start the test select Test code .
The Result panel shows the result, which is a JSON object in case of success or an error message. To copy the result JSON to the clipboard, select Copy code to clipboard .
Full screen mode
To extend the pop-up to the entire browser window or to restore its original size, select Toggle full screen .