---
agentTools:
  projectIndex: https://help.logichub.com/llms.txt
---

# callScript

Execute script for each row in a table

Execute a Python script for each row in a table, where the script does the processing of the values in the table that are passed as arguments.

The script must be uploaded to the LogicHub (drag and drop script file to **Automations > Scripts**).

[block:api-header]
{
  "title": "Operator Usage in Easy Mode"
}
[/block]

1. Click **+** on the parent node.
2. Enter the **Call Script** operator in the search field and select the operator from the **Results** to open the operator form.
3. In the **Input Table** drop-down, enter or select the table containing the data to run this operator on.
4. In the **Script Name** drop-down, select the name of the script.
5. In the **Args** drop-down, select one or multiple column names to use as arguments for the script.
6. In the **Time between Calls** field, enter a value in seconds, minutes, hours, days, or weeks.
7. Click **Run** to view the result.
8. Click **Save** to add the operator to the playbook.
9. Click **Cancel** to discard the operator form.

[block:api-header]
{
  "type": "basic",
  "title": "Usage Details"
}
[/block]

[block:code]
{
  "codes": [
    {
      "code": "callScript(inputTable, scriptName, args, timeBetweenCalls)",
      "language": "sql",
      "name": "LQL Command"
    }
  ]
}
[/block]

**Input**:
`inputTable`: Table containing the data to run this operator on.
`scriptName`: Name of the script as appear on the Scripts page
`args`: Column names to use as arguments for the script
`timeBetweenCalls`: Time between calls

**Output**:
new `lhub_id`, `exitCode`, `error` and `output` for each row. If the output is a list of JSON objects (e.g. rows), the output explodes the array into rows and extracts JSON values in the JSON objects into new columns.

`args` are the columns names (such as \["user", "accessCode"]). From the script they are accessed using *sys.argv\[1]* and so on. To produce output to the table, you must print the result. See examples below for more information.

[block:api-header]
{
  "title": "Example 1"
}
[/block]

Table = "users"

[block:html]
{
  "html": "<style>\n  th {\n    border: 1px solid #cccccc;\n    background-color: #eeeeee;\n    padding: 8px 5px 8px 5px;\n    text-align: left\n  }\n</style>\n\n<div><table>\n<thead>\n<tr>\n<th>firstName</th>\n<th>lastName</th>\n<th>emailAddress</th>\n</tr>\n</thead>\n<tbody>\n  <tr><td>Emil</td><td>Bilgazyev</td><td>emil@logichub.com</td></tr>\n</tbody>\n</tr>\n</table></div>"
}
[/block]

Create the following Python script file, save it as scriptOne.py, then drag and drop this file to **Automations > Scripts** page.

[block:code]
{
  "codes": [
    {
      "code": "import sys\n\n# sys.argv[1] is value of firstName column\n# sys.argv[2] is value of lastName column\n# sys.argv[3] is value of emailAddress column\n# ordering is important\n\nemail = sys.argv[3] # emil@logichub.com\nwork = email.split('@')[1].split()[0] #logichub\n\nprint(\"%s %s works at %s\" % (sys.argv[2].upper(), sys.argv[1], work.upper()))",
      "language": "python"
    }
  ]
}
[/block]

Create a computation step and in the LQL write following line.

[block:code]
{
  "codes": [
    {
      "code": "callScript(users, \"scriptOne.py\", [\"firstName\", \"lastName\", \"emailAddress\"], \"1 s\")\n-- Note: arguments (columns) ordering are important, you need to remember which columns is at which index",
      "language": "sql",
      "name": "LQL command"
    }
  ]
}
[/block]

**Output**

[block:html]
{
  "html": "\n<div><table>\n<thead>\n<tr>\n<th>firstName</th>\n<th>lastName</th>\n<th>emailAddress</th>\n  <th>output</th>\n  <th>exitCode</th>\n  <th>error</th>\n</tr>\n</thead>\n<tbody>\n  <tr><td>Emil</td><td>Bilgazyev</td><td>emil@logichub.com</td><td>BILGAZYEV Emil works at LOGICHUB</td><td></td><td></td></tr>\n</tbody>\n</tr>\n</table></div>"
}
[/block]

[block:api-header]
{
  "title": "Example 2"
}
[/block]

If the script produces multiple rows for each row, you must print them as an array of JSON objects that will get populated into columns and rows.

[block:code]
{
  "codes": [
    {
      "code": "import sys\nfn = sys.argv[1]\nln = sys.argv[2]\nea = sys.argv[3]\nprint('[{\"FIRSTNAME\":\"%s\", \"length\":%d}, {\"LASTNAME\":\"%s\", \"length\":%d}, {\"EMAILADDRESS\":\"%s\", \"length\":%d}]'%(fn, len(fn), ln, len(ln), ea, ln(ea)))\n\n",
      "language": "python"
    }
  ]
}
[/block]

**Output**

[block:html]
{
  "html": "<div><table>\n<thead>\n<tr>\n<th>firstName</th>\n<th>lastName</th>\n<th>emailAddress</th>\n  <th>output</th>\n  <th>FIRSTNAME</th>\n  <th>LASTNAME</th>\n  <th>EMAILADDRESS</th>\n  <th>length</th>\n  <th>exitCode</th>\n  <th>error</th>\n</tr>\n</thead>\n<tbody>\n  <tr><td>Emil</td><td>Bilgazyev</td><td>emil@logichub.com</td>\n    <td>JSON object</td><td>EMIL</td><td></td><td><td>4</td><td></td><td></td></tr>\n  <tr><td>Emil</td><td>Bilgazyev</td><td>emil@logichub.com</td><td>JSON object</td><td></td><td>BILGAZYEV</td><td></td><td>9</td><td></td><td></td></tr>\n    <tr><td>Emil</td><td>Bilgazyev</td><td>emil@logichub.com</td><td>JSON object</td><td></td><td><td>EMIL@LOGICHUB.COM</td><td>17</td><td></td><td></td></tr>\n</tbody>\n</tr>\n</table></div>"
}
[/block]

For 1 row, 3 rows were produced. Because each row has a different JSON schema.

* FIRSTNAME, length
* LASTNAME, length
* EMAILADDRESS, length

The operator did the union of all field names, and produced new columns for each field name.