@@ -124,40 +124,44 @@ def format_meter(n, total, elapsed, ncols=None, prefix='',
124124
125125 if bar_format :
126126 # Unpack variables if it's a list
127- if isinstance (bar_format , list ):
128- bar_format , custom_symbols = bar_format
129- # Custom bar formatting
130- # Populate a dict with all available progress indicators
131- bar_args = {'n' : n ,
132- 'n_fmt' : n_fmt ,
133- 'total' : total ,
134- 'total_fmt' : total_fmt ,
135- 'percentage' : percentage ,
136- 'rate' : rate if inv_rate is None else inv_rate ,
137- 'rate_noinv' : rate ,
138- 'rate_noinv_fmt' : ((format_sizeof (rate )
139- if unit_scale else
140- '{0:5.2f}' .format (rate ))
141- if rate else '?' ) + unit + '/s' ,
142- 'rate_fmt' : rate_fmt ,
143- 'elapsed' : elapsed_str ,
144- 'remaining' : remaining_str ,
145- 'l_bar' : l_bar ,
146- 'r_bar' : r_bar ,
147- 'desc' : prefix if prefix else '' ,
148- # 'bar': full_bar # replaced by procedure below
149- }
150-
151- # Interpolate supplied bar format with the dict
152- if '{bar}' in bar_format :
153- # Format left/right sides of the bar, and format the bar
154- # later in the remaining space (avoid breaking display)
155- l_bar_user , r_bar_user = bar_format .split ('{bar}' )
156- l_bar = l_bar_user .format (** bar_args )
157- r_bar = r_bar_user .format (** bar_args )
127+ if isinstance (bar_format , dict ):
128+ bar_format_template = bar_format .get ('template' , None )
158129 else :
159- # Else no progress bar, we can just format and return
160- return bar_format .format (** bar_args )
130+ bar_format_template = bar_format
131+
132+ if bar_format_template :
133+ # Custom bar formatting
134+ # Populate a dict with all available progress indicators
135+ bar_args = {'n' : n ,
136+ 'n_fmt' : n_fmt ,
137+ 'total' : total ,
138+ 'total_fmt' : total_fmt ,
139+ 'percentage' : percentage ,
140+ 'rate' : rate if inv_rate is None else inv_rate ,
141+ 'rate_noinv' : rate ,
142+ 'rate_noinv_fmt' : ((format_sizeof (rate )
143+ if unit_scale else
144+ '{0:5.2f}' .format (rate ))
145+ if rate else '?' ) + unit + '/s' ,
146+ 'rate_fmt' : rate_fmt ,
147+ 'elapsed' : elapsed_str ,
148+ 'remaining' : remaining_str ,
149+ 'l_bar' : l_bar ,
150+ 'r_bar' : r_bar ,
151+ 'desc' : prefix if prefix else '' ,
152+ # 'bar': full_bar # replaced by procedure below
153+ }
154+
155+ # Interpolate supplied bar format with the dict
156+ if '{bar}' in bar_format_template :
157+ # Format left/right sides of the bar, and format the bar
158+ # later in the remaining space (avoid breaking display)
159+ l_bar_user , r_bar_user = bar_format_template .split ('{bar}' )
160+ l_bar = l_bar_user .format (** bar_args )
161+ r_bar = r_bar_user .format (** bar_args )
162+ else :
163+ # Else no progress bar, we can just format and return
164+ return bar_format_template .format (** bar_args )
161165
162166 # Formatting progress bar
163167 # space available for bar's display
@@ -166,14 +170,14 @@ def format_meter(n, total, elapsed, ncols=None, prefix='',
166170
167171 # custom symbols format
168172 # need to provide both ascii and unicode versions of custom symbols
169- if custom_symbols :
173+ if bar_format and isinstance ( bar_format , dict ) :
170174 # get ascii or unicode template
171175 if ascii :
172- c_symb = custom_symbols [ 1 ]
176+ c_symb = bar_format [ 'symbols' ]. get ( 'ascii' , list ( "123456789#" ))
173177 else :
174- c_symb = custom_symbols [ 2 ]
178+ c_symb = bar_format [ 'symbols' ]. get ( 'unicode' , map ( _unich , range ( 0x258F , 0x2587 , - 1 )))
175179 # looping symbols: just update the symbol animation at each iteration
176- if custom_symbols [ 0 ] == 'loop' :
180+ if bar_format [ 'symbols' ]. get ( 'loop' , False ) :
177181 # increment one step in the animation at each step
178182 bar = c_symb [divmod (n , len (c_symb ))[1 ]]
179183 frac_bar = ''
@@ -229,57 +233,28 @@ def format_meter(n, total, elapsed, ncols=None, prefix='',
229233 n_fmt , unit , elapsed_str , rate_fmt )
230234
231235 def __init__ (self , * args , ** kwargs ):
236+ """
237+ bar_format: str/dict, optional
238+ Can either be a string, or a dict for more complex templating.
239+ Format: {'template': '{l_bar}{bar}{r_bar}',
240+ 'symbols': {'unicode': ['1', '2', '3', '4', '5', '6'],
241+ 'ascii': ['1', '2', '3'],
242+ 'loop': False}
243+ 'symbols_indeterminate': {'unicode': ....
244+ }
245+ """
232246 # get bar_format
233247 bar_format = kwargs .get ('bar_format' , None )
234-
235- # Custom symbols extraction
236- custom_symbols = None
237- if bar_format :
238- looping = None
239- c_symbols_ascii = None
240- c_symbols_unicode = None
241- found_tag = False
242- for tag in ['bar_symbols' , 'bar_symbols_ascii' , 'bar_symbols_loop' , 'bar_symbols_loop_ascii' ]:
243- start_tag = '{' + tag + '}'
244- end_tag = '{/' + tag + '}'
245- # Check if tag is found in the template
246- if start_tag in bar_format and end_tag in bar_format :
247- found_tag = True
248- # Extract custom symbols enclosed by tags, with the first character being the separator.
249- # Eg, extract_symbols('before{start},1,2,3,4,#{end}after', '{start}', '{end}')
250- start = bar_format .find (start_tag )
251- start_content = start + len (start_tag )
252- end_content = bar_format .find (end_tag )
253- end = end_content + len (end_tag )
254- sep = bar_format [start_content :start_content + 1 ]
255- c_symbols = bar_format [start_content + 1 :end_content ].split (sep )
256- # Cleanup all weird tags from bar_format else .format() crash
257- bar_format = bar_format [:start ] + bar_format [end :]
258-
259- if 'ascii' in tag :
260- c_symbols_ascii = c_symbols
261- else :
262- c_symbols_unicode = c_symbols
263-
264- # Looping symbol?
265- if 'loop' in tag :
266- looping = True
267- else :
268- looping = False
269-
270- # Compile the ascii/unicode bars in a nice argument for format_meter
271- if found_tag :
272- custom_symbols = ['loop' if looping else 'bar' , c_symbols_ascii , c_symbols_unicode ]
248+
249+ if bar_format and isinstance (bar_format , dict ):
250+ kwargs ['bar_format' ] = bar_format .get ('template' , None )
273251
274252 # Do rest of init with cleaned up bar_format
275- kwargs ['bar_format' ] = bar_format
276253 super (tqdm_custom , self ).__init__ (* args , ** kwargs )
277254
278255 # Store the arguments
279- if custom_symbols is not None :
280- self .bar_format = [bar_format , custom_symbols ]
281- else :
282- self .bar_format = bar_format
256+ bar_format ['template' ] = self .bar_format
257+ self .bar_format = bar_format
283258
284259
285260def tcrange (* args , ** kwargs ):
0 commit comments