Skip to content

Commit 9577c8a

Browse files
TimostTimothée Chehabmartindurant
authored
callbacks: add a tqdm callback (#931)
* callbacks: add a tqdm callback This commit adds a TqdmCallback class to display a progress bar using tqdm during fsspec operations. closes #923 Co-authored-by: Timothée Chehab <[email protected]> Co-authored-by: Martin Durant <[email protected]>
1 parent 534ea8d commit 9577c8a

File tree

5 files changed

+48
-1
lines changed

5 files changed

+48
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ environments. First, install conda with tox and tox-conda in a base environment
4040
used to configure a development environment and run tests.
4141

4242
First, setup a development conda environment via ``tox -e {env}`` where ``env`` is one of ``{py36,py37,py38,py39}``.
43-
This will install fspec dependencies, test & dev tools, and install fsspec in develop
43+
This will install fsspec dependencies, test & dev tools, and install fsspec in develop
4444
mode. You may activate the dev environment under ``.tox/{env}`` via ``conda activate .tox/{env}``.
4545

4646
### Testing

docs/source/api.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Base Classes
5151
fsspec.callbacks.Callback
5252
fsspec.callbacks.NoOpCallback
5353
fsspec.callbacks.DotPrinterCallback
54+
fsspec.callbacks.TqdmCallback
5455

5556
.. autoclass:: fsspec.spec.AbstractFileSystem
5657
:members:
@@ -96,6 +97,9 @@ Base Classes
9697
.. autoclass:: fsspec.callbacks.DotPrinterCallback
9798
:members:
9899

100+
.. autoclass:: fsspec.callbacks.TqdmCallback
101+
:members:
102+
99103
.. _implementations:
100104

101105
Built-in Implementations

docs/source/features.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,3 +399,5 @@ feature is new and experimental and supported by varying amounts in the
399399
backends.
400400

401401
See the docstrings in the callbacks module for further details.
402+
``fsspec.callbacks.TqdmCallback`` can be used to display a progress bar using
403+
tqdm.

fsspec/callbacks.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,44 @@ def call(self, **kwargs):
177177
print(self.chr, end="")
178178

179179

180+
class TqdmCallback(Callback):
181+
"""
182+
A callback to display a progress bar using tqdm
183+
184+
Examples
185+
--------
186+
>>> import fsspec
187+
>>> from fsspec.callbacks import TqdmCallback
188+
>>> fs = fsspec.filesystem("memory")
189+
>>> path2distant_data = "/your-path"
190+
>>> fs.upload(
191+
".",
192+
path2distant_data,
193+
recursive=True,
194+
callback=TqdmCallback(),
195+
)
196+
"""
197+
198+
def __init__(self, *args, **kwargs):
199+
try:
200+
import tqdm
201+
202+
self._tqdm = tqdm
203+
except ImportError as exce:
204+
raise ImportError(
205+
"Using TqdmCallback requires tqdm to be installed"
206+
) from exce
207+
super().__init__(*args, **kwargs)
208+
209+
def set_size(self, size):
210+
self.tqdm = self._tqdm.tqdm(desc="test", total=size)
211+
212+
def relative_update(self, inc=1):
213+
self.tqdm.update(inc)
214+
215+
def __del__(self):
216+
self.tqdm.close()
217+
self.tqdm = None
218+
219+
180220
_DEFAULT_CALLBACK = NoOpCallback()

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"fuse": ["fusepy"],
5555
"libarchive": ["libarchive-c"],
5656
"gui": ["panel"],
57+
"tqdm": ["tqdm"],
5758
},
5859
zip_safe=False,
5960
)

0 commit comments

Comments
 (0)