# Python Code in Workflows

## Python Nodes

### Overview

<figure><img src="https://4279757243-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTbBxR0Ob7RUmbvHZkQi2%2Fuploads%2FP1R7ft9qQGHphT0n5rlk%2Fpython.png?alt=media&#x26;token=56518ba0-00d9-4a2a-96cb-9a0382a720e0" alt="" width="563"><figcaption><p>Python node</p></figcaption></figure>

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.&#x20;

### **Configuration** <a href="#whisper-configuration" id="whisper-configuration"></a>

* **Name**: Customizable name for identifying this node.
* **Source Code**: The Python code to execute, provided in the run function format.

Example:

```python
def run(input_data: dict):
    if input_data["is_request_call_allowed"]:
        import requests
        response = requests.get(input_data["url"])
        if response.status_code in input_data["allowed_status_codes"]:
            return response.json()
            
```

{% hint style="warning" %}
If no custom input transformer is specified for a Python node, the passed data can be accessed using the `input_data["input_data"]` structure.
{% endhint %}

{% hint style="info" %}
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**.
{% endhint %}

* **Description**: Short description of the node’s functionality.

### Input

* **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`.

### Output

* **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.

### Execution Environment and Security

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](https://github.com/dynamiq-ai/dynamiq/blob/main/dynamiq/nodes/tools/python.py#L15). 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.

### Usage Example&#x20;

<figure><img src="https://4279757243-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTbBxR0Ob7RUmbvHZkQi2%2Fuploads%2FLrgNWem0kUlocUgHkIhc%2Fpython-flow.png?alt=media&#x26;token=da09b9e6-39f5-4a0e-bef3-6e91c45eb432" alt="" width="563"><figcaption><p>Python workflow</p></figcaption></figure>

1. Drag and Drop the **Python** node from the toolbox.
2. Write the required python code within the run function format in the **Source Code** section.
3. Connect an **Input** node to pass inputs (`url`, `is_request_call_allowed`, and `allowed_status_codes`) into **input\_data** for the **Python** node.
4. 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.
