Write your first Labric job

Parse a CSV file and write the results to a Labric table in under 20 lines of Python.
View as Markdown

Jobs are Python scripts that run in a sandboxed environment on Labric. They can be triggered automatically when files are uploaded, or run on demand. This guide walks through creating a simple job that reads a CSV and writes the data to a table.

Create a job

  1. Go to Jobs and click New
  2. Give your job a name (e.g., “CSV Importer”)
  3. Replace the default script with the code below

The script

1import csv
2import os
3from labric import Labric
4
5client = Labric()
6
7file_path = os.environ["LABRIC_FILE_PATH_0"]
8
9with open(file_path, "r") as f:
10 records = list(csv.DictReader(f))
11
12client.tools.write(
13 target_type="table",
14 target_name="measurements",
15 data=records,
16 mode="create",
17 job_execution_id=os.environ["LABRIC_JOB_EXECUTION_ID"],
18)
19
20print(f"Wrote {len(records)} records to measurements")

This reads the first input file as a CSV, then writes every row to a table called measurements. If the table doesn’t exist, Labric creates it automatically.

Set up a trigger (optional)

To run this job automatically whenever a CSV is uploaded:

  1. Toggle Enable trigger in the job form
  2. Set the file extension filter to .csv

The job will fire each time a matching file arrives, with the file path available as LABRIC_FILE_PATH_0.

Write modes

The mode parameter controls how data is written:

  • create — Insert new rows. Use batch_insert_ok=True for better performance on large datasets.
  • create-or-update — Upsert. Matches existing rows by the fields you specify in params_to_match_for_update, updates them if found, inserts if not.
1client.tools.write(
2 target_type="table",
3 target_name="measurements",
4 data=records,
5 mode="create-or-update",
6 params_to_match_for_update=["sample_id"],
7 job_execution_id=os.environ["LABRIC_JOB_EXECUTION_ID"],
8)

Run it

Click Execute on your job page, select a CSV file, and watch the execution log. Once it completes, your data will be available in the Data section.