# Code Runner

Code Runner allows the user to use Python code to perform operations.&#x20;

<figure><img src="/files/s1IRBT4EuARDtZjisB3r" alt=""><figcaption></figcaption></figure>

{% hint style="success" %}
The output should be stored in a variable called response at the end of Code Runner.&#x20;

response = "Hello World"
{% endhint %}

The previous step responses from Data Hub can be used in Code Runner by enclosing the step number (optionally followed by a dot and variable name in case of nested structure) within double curly brackets.&#x20;

```python
email = {{1.response.customer.email}}
// Do something here
```

## Restrictions

* Allowed packages:  `JSON, datetime, math, re and time`
* Allowed built-in methods: `min, max, len, range, all, any, bool, dict, enumerate, filter, float, int, isinstance, list, map, reversed and sum`
* Other than the above-specified packages and built-in methods, no other packages can be imported as `Import` is restricted.

## Use Cases

### "response" Variable

Code Runner should have a variable called `response` which holds the final result. If there is no variable called response, there will be a validation error.

**Example 1:**

```python
response = {"ids": [1,3,5,6]}
```

Output:

`{"ids": [1,3,5,6]}`

**Example 2:**

```python
def check(email):
    if(re.fullmatch(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,7}\b', email)):
        return True
    else:
        return False
response=check("abc@dckap.com")
```

Output:&#x20;

`True`

### Import is restricted

Importing other packages/modules is not allowed in Code Runner. If imported, the code will throw an error and will not be executed.

**Example:**

```python
import pandas as pd
data = {"items": ['pen', 'pencil', 'sharpener'], "quantity": [50, 40, 45]}
df = pd.DataFrame(data)
response = df
```

Output:

`Execution failed because import found in code`

### Built-in methods

The usage of built-in methods other than the listed methods is not supported.&#x20;

**Example 1: Using an allowed built-in method**

```python
a = [1,2,3,4]
response = min(a)
```

Output:&#x20;

`1`<br>

**Example 2: Using a not allowed built-in method**

```python
a = [1,2,3,4]
response = type(a)
```

Output:

`name 'type' is not defined`<br>

{% hint style="info" %}
If any restricted methods are needed for you, reach out to us with your use case by raising a ticket in the Support Portal.
{% endhint %}

### Execution Time Limit

<mark style="color:green;">`3 minutes`</mark>

If the code takes more than that for execution, a timeout occurs and an error is thrown.

### ~~Modify~~ Duplicate Previous Step Values

Changing the result of the previous step is restricted in workflows. However, with the help of Code Runner, any previous step from the Data hub can be duplicated and modified. The response of Code Runner can be used where the modified data is needed.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.dckapintegrator.com/project-manager/workflows/actions/code-runner.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
