MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / _download_with_progress

Function _download_with_progress

monai/apps/utils.py:90–122  ·  view source on GitHub ↗

Retrieve file from `url` to `filepath`, optionally showing a progress bar.

(url: str, filepath: Path, progress: bool = True)

Source from the content-addressed store, hash-verified

88
89
90def _download_with_progress(url: str, filepath: Path, progress: bool = True) -> None:
91 """
92 Retrieve file from `url` to `filepath`, optionally showing a progress bar.
93 """
94 try:
95 if has_tqdm and progress:
96
97 class TqdmUpTo(tqdm):
98 """
99 Provides `update_to(n)` which uses `tqdm.update(delta_n)`.
100 Inspired by the example in https://github.com/tqdm/tqdm.
101 """
102
103 def update_to(self, b: int = 1, bsize: int = 1, tsize: int | None = None) -> None:
104 """
105 Args:
106 b: number of blocks transferred so far, default: 1.
107 bsize: size of each block (in tqdm units), default: 1.
108 tsize: total size (in tqdm units). if None, remains unchanged.
109 """
110 if tsize is not None:
111 self.total = tsize
112 self.update(b * bsize - self.n) # will also set self.n = b * bsize
113
114 with TqdmUpTo(unit="B", unit_scale=True, unit_divisor=1024, miniters=1, desc=_basename(filepath)) as t:
115 urlretrieve(url, filepath, reporthook=t.update_to)
116 else:
117 if not has_tqdm and progress:
118 warnings.warn("tqdm is not installed, will not show the downloading progress bar.")
119 urlretrieve(url, filepath)
120 except (URLError, HTTPError, ContentTooShortError, OSError) as e:
121 logger.error(f"Download failed from {url} to {filepath}.")
122 raise e
123
124
125def safe_extract_member(member, extract_to):

Callers 1

download_urlFunction · 0.85

Calls 2

TqdmUpToClass · 0.85
_basenameFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…