Skip to content

Commit cff7413

Browse files
committed
add callable support for bar_format argument
Signed-off-by: Stephen L. <[email protected]>
1 parent c2941fb commit cff7413

2 files changed

Lines changed: 38 additions & 4 deletions

File tree

README.rst

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,9 @@ Parameters
271271
Exponential moving average smoothing factor for speed estimates
272272
(ignored in GUI mode). Ranges from 0 (average speed) to 1
273273
(current/instantaneous speed) [default: 0.3].
274-
* bar_format : str, optional
274+
* bar_format : str or callable, optional
275275
Specify a custom bar string formatting. May impact performance.
276+
Can be a callable that will handle bar display.
276277
If unspecified, will use '{l_bar}{bar}{r_bar}', where l_bar is
277278
'{desc}{percentage:3.0f}%|' and r_bar is
278279
'| {n_fmt}/{total_fmt} [{elapsed_str}<{remaining_str}, {rate_fmt}]'
@@ -462,6 +463,34 @@ It is recommend to use ``miniters=1`` whenever there is potentially
462463
large differences in iteration speed (e.g. downloading a file over
463464
a patchy connection).
464465

466+
Integration in a GUI
467+
~~~~~~~~~~~~~~~~~~~~
468+
``tqdm`` can easily be integrated in your own GUI by providing ``bar_format`` with
469+
a callback function that will update your GUI bar display:
470+
471+
.. code:: python
472+
473+
from tqdm import tqdm
474+
from time import sleep
475+
from awesome import GUI
476+
477+
class my_gui_bar(object):
478+
'''Toy GUI bar'''
479+
def __init__(self):
480+
self.gui_bar = GUI()
481+
self.gui_bar.init()
482+
# etc.
483+
484+
def update(self, bar_args={}):
485+
'''Callback for tqdm to update the bar display'''
486+
self.gui_bar.set_text = "{n_fmt}/{n_total} [{elapsed}>{remaining}]".format(bar_args)
487+
self.gui_bar.set_progress = bar_args['n']
488+
489+
gbar = my_gui_bar()
490+
for i in tqdm(range(100), bar_format=gbar.update):
491+
sleep(0.1)
492+
493+
465494
Pandas Integration
466495
~~~~~~~~~~~~~~~~~~
467496

tqdm/_tqdm.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,9 @@ def format_meter(n, total, elapsed, ncols=None, prefix='',
135135
rate : float, optional
136136
Manual override for iteration rate.
137137
If [default: None], uses n/elapsed.
138-
bar_format : str, optional
138+
bar_format : str or callable, optional
139139
Specify a custom bar string formatting. May impact performance.
140+
Can be a callable that will handle bar display.
140141
[default: '{l_bar}{bar}{r_bar}'], where l_bar is
141142
'{desc}{percentage:3.0f}%|' and r_bar is
142143
'| {n_fmt}/{total_fmt} [{elapsed_str}<{remaining_str}, {rate_fmt}]'
@@ -216,7 +217,10 @@ def format_meter(n, total, elapsed, ncols=None, prefix='',
216217
}
217218

218219
# Interpolate supplied bar format with the dict
219-
if '{bar}' in bar_format:
220+
if hasattr(bar_format, '__call__'):
221+
# Callback user provided function/method to handle display
222+
bar_format(bar_args)
223+
elif '{bar}' in bar_format:
220224
# Format left/right sides of the bar, and format the bar
221225
# later in the remaining space (avoid breaking display)
222226
l_bar_user, r_bar_user = bar_format.split('{bar}')
@@ -385,8 +389,9 @@ def __init__(self, iterable=None, desc=None, total=None, leave=True,
385389
Exponential moving average smoothing factor for speed estimates
386390
(ignored in GUI mode). Ranges from 0 (average speed) to 1
387391
(current/instantaneous speed) [default: 0.3].
388-
bar_format : str, optional
392+
bar_format : str or callable, optional
389393
Specify a custom bar string formatting. May impact performance.
394+
Can be a callable that will handle bar display.
390395
If unspecified, will use '{l_bar}{bar}{r_bar}', where l_bar is
391396
'{desc}{percentage:3.0f}%|' and r_bar is
392397
'| {n_fmt}/{total_fmt} [{elapsed_str}<{remaining_str}, {rate_fmt}]'

0 commit comments

Comments
 (0)