Connectors / Core / Python

Python

Python

This connector is for adding easy to use Python functionality to you workflows.

Overview

The Python connector is a core connector built in order to allow users to add Python functions into their workflows easily and seamlessly.

While the nature of this connector dictates that there is only ONE operation available, nonetheless there are several points that must be taken into account before using it.

Please make sure to read over the following information before continuing.

IMPORTANT!: Please note that the Python connector supports Python v8. All of the in-built functionality for Python 8 is supported apart from the Format function and Class definitions. See the Feature support section below for more details.

Python connector basics

Available operation

Only one key operation is available with the Python core connector:

Execute Script: Executes python code

Essential knowledge

Each time the Python connector is used it requires the following steps to be taken into account.

See our example workflow below for a simple demonstration of these points in action.

  1. Users can optionally define input variables. These can be pulled in from previous steps within the workflow itself or manually defined.

  2. Scripts must begin with def executeScript(input): in order to function correctly. From within this parameter users can then create/ return variables and work with objects.

  3. Within the script itself, input variables are written in the format of: input['NAME'].

For example, a user could say: message_object = input['message']; in order to save a variable with the name 'message' to a local function variable.

  1. What is returned in the script itself will determine what is available for subsequent connector steps to make use of e.g. return message_object.

  2. Always bare in mind that Python is indentation sensitive. You must correctly indent code blocks otherwise the connector will throw an error.

Feature support

  • Python's Format function is not permitted as it is not in alignment with the purpose of the connector itself.

  • Since only direct scripting is supported with the connector, Class definitions are not possible.

Example Usage

Below is an example of a way in which you could potentially use the Python connector, to reformat an array of product items and save them to an online e-commerce store.

The steps will be as follows:

  1. Setup using a manual trigger and create some sample product data.
  2. Use the Python connector to parse over the sample data and format it correctly.
  3. Loop over each product item from previous steps to get its individual data.
  4. Save the products to the online BigCommerce store.

The final outcome should look like this:

1 - Setup Trigger & create products

Once you have clicked 'Create new workflow' from your main Tray.io dashboard named it, select the Manual trigger from the trigger options available:

After you have been redirected to the Tray.io workflow dashboard, from the connectors panel on the left, add an Object Helpers connector step.

Use this step to add in some objects, or products, as per this workflows needs. Feel free to use the sample data below for testing purposes.

{
"products":[
{
"name":"itemNames",
"value":[
"Sauce Labs Backpack",
"Sauce Labs Bike Light",
"Sauce Labs Bolt T-Shirt",
"Test.allTheThings() T-Shirt (Red)"
]
},
{
"name":"itemDescriptions",
"value":[
"carry.allTheThings() with the sleek, streamlined Sly Pack that melds uncompromising style with unequaled laptop and tablet protection.",
"A red light isn't the desired state in testing but it sure helps when riding your bike at night. Water-resistant with 3 lighting modes, 1 AAA battery included.",
"Get your testing superhero on with the Sauce Labs bolt T-shirt. From American Apparel, 100% ringspun combed cotton, heather gray with red bolt.",
]
},
{
"name":"itemPrices",
"value":[
"$29.99",
"$9.99",
"$15.99",
]
}
]
}

These steps will give the later Python step some data to work with.

Feel free to re-name your steps as you go along to make things clearer for yourself and other users.

2 - Format the data

Add a Python connector to your second step. Set the operation to 'Execute Script'.

By clicking the 'Edit' button under the 'Script' section, an in-browser text editor will automatically open up for users to write their script within.

Here you can see we reference the variable 'itemNames' using bracket notation, and place it in a "for loop" so as to reformat the data into a structure which can be worked with later.

Feel free to copy/ paste in the following:

'''
You can reference the input variables using input["NAME"]
'''
def executeScript(input):
products = []
for idx in range(len(input['itemNames'])):
products.append({
'name': input['itemNames'][idx],
'description': input['itemDescription'][idx],
'price': float(input['itemPrice'][idx].split("$")[1]),
'imageUrl': input['itemImageUrl'][idx],
})
return products

While the below method is not used in this example, within the Python step itself there is another way to set variables. That is, manually:

3 - Loop through list

Next, search for the Loop collection connector within your connector panel, and drag it into your workflow as your next step. Set your operations to 'Loop list'.

The Loop Collection connector allows you to iterate through a list of results. In this example, we will use it to iterate through the data found within the previous Python connector step.

In order to specify the list you want to loop through, start by using the 'List' mapping icon (found next to the list input field, within the properties panel) to generate the connector-snake.

While hovering over the 'Transform Data' step (with the tail end of the connector-snake), select value from the list of output properties displayed. This will auto-populate a jsonpath within your 'List' input field, and update the type selector to jsonpath.

For more clarification on the pathways you have available, open the Debug panel to view your step's Input and Output.

Now, the workflow will iterate through the product results from the Python step. But currently, it doesn't do anything with them.

4 - Create product

The last step will be to drag a BigCommerce connector inside of the Loop Collection step itself.

Set the operation to 'Create Product'. As you can see, the 'Name', 'Type', 'Price', 'Weight' and 'Categories' fields are required.

Again we use the connector-snake to auto populate some of the fields with a jsonpath $.steps.loop-1.value.

Note, that in the example above to get the correct value we append the corresponding name as defined in the python script.

Now when we run the workflow the products will automatically be added to the BigCommerce store.

BEST PRACTICES: Whenever you do decide to create your own workflow, please make sure you take a look at our Managing data best practices guide.

All Operations

Latest version:

1.0