Skip to content

Commit f21699a

Browse files
authored
TqdmCallback: add support for custom tqdm subclasses (#1497)
1 parent a408121 commit f21699a

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

fsspec/callbacks.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,9 @@ class TqdmCallback(Callback):
243243
tqdm_kwargs : dict, (optional)
244244
Any argument accepted by the tqdm constructor.
245245
See the `tqdm doc <https://tqdm.github.io/docs/tqdm/#__init__>`_.
246-
Will be forwarded to tqdm.
246+
Will be forwarded to `tqdm_cls`.
247+
tqdm_cls: (optional)
248+
subclass of `tqdm.tqdm`. If not passed, it will default to `tqdm.tqdm`.
247249
248250
Examples
249251
--------
@@ -266,6 +268,28 @@ class TqdmCallback(Callback):
266268
recursive=True,
267269
callback=TqdmCallback(tqdm_kwargs={"desc": "Your tqdm description"}),
268270
)
271+
272+
You can also customize the progress bar by passing a subclass of `tqdm`.
273+
274+
.. code-block:: python
275+
276+
class TqdmFormat(tqdm):
277+
'''Provides a `total_time` format parameter'''
278+
@property
279+
def format_dict(self):
280+
d = super().format_dict
281+
total_time = d["elapsed"] * (d["total"] or 0) / max(d["n"], 1)
282+
d.update(total_time=self.format_interval(total_time) + " in total")
283+
return d
284+
285+
>>> with TqdmCallback(
286+
tqdm_kwargs={
287+
"desc": "desc",
288+
"bar_format": "{total_time}: {percentage:.0f}%|{bar}{r_bar}",
289+
},
290+
tqdm_cls=TqdmFormat,
291+
) as callback:
292+
fs.upload(".", path2distant_data, recursive=True, callback=callback)
269293
"""
270294

271295
def __init__(self, tqdm_kwargs=None, *args, **kwargs):
@@ -277,7 +301,7 @@ def __init__(self, tqdm_kwargs=None, *args, **kwargs):
277301
"Using TqdmCallback requires tqdm to be installed"
278302
) from exce
279303

280-
self._tqdm_cls = tqdm
304+
self._tqdm_cls = kwargs.pop("tqdm_cls", tqdm)
281305
self._tqdm_kwargs = tqdm_kwargs or {}
282306
self.tqdm = None
283307
super().__init__(*args, **kwargs)

0 commit comments

Comments
 (0)