From 348c8a77f3abd613760a8126de62b18dd6051b47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Saugat=20Pachhai=20=28=E0=A4=B8=E0=A5=8C=E0=A4=97=E0=A4=BE?= =?UTF-8?q?=E0=A4=A4=29?= Date: Thu, 11 Jan 2024 20:16:03 +0545 Subject: [PATCH 1/2] TqdmCallback: add support for custom tqdm subclasses This will allow customizing the tqdm progressbar used for the callback, and extending (since all `tqdm_kwargs` is passed to the underlying implementation.) Eg: in dvc, we have custom logic to align all the _progress_ _bars_ of tqdm, even if they have different description length. --- fsspec/callbacks.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/fsspec/callbacks.py b/fsspec/callbacks.py index 3e312566d..ef3c72a45 100644 --- a/fsspec/callbacks.py +++ b/fsspec/callbacks.py @@ -243,7 +243,9 @@ class TqdmCallback(Callback): tqdm_kwargs : dict, (optional) Any argument accepted by the tqdm constructor. See the `tqdm doc `_. - Will be forwarded to tqdm. + Will be forwarded to `tqdm_cls`. + tqdm_cls: (optional) + subclass of `tqdm.tqdm`. If not passed, it will default to `tqdm.tqdm`. Examples -------- @@ -277,7 +279,7 @@ def __init__(self, tqdm_kwargs=None, *args, **kwargs): "Using TqdmCallback requires tqdm to be installed" ) from exce - self._tqdm_cls = tqdm + self._tqdm_cls = kwargs.pop("tqdm_cls", tqdm) self._tqdm_kwargs = tqdm_kwargs or {} self.tqdm = None super().__init__(*args, **kwargs) From 1ef9dcfc0a9f341afbd8701165c4e7b382a34713 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Saugat=20Pachhai=20=28=E0=A4=B8=E0=A5=8C=E0=A4=97=E0=A4=BE?= =?UTF-8?q?=E0=A4=A4=29?= Date: Tue, 23 Jan 2024 14:45:56 +0545 Subject: [PATCH 2/2] add tqdm_cls docs --- fsspec/callbacks.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/fsspec/callbacks.py b/fsspec/callbacks.py index ef3c72a45..7ca99ca6a 100644 --- a/fsspec/callbacks.py +++ b/fsspec/callbacks.py @@ -268,6 +268,28 @@ class TqdmCallback(Callback): recursive=True, callback=TqdmCallback(tqdm_kwargs={"desc": "Your tqdm description"}), ) + + You can also customize the progress bar by passing a subclass of `tqdm`. + + .. code-block:: python + + class TqdmFormat(tqdm): + '''Provides a `total_time` format parameter''' + @property + def format_dict(self): + d = super().format_dict + total_time = d["elapsed"] * (d["total"] or 0) / max(d["n"], 1) + d.update(total_time=self.format_interval(total_time) + " in total") + return d + + >>> with TqdmCallback( + tqdm_kwargs={ + "desc": "desc", + "bar_format": "{total_time}: {percentage:.0f}%|{bar}{r_bar}", + }, + tqdm_cls=TqdmFormat, + ) as callback: + fs.upload(".", path2distant_data, recursive=True, callback=callback) """ def __init__(self, tqdm_kwargs=None, *args, **kwargs):