Skip to content

Commit fefee3a

Browse files
committed
update to have optional include metadata field
1 parent 8e0c042 commit fefee3a

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

labelbox/schema/batch.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def remove_queued_data_rows(self) -> None:
7777
},
7878
experimental=True)
7979

80-
def export_data_rows(self, timeout_seconds=120) -> Generator:
80+
def export_data_rows(self, timeout_seconds=120, include_metadata: bool=False) -> Generator:
8181
""" Returns a generator that produces all data rows that are currently
8282
in this batch.
8383
@@ -92,22 +92,23 @@ def export_data_rows(self, timeout_seconds=120) -> Generator:
9292
LabelboxError: if the export fails or is unable to download within the specified time.
9393
"""
9494
id_param = "batchId"
95-
query_str = """mutation GetBatchDataRowsExportUrlPyApi($%s: ID!)
96-
{exportBatchDataRows(data:{batchId: $%s }) {downloadUrl createdAt status}}
97-
""" % (id_param, id_param)
95+
metadata_param = "includeMetadataInput"
96+
query_str = """mutation GetBatchDataRowsExportUrlPyApi($%s: ID!, $%s: Boolean!)
97+
{exportBatchDataRows(data:{batchId: $%s , includeMetadataInput: $%s}) {downloadUrl createdAt status}}
98+
""" % (id_param, metadata_param, id_param, metadata_param)
9899
sleep_time = 2
99100
while True:
100-
res = self.client.execute(query_str, {id_param: self.uid})
101+
res = self.client.execute(query_str, {id_param: self.uid, metadata_param: include_metadata})
101102
res = res["exportBatchDataRows"]
102103
if res["status"] == "COMPLETE":
103104
download_url = res["downloadUrl"]
104105
response = requests.get(download_url)
105106
response.raise_for_status()
106107
reader = ndjson.reader(StringIO(response.text))
107-
# TODO: Update result to parse metadataFields when resolver returns
108108
return (Entity.DataRow(self.client, {
109-
**result, 'metadataFields': [],
110-
'customMetadata': []
109+
**result,
110+
'customMetadata': result['metadata'],
111+
'metadataFields': result['metadataFields']
111112
}) for result in reader)
112113
elif res["status"] == "FAILED":
113114
raise LabelboxError("Data row export failed.")

labelbox/schema/dataset.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ def data_row_for_external_id(self, external_id) -> "DataRow":
413413
external_id)
414414
return data_rows[0]
415415

416-
def export_data_rows(self, timeout_seconds=120) -> Generator:
416+
def export_data_rows(self, timeout_seconds=120, include_metadata: bool=False) -> Generator:
417417
""" Returns a generator that produces all data rows that are currently
418418
attached to this dataset.
419419
@@ -428,22 +428,23 @@ def export_data_rows(self, timeout_seconds=120) -> Generator:
428428
LabelboxError: if the export fails or is unable to download within the specified time.
429429
"""
430430
id_param = "datasetId"
431-
query_str = """mutation GetDatasetDataRowsExportUrlPyApi($%s: ID!)
432-
{exportDatasetDataRows(data:{datasetId: $%s }) {downloadUrl createdAt status}}
433-
""" % (id_param, id_param)
431+
metadata_param = "includeMetadataInput"
432+
query_str = """mutation GetDatasetDataRowsExportUrlPyApi($%s: ID!, $%s: Boolean!)
433+
{exportDatasetDataRows(data:{datasetId: $%s , includeMetadataInput: $%s}) {downloadUrl createdAt status}}
434+
""" % (id_param, metadata_param, id_param, metadata_param)
434435
sleep_time = 2
435436
while True:
436-
res = self.client.execute(query_str, {id_param: self.uid})
437+
res = self.client.execute(query_str, {id_param: self.uid, metadata_param: include_metadata})
437438
res = res["exportDatasetDataRows"]
438439
if res["status"] == "COMPLETE":
439440
download_url = res["downloadUrl"]
440441
response = requests.get(download_url)
441442
response.raise_for_status()
442443
reader = ndjson.reader(StringIO(response.text))
443-
# TODO: Update result to parse metadataFields when resolver returns
444444
return (Entity.DataRow(self.client, {
445-
**result, 'metadataFields': [],
446-
'customMetadata': []
445+
**result,
446+
'customMetadata': result['metadata'],
447+
'metadataFields': result['metadataFields']
447448
}) for result in reader)
448449
elif res["status"] == "FAILED":
449450
raise LabelboxError("Data row export failed.")
@@ -457,3 +458,5 @@ def export_data_rows(self, timeout_seconds=120) -> Generator:
457458
logger.debug("Dataset '%s' data row export, waiting for server...",
458459
self.uid)
459460
time.sleep(sleep_time)
461+
462+

labelbox/schema/project.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def labels(self, datasets=None, order_by=None) -> PaginatedCollection:
186186
["project", "labels"], Label)
187187

188188
def export_queued_data_rows(self,
189-
timeout_seconds=120) -> List[Dict[str, str]]:
189+
timeout_seconds=120, include_metadata: bool=False) -> List[Dict[str, str]]:
190190
""" Returns all data rows that are currently enqueued for this project.
191191
192192
Args:
@@ -197,12 +197,13 @@ def export_queued_data_rows(self,
197197
LabelboxError: if the export fails or is unable to download within the specified time.
198198
"""
199199
id_param = "projectId"
200-
query_str = """mutation GetQueuedDataRowsExportUrlPyApi($%s: ID!)
201-
{exportQueuedDataRows(data:{projectId: $%s }) {downloadUrl createdAt status} }
202-
""" % (id_param, id_param)
200+
metadata_param = "includeMetadataInput"
201+
query_str = """mutation GetQueuedDataRowsExportUrlPyApi($%s: ID!, $%s: Boolean!)
202+
{exportQueuedDataRows(data:{projectId: $%s , includeMetadataInput: $%s}) {downloadUrl createdAt status} }
203+
""" % (id_param, metadata_param, id_param, metadata_param)
203204
sleep_time = 2
204205
while True:
205-
res = self.client.execute(query_str, {id_param: self.uid})
206+
res = self.client.execute(query_str, {id_param: self.uid, metadata_param: include_metadata})
206207
res = res["exportQueuedDataRows"]
207208
if res["status"] == "COMPLETE":
208209
download_url = res["downloadUrl"]

0 commit comments

Comments
 (0)