Skip to content

Commit 2df204f

Browse files
authored
Merge pull request #876 from Labelbox/mno/x-0-fix-default-values
2 parents d0316ef + e10b35a commit 2df204f

File tree

3 files changed

+38
-16
lines changed

3 files changed

+38
-16
lines changed

labelbox/schema/model_run.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -456,14 +456,19 @@ def export_labels(
456456
457457
"""
458458

459-
def export_v2(self, task_name: str,
460-
params: Optional[ModelRunExportParams]) -> Task:
461-
_params = params or {}
459+
def export_v2(self,
460+
task_name: Optional[str] = None,
461+
params: Optional[ModelRunExportParams] = None) -> Task:
462462
mutation_name = "exportDataRowsInModelRun"
463463
create_task_query_str = """mutation exportDataRowsInModelRunPyApi($input: ExportDataRowsInModelRunInput!){
464464
%s(input: $input) {taskId} }
465465
""" % (mutation_name)
466-
params = {
466+
if (task_name is None):
467+
task_name = f'Export Data Rows in Model Run - {self.name}'
468+
469+
_params = params or ModelRunExportParams()
470+
471+
queryParams = {
467472
"input": {
468473
"taskName": task_name,
469474
"filters": {
@@ -490,7 +495,7 @@ def export_v2(self, task_name: str,
490495
}
491496
res = self.client.execute(
492497
create_task_query_str,
493-
params,
498+
queryParams,
494499
)
495500
res = res[mutation_name]
496501
task_id = res["taskId"]

labelbox/schema/project.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -393,18 +393,23 @@ def _validate_datetime(string_date: str) -> bool:
393393
394394
"""
395395

396-
def export_v2(self, task_name: str,
397-
params: Optional[ProjectExportParams]) -> Task:
398-
defaultParams: ProjectExportParams = {
396+
def export_v2(self,
397+
task_name: Optional[str] = None,
398+
params: Optional[ProjectExportParams] = None) -> Task:
399+
400+
if (task_name is None):
401+
task_name = f'Export Data Rows in Project - {self.name}'
402+
403+
_params = params or ProjectExportParams({
399404
"attachments": False,
400405
"media_attributes": False,
401406
"metadata_fields": False,
402407
"data_row_details": False,
403408
"project_details": False,
404409
"labels": False,
405410
"performance_details": False
406-
}
407-
_params: ProjectExportParams = params if params is not None else defaultParams
411+
})
412+
408413
mutation_name = "exportDataRowsInProject"
409414
create_task_query_str = """mutation exportDataRowsInProjectPyApi($input: ExportDataRowsInProjectInput!){
410415
%s(input: $input) {taskId} }

labelbox/schema/task.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import logging
22
import requests
33
import time
4-
from typing import TYPE_CHECKING, Callable, Optional, Dict, Any, List
4+
from typing import TYPE_CHECKING, Callable, Optional, Dict, Any, List, Union
5+
import ndjson
56

67
from labelbox.exceptions import ResourceNotFoundError
78
from labelbox.orm.db_object import DbObject
@@ -38,6 +39,7 @@ class Task(DbObject):
3839
status = Field.String("status")
3940
completion_percentage = Field.Float("completion_percentage")
4041
result_url = Field.String("result_url", "result")
42+
type = Field.String("type")
4143
_user: Optional["User"] = None
4244

4345
# Relationships
@@ -92,13 +94,16 @@ def errors(self) -> Optional[Dict[str, Any]]:
9294
return None
9395

9496
@property
95-
def result(self) -> List[Dict[str, Any]]:
97+
def result(self) -> Union[List[Dict[str, Any]], Dict[str, Any]]:
9698
""" Fetch the result for an import task.
9799
"""
98100
if self.status == "FAILED":
99101
raise ValueError(f"Job failed. Errors : {self.errors}")
100102
else:
101103
result = self._fetch_remote_json()
104+
if self.type == 'export-data-rows':
105+
return result
106+
102107
return [{
103108
'id': data_row['id'],
104109
'external_id': data_row.get('externalId'),
@@ -124,11 +129,18 @@ def _fetch_remote_json(self) -> Dict[str, Any]:
124129
def download_result():
125130
response = requests.get(self.result_url)
126131
response.raise_for_status()
127-
return response.json()
128-
129-
if self.name != 'JSON Import':
132+
try:
133+
return response.json()
134+
except Exception as e:
135+
pass
136+
try:
137+
return ndjson.loads(response.text)
138+
except Exception as e:
139+
raise ValueError("Failed to parse task JSON/NDJSON result.")
140+
141+
if self.name != 'JSON Import' and self.type != 'export-data-rows':
130142
raise ValueError(
131-
"Task result is only supported for `JSON Import` tasks."
143+
"Task result is only supported for `JSON Import` and `export` tasks."
132144
" Download task.result_url manually to access the result for other tasks."
133145
)
134146

0 commit comments

Comments
 (0)