Scripts

Seal allows you to write and execute your own custom Python code within the platform, enabling powerful custom solutions and automations.

Scripts can contain code that can be then executed. This is useful for automating data transformation and creation.

For general information about writing code in Seal, see this page.

For example, scripts can ingest a machine output CSV file, do some data cleaning and other transformation, and store the results in Seal as Records.

Scripts enable you to transform your data in more powerful and flexible ways.

Outputting Records

To output new Records from a Script, assign a pandas dataframe to the special variable records_to_create. Records generated from the script will be linked to the corresponding Step. For example:

import random
data = [
    {
        'Machine': 'XXXX',
        'Reading': random.randrange(1,100)
    },
    {
        'Machine': 'YYYY',
        'Reading': random.randrange(1,100)
    },
    {
        'Machine': 'ZZZZ',
        'Reading': random.randrange(1,100)
    },
]
records_to_create = pandas.DataFrame.from_records(data)

Note:

  • you can't return data from Scripts via the return keyword, since the Script itself is not a Python function. Also, the final expression isn't automatically returned - it's not like Juypter Notebooks. To output record data, you must use the records_to_create variable.

  • To throw exceptions, use normal Python `raise` syntax, don't put error messages in record fields in records_to_create.

Fields

Fields are stored as an object called fields, keyed by the unique field names. Every field object has the following fields:

  • id - a unique UUID given to every field, so it can be referenced. When a Data Record is created from a Data Step, it will have the same fields with matching ids

  • the field's type

  • the field's dataType (usually the same as the type, but multiple field types may use the same underlying data type). type is how it appears in the UI, dataType refers to the underlying data type

  • value - the actual data. All field values are nullable - they are usually null in Data Steps (unless you want a default value for produced Records), and then populated in Data Records when a lab technician is doing data entry, for example.

  • config - certain field have additional config, for example specifying a number display format, or whether the field can contain multiple values.

For example:


  "fields": {
    "CSV file": {
      "id": "f1714b77-d081-4a1e-bfc4-427289fce204",
      "type": "REFERENCE",
      "value": [
        {
          "id": "e9a49732-49a0-4674-b683-c6991fac160a",
          "version": "12"
        }
      ],
      "config": {
        "allowMultiple": false
      },
      "dataType": "ENTITY"
    },
    "Group name": {
      "id": "0519bc74-7086-4ba2-b769-994b62a48945",
      "type": "STRING",
      "value": "A23-Z",
      "config": {},
      "dataType": "STRING"
    },
    "Sample weight": {
      "id": "70c33317-16ae-48b2-9907-fc5112677773",
      "type": "NUMBER",
      "value": 52.3,
      "config": {
        "format": "0.000"
      },
      "dataType": "NUMBER"
    }
  }

Field Types

The columns in the dataframe are converted to fields in each Record. Seal infers the field types based on the data provided:

Data shape
Field type

a number or None

NUMBER

a boolean

BOOLEAN

an ISO date string

DATE

an ISO datetime string (must be timezone aware)

DATETIME

any other string

STRING

an array of UUID strings

UPLOAD (i.e. an array of File ids)

an array of non-UUID strings

SELECT (a select field aka dropdown)

an array of {id, version} objects

REFERENCE (an entity reference field)

Expand this section to find out more about specific Field Type Configurations.

Type: NUMBER

Description: Numeric value.

Value: A valid JSON number or null.

Config:

format // '0.X' | '0.00' | '0.000' | '0,0.00' | '0.0%' | '0.00%' | '0.0a' | 'SCI' | 'SCI3' | 'EU' | 'IND' | 'Rounded'

Type: STRING

Description: Text.

Value: A string or null. The string must be at least length 1 - to represent an empty value, use null.

Config: None


Type: BOOLEAN

Description: True/false.

Value: A JSON boolean or null.

Config: None


Type: DATE

Description: A date in ISO 8601 format.

Value: An ISO date string or null

Config: None


Type: DATETIME

Description: A datetime in ISO 8601 format, requiring a timezone.

Value: A timezone aware ISO datetime string or null

Config:

format // 'PPpp' | 'PPppp' | 'Pp' | "yyyy-MM-dd'T'HH':'mm':'ssXXX" | 'dd-MM-yyyy HH:mm:ss'

Type: SELECT

Description: An array of enum-like strings. Displayed in the UI as a dropdown/select field with multiple options.

Value: An array of strings.

Config:

selectOptions // array of strings specifying the enum options
allowMultiple // bool, whether multiple values are allowed

Type: REFERENCE

Description: An entity reference field for referencing other entities (of any kind), including File entities for referencing files.

Value: An array of {id, version} objects.

Config:

allowMultiple // bool

Title shortcut

If the column name is 'title' in the dataframe, Seal will automatically output this as the title of the Record, rather than creating a field called 'title'.

Developing Scripts

Scripts can only be properly executed in an Instance of a Record Template. While developing the Script, you can run it in a sandboxed preview mode to check your code for bugs - no output data will be persisted (caution: side effects in your code, like uploading a file, will be persisted).

Seal's AI Co-Pilot

Seal comes with a built in AI agent to write the code for you. Navigate to the 'Co-pilot' tab on the right hand side, and start chatting with the AI to describe what you need. The AI is also able to de-bug and add to your existing code.

Note that the generated code may not fully be fit for your purpose - we recommend checking over the results.

Last updated