Table of Contents

Data Rows

Alex Cota Updated by Alex Cota

These are some common use cases people have when working with Data Rows in the Python SDK. For a complete list of methods, see our API reference.

For the definition of a Data Row, see Overview & data types.

Before you start

Import labelbox.

import labelbox

Make sure the client is initialized.

from labelbox import Client
client = Client()

Bulk add Data Rows

For adding Data Rows individually, see Creating your first project.

Dataset.create_data_rows() accepts a list of items and is an asynchronous bulk operation. This is typically faster and avoids API limit issues. Unless you specify otherwise, your code will continue before the Data Rows are fully created on the server side. Paths to local files must be passed as a string.

  • external_id (string) - OPTIONAL
  • row_data (string) - REQUIRED
  • uid (ID) - not updatable
  • updated_at (DateTime) - not updatable
  • created_at (DateTime) - not updatable

DataRow.row_data should only be used when you are passing a path to an external URL. You must pass paths to external URLs as a dict.

dataset = client.get_dataset("<dataset_uid>")
task = dataset.create_data_rows([{labelbox.schema.data_row.DataRow.row_data:""},{labelbox.schema.data_row.DataRow.row_data:""}])

Pass paths to local files as strings.

task = dataset.create_data_rows(["path/to/file1.jpg", "path/to/file2.jpg"])

You may also include paths to local files and paths to URLs at same time.

task = dataset.create_data_rows([{labelbox.schema.data_row.DataRow.row_data:""},"path/to/file2.jpg"])

You can (but don't have to) use task.wait_till_done() wait for the bulk creation to complete before continuing with other tasks. For more information on Tasks, see the API reference.

Add Data Rows from a JSON file

At a minimum, the JSON file you are using to import your Data Rows must include row_data as a key for each asset you wish to add. You have the option to include an external_id.

"row_data": "<IMG_URL>",
"external_id": "12345"
"row_data": "<IMG_URL_2>"
"row_data": "<path/to/file.jpg>"

Use Dataset.create_data_rows to import Data Rows from your JSON file.

dataset = client.get_dataset("<dataset_uid>")
with open("file.json") as f: dataset.create_data_rows(json.load(f))

This will return a task ID.

<Task ID: ck37lee3hyixi0725egxwooue>

See the API reference for more information.

Fetch Data Row by external ID

The optional external_id field of the Data Row object is additional to the uid field. People have found this additional external_id field useful when they need to map the Data Row to an asset in their own system.

Use the data_row_for_external_id method for getting a single Data Row by external_id within a dataset.

dataset = client.get_dataset("<dataset_uid>")
data_row = dataset.data_row_for_external_id("<external_id>")
If there are two Data Rows that have the same external ID, this method will raise a ResourceNotFoundError. See the API reference for more information.

Fetch all Data Rows in a Dataset

To fetch multiple Data Rows, use the data_rows method. This example demonstrates how to get a list of all Data Rows within a specified dataset.

dataset = client.get_dataset("<dataset_uid>")
for data_row in dataset.data_rows():

Bulk delete Data Rows

To delete many Data Rows at a time, use the bulk_delete method. The code below demonstrates how to delete all Data Rows from a specified dataset if they were created after a certain date.

from labelbox import DataRow
dataset = client.get_dataset("<dataset_uid>")
data_rows = list(dataset.data_rows(where=DataRow.created_at > some_date))

Was this page helpful?