
Overview
The Python code block allows you to write Python code to extend your blueprint’s functionality. Use it to perform custom data processing, calculations, API integrations, or any other logic that requires Python programming capabilities.Common use cases
- Custom data processing and transformations
- Complex calculations and mathematical operations
- API integrations with external services
- File manipulation and data parsing
How it works
- Code execution: Write Python code in the block’s code editor.
- Variable access: Access state variables, environment variables, and preinstalled libraries.
- Output generation: Use
set_output()
to return values to subsequent blocks. - Error handling: Handle exceptions and provide meaningful error messages. If an error occurs, access the error message in the next block using
@{message}
. - Error exit condition: Raise an
Exception
to trigger the Error exit condition and route to error handling blocks.
Examples
Calculate average from list
This example shows how to return a value from the Python code block. It calculates the average of a list of numbers and returns the result to the next block by using theset_output
function.
Blueprint flow:
- UI Trigger → User provides list of numbers
- Python code → Calculates average of numbers
- Set state → Stores result in state variable

Error handling with exceptions
You can trigger the Error exit condition by raising anException
in your Python code. This allows you to route to error handling blocks when specific conditions are met.
Example:
Exception
occurs, the Python code block exits with the Error condition, allowing you to route to error handling blocks in your blueprint. You can access the error message in the next block by referencing the @{message}
variable.
Available variables and libraries
Python code block can access the following global variables and libraries:- State variables
- Execution environment variables
- Preinstalled Python libraries
- Functions you’ve defined in your
main.py
file
Return values from the Python code block
To return a value from a Python code block and store it in theresult
execution environment variable, you must use the set_output
function.
Anything that runs in the block but is not returned in the set_output
function does not get passed to the next block.
Fields
Name | Type | Control | Default | Description | Options | Validation |
---|---|---|---|---|---|---|
Code | Code | Textarea | - | The code to be executed. | - | - |
End states
Below are the possible end states of the block call.Name | Field | Type | Description |
---|---|---|---|
Success | - | success | The event handler execution was successful. |
Error | - | error | The event handler execution wasn’t successful. |
@{result}
variable in the block that follows it in a blueprint. The output contains whatever value was passed to the set_output()
function.
Access the error message of a Python code block using the @{message}
variable in the block that follows it in a blueprint. The error message contains whatever value was passed to the raise Exception
function.