Action editor

In the Action editor, you can write your code and configure the execution environment by specifying the necessary libraries and dependencies for your action. You can also test your code with a simulated event before integrating it into the Pipeline.

The action editor consists of the Code editor, and the following tabs: Environment, Testing, and Settings.

Code editor

When creating or editing your action, you can find the Code editor on the right-hand side at all times. Here, you can write your Python code using a handler function.

Handler function

def handler(event, context): This function will process the input data and return results to be used in subsequent pipeline stages.

Arguments

event: This dictionary provides runtime information about the execution, including the event's unique identifier, event type, and the ID of the pipeline where the trigger occurs.

{
    "uuid": 2757097,
    "event": "project c",
    "pipeline_id": 0
}

context: is the input data passed to the function, which includes a trigger payload.

{
    "op": "c",
    "after": {
        "id": 0,
        "name": "MyProject",
        "type": "Vector",
        "status": "NotStarted",
        "options": 0,
        "team_id": 0,
        "createdAt": 1718965118000,
        "deletedAt": 0,
        "is_pinned": false,
        "updatedAt": 1718965118000,
        "creator_id": "[email protected]",
        "description": null,
        "upload_state": "Initial",
        "entropy_status": "NotStarted",
        "sharing_status": null,
        "attachment_name": null,
        "attachment_path": null,
        "custom_editor_url": null
    },
    "ts_ms": 1718965118466,
    "before": null
}

Return value

The function should return a serializable JSON object, which represents the result of the action and will be used by the next stage in the pipeline. Make sure to structure this result in a way that is compatible with subsequent processing steps.

Example

Here's an example of how you might use the handler method in your code to handle an event.

The following script sends a message to a Slack channel using Slack's API. For detailed information and additional examples, please refer to the templates available in our GitHub repository.

import os
import logging

from superannotate import SAClient
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError

logging.basicConfig(level=logging.INFO)

SLACK_TOKEN = os.environ['SLACK_TOKEN']
CHANNEL_ID = os.environ['CHANNEL_ID']


def handler(event, context):
    sa = SAClient()

    current_state = context['after']
    project_name = sa.get_project_by_id(current_state['project_id'])['name']

    # Change the message to be sent
    message = f"Ready for annotation: {project_name} has been updated"

    slack_client = WebClient(token=SLACK_TOKEN)
    # Call the conversations.list method using the WebClient
    slack_client.chat_postMessage(
        channel=CHANNEL_ID,
        text=message
        # You could also use a blocks[] array to send richer content
    )

Environment configurations

In the Environment tab, you set up the execution environment for your action by creating a container image. This environment is where your code will be executed.

Python Version

Specify the version of Python that your code will use. The chosen Python version should be compatible with your code and any libraries you plan to use.

Dependencies

List and manage the dependencies required by your code. This includes external libraries and packages that your action needs to function correctly.

Action testing

In the Testing tab, you can test the action by replicating execution using simulated events.

To test your code, you first need to create the action, ensure the environment is successfully built, and then set up the test configurations.

📘

Keep in mind that you can only test code when editing an existing action.

Create a test

To create a test configuration:

  1. When in the Testing tab, click + Create.
  2. Type in the configuration’s name in the Test name field.
  3. Select a secret you want to link to your test (optional). Refer to the Secret section for details.
  4. Select a payload template (optional). This section lists all available events at SuperAnnotate. Payload templates provide a structured JSON format that you can customize for your testing needs.
  5. Modify the prefilled data of the selected payload template or manually enter valid JSON into the Event and Context fields.
  6. Click Save.

Once you’ve done this, you can see your configuration in a list under Test configurations. You may have a maximum of 10 configurations at one time. You can Run, Edit, or Delete your configurations from this menu.

While running your test, you may Revoke it at any time before it’s completed in order to cancel the run.

Run the test

Clicking the Run button initiates the testing process, simulating code execution based on the current configuration. The button is located on the configuration row in the left panel and appears when hovering over the row.

You can find the results of your test under the Test result section when clicking on the test configuration row.

Settings

In the Settings tab, you can update the action's name and description.