[O] Allow disabling progress bar during download
This commit is contained in:
@@ -7,7 +7,7 @@ import requests
|
|||||||
import tqdm
|
import tqdm
|
||||||
|
|
||||||
|
|
||||||
def download_file(url: str, file: str | Path):
|
def download_file(url: str, file: str | Path, progress: bool = True):
|
||||||
"""
|
"""
|
||||||
Helper method handling downloading large files from `url` to `filename`.
|
Helper method handling downloading large files from `url` to `filename`.
|
||||||
Returns a pointer to `filename`.
|
Returns a pointer to `filename`.
|
||||||
@@ -32,11 +32,16 @@ def download_file(url: str, file: str | Path):
|
|||||||
tqdm_args['total'] = int(r.headers['content-length']) / 1024 / 1024
|
tqdm_args['total'] = int(r.headers['content-length']) / 1024 / 1024
|
||||||
|
|
||||||
with open(file, 'wb') as f:
|
with open(file, 'wb') as f:
|
||||||
pbar = tqdm.tqdm(unit=" MB", ncols=term_len,
|
pbar = None
|
||||||
bar_format='{desc} {rate_noinv_fmt} {remaining} [{bar}] {percentage:.0f}%', ascii=' #',
|
if progress:
|
||||||
desc=file.name[:bar_len].ljust(bar_len), **tqdm_args)
|
pbar = tqdm.tqdm(unit=" MB", ncols=term_len,
|
||||||
|
bar_format='{desc} {rate_noinv_fmt} {remaining} [{bar}] {percentage:.0f}%', ascii=' #',
|
||||||
|
desc=file.name[:bar_len].ljust(bar_len), **tqdm_args)
|
||||||
|
|
||||||
for chunk in r.iter_content(chunk_size=chunk_size):
|
for chunk in r.iter_content(chunk_size=chunk_size):
|
||||||
if chunk:
|
if chunk:
|
||||||
pbar.update(len(chunk) / 1024 / 1024)
|
if pbar:
|
||||||
|
pbar.update(len(chunk) / 1024 / 1024)
|
||||||
f.write(chunk)
|
f.write(chunk)
|
||||||
|
|
||||||
return file
|
return file
|
||||||
|
|||||||
Reference in New Issue
Block a user