Skip to content
Get started

Write Your First Labric Job

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

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.

  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
import csv
import os
from labric import Labric
client = Labric()
file_path = os.environ["LABRIC_FILE_PATH_0"]
with open(file_path, "r") as f:
records = list(csv.DictReader(f))
client.tools.write(
target_type="table",
target_name="measurements",
data=records,
mode="create",
job_execution_id=os.environ["LABRIC_JOB_EXECUTION_ID"],
)
print(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.

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.

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.
client.tools.write(
target_type="table",
target_name="measurements",
data=records,
mode="create-or-update",
params_to_match_for_update=["sample_id"],
job_execution_id=os.environ["LABRIC_JOB_EXECUTION_ID"],
)

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.