Python Code in Workflows
Last updated
Last updated
The Python node allows executing custom Python code which makes it an essential tool for adding flexible, custom logic to your workflows. This node is ideal for running data processing, API interactions and other custom Python functions while ensuring safe execution through restricted imports and controlled built-in functions.
Name: Customizable name for identifying this node.
Source Code: The Python code to execute, provided in the run function format.
Example:
Ensure that all code is structured to work within the def run(input_data: dict) function, as this function will be executed with input_data passed in as a dict.
Description: Short description of the node’s functionality.
input_data: Dictionary containing key-value pairs for the code execution. For example, in the provided code, input_data might contain keys like url
, is_request_call_allowed
, and allowed_status_codes
.
content: The output of the run function. This can be any Python-compatible data structure, such as string, integer, list, or dict. The output can then be transferred to other nodes in the workflow or traced for debugging.
This node uses a restricted Python environment:
Allowed Imports: Only specific modules are permitted for import, such as requests, json, math, datetime, and other utility libraries listed here. Custom imports outside of the allowed modules will raise an ImportError.
Restricted Built-ins: Only safe and limited built-in functions are available for use (e.g., len
, map
, sum
, etc.), while potentially unsafe functions are disabled. Custom or restricted access guards (_getattr_
, _getitem_
, _getiter_
) are enforced to prevent unauthorized actions.
Drag and Drop the Python node from the toolbox.
Write the required python code within the run function format in the Source Code section.
Connect an Input node to pass inputs (url
, is_request_call_allowed
, and allowed_status_codes
) into input_data for the Python node.
Connect an Output node or other downstream nodes to process the content output generated by the Python node, allowing easy integration with subsequent steps in your workflow.