Skip to content

Commit 6eba50a

Browse files
committed
Add SDK method & test
1 parent c9d8265 commit 6eba50a

File tree

2 files changed

+84
-5
lines changed

2 files changed

+84
-5
lines changed

labelbox/schema/project.py

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,20 @@
1212

1313
from labelbox import utils
1414
from labelbox.exceptions import (InvalidQueryError, LabelboxError,
15-
ProcessingWaitTimeout, ResourceConflict)
15+
ProcessingWaitTimeout, ResourceConflict,
16+
ResourceNotFoundError)
1617
from labelbox.orm import query
1718
from labelbox.orm.db_object import DbObject, Deletable, Updateable
1819
from labelbox.orm.model import Entity, Field, Relationship
1920
from labelbox.pagination import PaginatedCollection
2021
from labelbox.schema.consensus_settings import ConsensusSettings
2122
from labelbox.schema.data_row import DataRow
23+
from labelbox.schema.export_params import ModelRunExportParams
2224
from labelbox.schema.media_type import MediaType
2325
from labelbox.schema.queue_mode import QueueMode
2426
from labelbox.schema.resource_tag import ResourceTag
27+
from labelbox.schema.task import Task
28+
from labelbox.schema.user import User
2529

2630
if TYPE_CHECKING:
2731
from labelbox import BulkImportRequest
@@ -283,10 +287,11 @@ def label_generator(self, timeout_seconds=600, **kwargs):
283287
return LBV1Converter.deserialize_video(json_data, self.client)
284288
return LBV1Converter.deserialize(json_data)
285289

286-
def export_labels(self,
287-
download=False,
288-
timeout_seconds=1800,
289-
**kwargs) -> Optional[Union[str, List[Dict[Any, Any]]]]:
290+
def export_labels_v2(
291+
self,
292+
download=False,
293+
timeout_seconds=1800,
294+
**kwargs) -> Optional[Union[str, List[Dict[Any, Any]]]]:
290295
""" Calls the server-side Label exporting that generates a JSON
291296
payload, and returns the URL to that payload.
292297
@@ -371,6 +376,66 @@ def _validate_datetime(string_date: str) -> bool:
371376
self.uid)
372377
time.sleep(sleep_time)
373378

379+
"""
380+
Creates a project run export task with the given params and returns the task.
381+
382+
>>> export_task = export_labels_v2("my_export_task", filter={"media_attributes": True})
383+
384+
"""
385+
386+
def export_labels_v2(self, task_name: str,
387+
params: Optional[ModelRunExportParams]) -> Task:
388+
_params = params or {}
389+
mutation_name = "exportDataRows"
390+
create_task_query_str = """mutation exportDataRowsPyApi($input: ExportDataRowsInput!){
391+
%s(input: $input) {taskId} }
392+
""" % (mutation_name)
393+
params = {
394+
"input": {
395+
"taskName": task_name,
396+
"filters": {
397+
"modelRunIds": [],
398+
"projectIds": [self.uid]
399+
},
400+
"params": {
401+
"includeAttachments":
402+
_params.get('include_attachments', False),
403+
"includeMediaAttributes":
404+
_params.get('include_media_attributes', False),
405+
"includeMetadata":
406+
_params.get('include_metadata_fields', False),
407+
"includeDataRowDetails":
408+
_params.get('include_data_row_details', False),
409+
"includeProjectDetails":
410+
_params.get('include_project_details', False),
411+
"includeLabels":
412+
_params.get('include_labels', False),
413+
"includePerformanceDetails":
414+
_params.get('include_performance_details', False),
415+
# Arguments locked based on exectuion context
416+
"includeModelRuns":
417+
False,
418+
},
419+
}
420+
}
421+
res = self.client.execute(
422+
create_task_query_str,
423+
params,
424+
)
425+
res = res[mutation_name]
426+
task_id = res["taskId"]
427+
user: User = self.client.get_user()
428+
tasks: List[Task] = list(
429+
user.created_tasks(where=Entity.Task.uid == task_id))
430+
# Cache user in a private variable as the relationship can't be
431+
# resolved due to server-side limitations (see Task.created_by)
432+
# for more info.
433+
if len(tasks) != 1:
434+
raise ResourceNotFoundError(Entity.Task, task_id)
435+
task: Task = tasks[0]
436+
task._user = user
437+
return task
438+
374439
def export_issues(self, status=None) -> str:
375440
""" Calls the server-side Issues exporting that
376441
returns the URL to that payload.

tests/integration/test_label.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@ def test_label_export(configured_project_with_label):
4343
# The new exporter doesn't work with the create_label mutation
4444

4545

46+
def test_label_export_v2(configured_project_with_label):
47+
project, _, _, label = configured_project_with_label
48+
label_id = label.uid
49+
# Wait for exporter to retrieve latest labels
50+
time.sleep(10)
51+
task_name = "test_label_export_v2"
52+
53+
task = project.export_labels_v2(task_name, filter={"project_details": True})
54+
assert task.name == task_name
55+
task.wait_till_done()
56+
assert task.status == "COMPLETE"
57+
# TODO: Download result and check it
58+
59+
4660
# TODO: Skipping this test in staging due to label not updating
4761
@pytest.mark.skipif(condition=os.environ['LABELBOX_TEST_ENVIRON'] == "onprem" or
4862
os.environ['LABELBOX_TEST_ENVIRON'] == "staging" or

0 commit comments

Comments
 (0)