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.
Create a job
Section titled “Create a job”- Go to Jobs and click New
- Give your job a name (e.g., “CSV Importer”)
- Replace the default script with the code below
The script
Section titled “The script”import csvimport osfrom 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.
Set up a trigger (optional)
Section titled “Set up a trigger (optional)”To run this job automatically whenever a CSV is uploaded:
- Toggle Enable trigger in the job form
- 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
Section titled “Write modes”The mode parameter controls how data is written:
create— Insert new rows. Usebatch_insert_ok=Truefor better performance on large datasets.create-or-update— Upsert. Matches existing rows by the fields you specify inparams_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"],)Run it
Section titled “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.