@@ -23,121 +23,172 @@ class PyLoadBar:
2323
2424 Settings:
2525
26- - Set custom start/completion messages by passing string to `msg_loading` and `msg_complete` respectively.
27-
28- - Toggle visual progress meter using `enable_display`.
29-
30- - Apply label to progress meter by passing string to `label: str | None` (set to `None` by default)
31- - Note that `enable_display: bool` must be set to `True` for this to take effect.
32-
33- - When calling :func:`start`:
34-
35- - Optionally set the total number of iterations to run using `iter_total: int`
36-
37- - Optionally set the minimum/maximum iteration length in seconds using the `min_iter: float` and `max_iter: float` parameters respectively
26+ - When instantiating a new :class:`PyLoadBar` object, you can set the following parameters:
27+ - Set custom start/completion messages by passing string to :param:`msg_loading` and :param:`msg_complete` respectively.
28+ - Toggle visual progress meter using :param:`enable_display`.
29+ - Apply label to progress meter by passing string to :param:`label` (set to `None` by default)
30+ - Note that :param:`enable_display` must be set to `True` for this to take effect.
31+
32+ - When calling :func:`start(self, iter_total, min_iter, max_iter)`:
33+ - Optionally set the total number of iterations to run using :param:`iter_total`, defaults to 5.
34+ - Optionally set the minimum/maximum iteration length in seconds using the :param:`min_iter` and :param:`max_iter` parameters respectively
3835 - Default values are `min_iter: 0.1` seconds and `max_iter: 1.0` seconds
39-
4036 """
4137
4238 def __init__ (self ,
43- msg_loading : str | None = 'Loading... ' ,
39+ msg_loading : str | None = 'Loading' ,
4440 msg_complete : str | None = 'Done!' ,
4541 label : str | None = None ,
46- enable_display : bool = True ):
42+ enable_display : bool = True ) -> None :
4743 """Initialize loading sequence with set configuration.
4844
4945 ---
5046
51- :param msg_loading: initial loading message string , defaults to 'Loading... '
47+ :param msg_loading: initial loading message, defaults to 'Loading'
5248 :type msg_loading: :class:`str` | None, optional
53- :param msg_complete: final message string displayed upon completion, defaults to 'Done!'
49+ :param msg_complete: final message displayed upon completion, defaults to 'Done!'
5450 :type msg_complete: :class:`str` | None, optional
5551 :param label: label displayed alongside progress bar, defaults to None
5652 :type label: :class:`str` | None, optional
57- :param enable_display: toggle visible progress meter, defaults to True
53+ :param enable_display: toggle visible progress meter, defaults to ` True`
5854 :type enable_display: :class:`bool`, optional
55+ :return: :class:`PyLoadBar` object
56+ :rtype: None
5957 """
6058
6159 self .msg_loading : str | None = msg_loading
6260 self .msg_complete : str | None = msg_complete
6361 self .label : str | None = label
6462 self .enable_display : bool = enable_display
6563
64+ if self .enable_display and self .msg_loading == 'Loading' :
65+ self .msg_loading = f'{ msg_loading } ...' # Add ellipses to default loading message.
66+
6667 def start (
6768 self ,
6869 iter_total : int = 5 ,
69- min_iter : float = 0.1 ,
70+ min_iter : float = 0.05 ,
7071 max_iter : float = 1.0 ,
7172 ) -> None :
7273 """Start loading sequence.
7374
7475 ---
7576
76- - Set the total number of iterations using the `iter_total: int` parameter
77+ Settings:
7778
78- - Set the minimum/maximum iteration length in seconds using the `min_iter: float` and `max_iter: float` parameters respectively
79- - Default values are `min_iter: 0.1` seconds and `max_iter: 1.0` seconds
79+ - Set the total number of iterations using the :param:`iter_total` parameter
80+
81+ - Set the minimum/maximum iteration length in seconds using the :param:`min_iter` and :param:`max_iter` parameters respectively
82+ - Default values are `min_iter` = 0.1 seconds and `max_iter` = 1.0 seconds
83+ - Time of each iteration is randomized between values of :param:`min_iter` and :param:`max_iter`
8084
8185 ---
8286
83- :param iter_total: number of iter_total until completion , defaults to 5
87+ :param iter_total: total amount of iterations to run , defaults to 5
8488 :type iter_total: :class:`int`, optional
85- :param min_iter: minimum possible time to complete an iteration, defaults to 1
89+ :param min_iter: minimum possible time to complete an iteration, defaults to 0.05 seconds
8690 :type min_iter: :class:`int`, optional
87- :param max_iter: maximum possible time to complete an iteration, defaults to 5
91+ :param max_iter: maximum possible time to complete an iteration, defaults to 1.0 seconds
8892 :type max_iter: :class:`int`, optional
93+ :return: Loading sequence
94+ :rtype: None
8995 """
9096
91- if self .enable_display :
92- return self .__loadseq_A (iter_total , min_iter , max_iter )
97+ try :
98+ if self .enable_display :
99+ self .__loadseq_A (iter_total , min_iter , max_iter )
100+
101+ else :
102+ self .__loadseq_B (iter_total )
93103
94- else :
95- return self .__loadseq_B (iter_total )
104+ sleep (
105+ 0.5
106+ ) # Add small delay to allow user to read completion message.
107+
108+ except Exception as e :
109+ print (f'Error: { e } ' )
96110
97111 def __loadseq_A (self , iter_total : int , min_iter : float ,
98112 max_iter : float ) -> None :
99113 """
100- Run loading sequence and display progress with graphical progress bar.
114+ Run loading sequence and display current progress with graphical progress bar.
115+
116+ ---
117+
118+ Example:
119+
120+ ```python
121+ >>> bar = PyLoadBar(msg_loading='Loading', msg_complete='Done!', label='Progress') # Initialize loading sequence.
122+ >>> bar.start(iter_total=5, min_iter=0.1, max_iter=1.0) # Start loading sequence.
123+
124+ Loading...
125+
126+ Progress: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████▊| 5/5]
127+
128+ Done!
129+
130+ ```
101131
102- :param iter_total: total number of iter_total to run
132+ ---
133+
134+ :param iter_total: total amount of iterations to run
103135 :type iter_total: :class:`int`
104136 :param min_iter: minimum possible time (in seconds) an iteration can take
105137 :type min_iter: :class:`float`
106138 :param max_iter: maximum possible time (in seconds) an iteration can take
107139 :type max_iter: :class:`float`
140+ :return: Loading sequence with graphical progress bar.
141+ :rtype: None
108142 """
109143
110144 print (f'\n { self .msg_loading } \n ' )
111145
112- for iter in tqdm .trange (iter_total , desc = self .label ):
146+ # Start loading sequence.
147+ for iter in tqdm .trange (
148+ iter_total ,
149+ desc = self .label ,
150+ bar_format = '{l_bar}{bar}| {n_fmt}/{total_fmt}]' ):
113151 sleep (uniform (min_iter , max_iter ))
114152 iter -= 1
115153
116- print (f'\n { self .msg_complete } \n ' )
117-
118- # Add a small delay to allow user to see completion message.
119- sleep (0.5 )
154+ print (f'\n { self .msg_complete } \n ' ) # Print completion message.
120155
121156 def __loadseq_B (self , iter_total : int ) -> None :
122- """
123- Run non-graphical loading sequence.
157+ """Run loading sequence with animated text instead of graphical progress bar.
124158
125- :param iter_total: Amount of iter_total to run loading sequence
126- :type iter_total: :class:`int`
127- :return: nonvisual loading sequence
159+ ---
128160
129- """
161+ Example:
162+
163+ ```python
164+ >>> bar = PyLoadBar(msg_loading='Loading', msg_complete='Done!', enable_display=False) # Initialize loading sequence.
165+ >>> bar.start(iter_total=1) # Start loading sequence.
166+ >>> # Note that during actual use case, text is printed to same line with an animated dot sequence:
167+
168+ > Loading > Loading. > Loading.. > Loading...
130169
131- print (f'\n { self .msg_loading } ' , end = '' )
170+ Done!
171+
172+ ```
173+
174+ ---
175+
176+ :param iter_total: Amount of iterations to run loading sequence
177+ :type iter_total: :class:`int`
178+ :return: loading sequence with animated text instead of progress bar.
179+ :rtype: None
180+ """
132181
133182 for iter in range (iter_total ):
134- for _ in range (3 ):
183+ print (f'{ self .msg_loading } ' , end = '' , flush = True )
184+ sleep (0.3 )
185+
186+ for _ in range (3 ): # Animate dot sequence.
135187 print ('.' , end = '' )
136188 sleep (0.3 )
137- print ('\b \b \b ' , end = '' )
138- iter -= 1
139189
140- print (f'\n \n { self .msg_complete } \n ' )
190+ print ('\x1b [2K\r ' , end = '' , flush = True ) # Clear line.
191+
192+ iter -= 1 # Decrement iteration counter.
141193
142- # Add a small delay to allow user to see completion message.
143- sleep (0.5 )
194+ print (f'\n { self .msg_complete } \n ' ) # Print completion message.
0 commit comments