Skip to content

Commit

Permalink
Make progressbar for downloads optional
Browse files Browse the repository at this point in the history
  • Loading branch information
andreArtelt committed Mar 16, 2024
1 parent 701355e commit 9adfe7f
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions epyt_flow/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def plot_timeseries_prediction(y: np.ndarray, y_pred: np.ndarray,
plt.show()


def download_if_necessary(download_path: str, url: str) -> None:
def download_if_necessary(download_path: str, url: str, verbose: bool = True) -> None:
"""
Downloads a file from a given URL if it does not already exist in a given path.
Expand All @@ -116,18 +116,27 @@ def download_if_necessary(download_path: str, url: str) -> None:
the provided 'url' and stored in 'download_dir'.
url : `str`
Web-URL.
verbose : `bool`, optional
If True, a progress bar is shown while downloading the file.
The default is True.
"""
if not os.path.isfile(download_path):
response = requests.get(url, stream=True, allow_redirects=True, timeout=1000)
content_length = int(response.headers.get('content-length', 0))
with open(download_path, "wb") as file, tqdm(desc=download_path,
total=content_length,
unit='B',
unit_scale=True,
unit_divisor=1024) as progress_bar:
for data in response.iter_content(chunk_size=1024):
size = file.write(data)
progress_bar.update(size)
response = requests.get(url, stream=verbose, allow_redirects=True, timeout=1000)

if verbose is True:
content_length = int(response.headers.get('content-length', 0))
with open(download_path, "wb") as file, tqdm(desc=download_path,
total=content_length,
unit='B',
unit_scale=True,
unit_divisor=1024) as progress_bar:
for data in response.iter_content(chunk_size=1024):
size = file.write(data)
progress_bar.update(size)
else:
with open(download_path, "wb") as f_out:
f_out.write(response.content)


def create_path_if_not_exist(path_in: str) -> None:
Expand Down

0 comments on commit 9adfe7f

Please sign in to comment.