44import os
55import time
66from typing import Any , Dict , List , BinaryIO
7+ from tqdm import tqdm # type: ignore
78
89import backoff
910import ndjson
@@ -25,6 +26,7 @@ class AnnotationImport(DbObject):
2526 input_file_url = Field .String ("input_file_url" )
2627 error_file_url = Field .String ("error_file_url" )
2728 status_file_url = Field .String ("status_file_url" )
29+ progress = Field .String ("progress" )
2830
2931 created_by = Relationship .ToOne ("User" , False , "created_by" )
3032
@@ -76,18 +78,30 @@ def statuses(self) -> List[Dict[str, Any]]:
7678 self .wait_until_done ()
7779 return self ._fetch_remote_ndjson (self .status_file_url )
7880
79- def wait_until_done (self , sleep_time_seconds : int = 10 ) -> None :
81+ def wait_until_done (self ,
82+ sleep_time_seconds : int = 10 ,
83+ show_progress : bool = False ) -> None :
8084 """Blocks import job until certain conditions are met.
8185 Blocks until the AnnotationImport.state changes either to
8286 `AnnotationImportState.FINISHED` or `AnnotationImportState.FAILED`,
8387 periodically refreshing object's state.
8488 Args:
85- sleep_time_seconds (str): a time to block between subsequent API calls
89+ sleep_time_seconds (int): a time to block between subsequent API calls
90+ show_progress (bool): should show progress bar
8691 """
92+ pbar = tqdm (total = 100 ,
93+ bar_format = "{n}% |{bar}| [{elapsed}, {rate_fmt}{postfix}]"
94+ ) if show_progress else None
8795 while self .state .value == AnnotationImportState .RUNNING .value :
8896 logger .info (f"Sleeping for { sleep_time_seconds } seconds..." )
8997 time .sleep (sleep_time_seconds )
9098 self .__backoff_refresh ()
99+ if self .progress and self .progress and pbar :
100+ pbar .update (int (self .progress .replace ("%" , "" )) - pbar .n )
101+
102+ if pbar :
103+ pbar .update (100 - pbar .n )
104+ pbar .close ()
91105
92106 @backoff .on_exception (
93107 backoff .expo ,
@@ -499,7 +513,7 @@ def create_from_objects(cls, client: "labelbox.Client", project_id: str,
499513 client: Labelbox Client for executing queries
500514 project_id: Project to import labels into
501515 name: Name of the import job. Can be used to reference the task later
502- labels: List of labels
516+ labels: List of labels
503517 Returns:
504518 LabelImport
505519 """
0 commit comments