Skip to content

Commit 3fbcd11

Browse files
committed
Use progressbar2 instead of tqdm
This fixes #18 and works beautifully in interactive Jupyter, but not with Jupyter Book (every update of the progress bars get included as a new line).
1 parent 1298254 commit 3fbcd11

File tree

3 files changed

+28
-40
lines changed

3 files changed

+28
-40
lines changed

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ classifiers = [
2929

3030
dependencies = [
3131
"click >= 8.0",
32+
"progressbar2 >= 4.0",
3233
"requests >= 2.24",
33-
"tqdm >= 4.0",
3434
]
3535

3636
[project.optional-dependencies]

src/cjdk/_cache.py

+12-16
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from contextlib import contextmanager
99
from pathlib import Path
1010

11-
from tqdm.auto import tqdm
11+
import progressbar
1212

1313
from . import _compat
1414

@@ -211,13 +211,10 @@ def _swap_in_fetched_file(target, tmpfile, timeout, progress=False):
211211
WINDOWS_ERROR_ACCESS_DENIED = 5
212212

213213
target.parent.mkdir(parents=True, exist_ok=True)
214-
with tqdm(
215-
desc="Waiting for another process",
216-
# Intentionally not showing total; percentage makes no sense here
217-
disable=(None if progress else True),
218-
unit="s",
219-
delay=0.5,
220-
) as tq:
214+
barclass = progressbar.ProgressBar if progress else progressbar.NullBar
215+
with barclass(
216+
max_value=progressbar.UnknownLength, prefix="File busy; waiting "
217+
) as pbar:
221218
for wait_seconds in _backoff_seconds(0.001, 0.5, timeout):
222219
try:
223220
tmpfile.replace(target)
@@ -228,7 +225,7 @@ def _swap_in_fetched_file(target, tmpfile, timeout, progress=False):
228225
and wait_seconds > 0
229226
):
230227
time.sleep(wait_seconds)
231-
tq.update(wait_seconds)
228+
pbar.update()
232229
continue
233230
raise
234231
else:
@@ -246,12 +243,11 @@ def _add_url_file(keydir, key_url):
246243

247244

248245
def _wait_for_dir_to_vanish(directory, timeout, progress=True):
249-
with tqdm(
250-
desc="Waiting for another download",
251-
# Intentionally not showing total; percentage makes no sense here
252-
disable=(None if progress else True),
253-
unit="s",
254-
) as tq:
246+
barclass = progressbar.ProgressBar if progress else progressbar.NullBar
247+
with barclass(
248+
max_value=progressbar.UnknownLength,
249+
prefix="Already downloading; waiting ",
250+
) as pbar:
255251
for wait_seconds in _backoff_seconds(0.001, 0.5, timeout):
256252
if not directory.is_dir():
257253
return
@@ -260,7 +256,7 @@ def _wait_for_dir_to_vanish(directory, timeout, progress=True):
260256
f"Timeout while waiting for directory {directory} to disappear"
261257
)
262258
time.sleep(wait_seconds)
263-
tq.update(wait_seconds)
259+
pbar.update()
264260

265261

266262
def _backoff_seconds(initial_interval, max_interval, max_total, factor=1.5):

src/cjdk/_download.py

+15-23
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
from pathlib import Path
99
from urllib.parse import urlparse
1010

11+
import progressbar
1112
import requests
12-
from tqdm.auto import tqdm
1313

1414
from . import _compat
1515

@@ -79,18 +79,17 @@ def download_file(
7979

8080
response = requests.get(url, stream=True)
8181
response.raise_for_status()
82-
size = int(response.headers.get("content-length", None))
83-
with tqdm(
84-
desc="Downloading",
85-
total=size,
86-
disable=(None if progress else True),
87-
unit="B",
88-
unit_scale=True,
89-
) as tq:
82+
total = int(response.headers.get("content-length", None))
83+
if not total:
84+
total = progressbar.UnknownLength
85+
barclass = progressbar.DataTransferBar if progress else progressbar.NullBar
86+
size = 0
87+
with barclass(max_value=total, prefix="Download ") as pbar:
9088
with open(dest, "wb") as outfile:
9189
for chunk in response.iter_content(chunk_size=16384):
9290
outfile.write(chunk)
93-
tq.update(len(chunk))
91+
size += len(chunk)
92+
pbar.update(size)
9493

9594
if checkfunc:
9695
checkfunc(dest)
@@ -99,13 +98,8 @@ def download_file(
9998
def _extract_zip(destdir, srcfile, progress=True):
10099
with zipfile.ZipFile(srcfile) as zf:
101100
infolist = zf.infolist()
102-
for member in tqdm(
103-
infolist,
104-
desc="Extracting",
105-
total=len(infolist),
106-
disable=(None if progress else True),
107-
unit="files",
108-
):
101+
barclass = progressbar.ProgressBar if progress else progressbar.NullBar
102+
for member in barclass(prefix="Extract ")(infolist):
109103
extracted = Path(zf.extract(member, destdir))
110104

111105
# Recover executable bits; see https://stackoverflow.com/a/46837272
@@ -116,10 +110,8 @@ def _extract_zip(destdir, srcfile, progress=True):
116110

117111
def _extract_tgz(destdir, srcfile, progress=True):
118112
with tarfile.open(srcfile, "r:gz", bufsize=65536) as tf:
119-
for member in tqdm(
120-
tf,
121-
desc="Extracting",
122-
disable=(None if progress else True),
123-
unit="files",
124-
):
113+
barclass = progressbar.ProgressBar if progress else progressbar.NullBar
114+
for member in barclass(
115+
prefix="Extract ", max_value=progressbar.UnknownLength
116+
)(tf):
125117
tf.extract(member, destdir)

0 commit comments

Comments
 (0)