Skip to navigation

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

import csv
import os
from labric import Labric, TableWrite
client = Labric()
file_path = os.environ["LABRIC_FILE_PATH_0"]
with open(file_path, "r") as f:
records = list(csv.DictReader(f))
client.data.write(
tables=[TableWrite(table="measurements", rows=records)],
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. The table must already exist in your organization, and each CSV header must name one of its columns. Values are converted to each column’s type, so numbers read from the CSV as text are stored as numbers.

Add dependencies

The labric SDK is always available. To use other packages, declare them in a PEP 723 inline metadata block at the top of your script — Labric installs them before the script runs:

# /// script
# dependencies = ["pandas", "scipy>=1.13"]
# ///
import pandas as pd

Any standard version specifier works, so pin versions (e.g. pandas==2.2.2) when reproducibility matters. The block is validated when you save the job, so a malformed declaration surfaces immediately rather than at run time.

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 of a table write controls how its rows are written:

  • insert (the default) creates every row.
  • upsert updates rows that match an existing row on the key columns and creates the rest. Running the job again on the same file then updates rows instead of duplicating them.
  • update and delete require every row to match an existing row on key.
client.data.write(
tables=[
TableWrite(
table="measurements",
rows=records,
mode="upsert",
key=["sample_id"],
)
],
job_execution_id=os.environ["LABRIC_JOB_EXECUTION_ID"],
)

A single client.data.write call can write several tables in one transaction, with rows that reference rows written earlier in the same call, and can write raw data series column by column. If any row is invalid, nothing is written and the error lists every problem by its place in the request. See the write data reference for the full request.

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.