Run Code

The Run Code App is a built-in tool in Locale that allows users to run custom code within their workflows. This feature is perfect for performing custom data transformations or adding complex logic to your workflows.

Supported Languages

Locale currently supports two programming languages for running custom code:

  • JavaScript
  • Python

Adding the Run Code Node

  1. Add it from App Picker: Add the Run Code node to your workflow by opening up the app picker, either from the add icon on top left, or simply by dragging/clicking on an output handle of a node.
  2. Choose Language: Select either JavaScript or Python as the language for your code.

Code Execution Environment

The code execution environment in Locale has a minimal memory footprint and provides limited third-party libraries. It is important to note that network requests cannot be made directly from your code. Instead, use the API Call node to make network requests and then access the resulting data in your code block.

Writing Custom Code

Users are provided with a template function where they can write their custom code. The function will have a data parameter, which allows access to data from other nodes. Template/data variables are not supported in code blocks, so all input data must be accessed through the function parameters.

JavaScript Function Template

Here is the template function for JavaScript:

async function run(data) {
  let result = {};

  // Your code here

  // Add your data to the result object
  // Return the result to the next step
  return result;
}

Python Function Template

Here is the template function for Python:

import json

def run(data):

  # Your code here

  # Add your data to the result object
  # Return the result to the next step
  result = json.dumps({})
  return result

Example Usage

Suppose the data parameter is a JSON object containing the following data:

{
  "postgres": {
    "data": [
      {
        "order_id": "12323",
        "amount": 1255,
        "discount": 0.5
      },
      ....
    ]
  }
}

JavaScript Example

Let's say you want to calculate the total amount of the orders:

async function run(data) {
  let result = {};

  // Calculate total amount
  let totalAmount = 0;
  for (let order of data.postgres.data) {
    totalAmount += order.amount - order.discount;
  }

  result.totalAmount = totalAmount;
  return result;
}

Python Example

Here is the same calculation in Python:

import json

def run(data):
  
  # Calculate total amount
  total_amount = 0
  for order in data['postgres']['data']:
    total_amount += order['amount'] - order['discount']
  
  result = json.dumps({"totalAmount": total_amount})
  return result

Limitations

  • Memory Footprint: The code runs on a minimal memory footprint, so keep your code efficient.
  • Limited Libraries: Only a limited set of third-party libraries are available.
  • No Network Requests: Network requests should be handled using the API Call node.

Need Help?

If you have any questions or need assistance with using the Code App in Locale, our support team is here to help. Visit our Help Center or contact us directly at support@locale.ai.


The Code App in Locale is a powerful tool for adding custom data transformations and complex logic to your workflows. By writing JavaScript or Python code within the provided template functions, you can create highly customized workflows that meet your specific needs.