The Client is the main entry point for working with the Labelbox SDK. The Client is used to fetch DbObjects and execute queries.
The Labelbox SDK is primarily used for interacting with Labelbox's database. The core abstraction used to represent a database entity is a DbObject. It has special attributes called Fields and Relationships that facilitate database queries.
Most objects within the Labelbox SDK are DbOjects. You will see
Dataset, for example, among others throughout the documentation.
When you fetch an object from the server, the client obtains all field values for that object. When you access that obtained field value, the cached value is returned. There is no round-trip fetch to the server to get the field value you have already fetched. Server-side updates made after the client-side fetch are not auto-propagated, meaning the values returned will still be the cached values.
Each DbObject has a set of fields. The below sections explain how to access or update one or more fields on a DbObject.
DbObject fields must be accessed as attributes (e.g.,
project.name). Unlike relationships, fields are cached.
In this example,
client.get_projects() returns a
PaginatedCollection object. You can iterate through the
PaginatedCollection object and specify the fields you want to be returned in the fetch. To fetch multiple fields from an object, you must separate them by commas.
from labelbox import Client client = Client() projects = client.get_projects() for project in projects: print(project.name, project.uid)
To update a field, use the
DbObject.update() method to pass the field and the new value. The following data types support the update method:
Each data update that uses
DbObject.update() on the client side immediately performs the same update on the server side. If the client-side update does not raise an exception, you can assume the update was successfully passed on the server-side.
This example uses the
project.update() method to change a project's name and description.
project = client.get_project("<project_id>") project.update(name="Project Name", description="New description")
Paginated collections support comparisons for filtering a query.
For example, you can use the
get_datasets method with a
where parameter with a comparison operator to filter. Any of the standard comparison operators (
<=) will work. The
get_datasets method will give you a
PaginatedCollection. You can iterate over the
PaginatedCollection to get your dataset.
from labelbox import Dataset datasets_x = client.get_datasets(where=Dataset.name == "X") for x in datasets_x: print(x)
Combine comparisons using logical expressions to filter projects. Currently, the
where clause supports the logical AND operator.
from labelbox import Project, Client client = Client() projects = client.get_projects(where=(Project.name == "X") & (Project.description == "Y")) for x in projects: print(x)
Some calls to the API return a very large number of results. To prevent too many results from being returned at once, the Labelbox API limits the number of returned objects and the Python SDK automatically creates a PaginatedCollection instead.
When a PaginatedCollection object is created, nothing is actually fetched from the server. You cannot get a count of objects in the relationship from a PaginatedCollection nor can you access objects within it like you would a list (using squared-bracket indexing).
- For both the top-level object fetch,
client.get_projects(), and the relationship call,
PaginatedCollectionobject is returned. This
PaginatedCollectionobject takes care of the paginated fetching.
projects = client.get_projects() type(projects)
projects = list(projects) type(projects)
Note that nothing is fetched immediately when the
PaginatedCollectionobject is created.
Round-trips to the server are made only as you iterate through a
PaginatedCollection. In this code block, that happens when a
listis initialized with a
PaginatedCollection, and when a
PaginatedCollectionis iterated over in a for-loop.
project = projects datasets = project.datasets() type(datasets) for dataset in datasets: dataset.name
- You cannot get a count of objects in the relationship from a
PaginatedCollectionnor can you access objects within it like you would a list (using squared-bracket indexing). You can only iterate over it.
Be careful about converting a PaginatedCollection into a list. This will cause all objects in that collection to be fetched from the server.
In cases when you need only some objects, you can use two convenient methods:
data_row = dataset.data_rows().get_one() # returns first result # or data_rows = dateset.date_rows.get_many(10) # returns list of 10 first results
You can still iterate over a PaginatedCollection object as with a normal iterator object:
data_rows = dataset.data_rows() for data_row in data_rows: # your business logic pass