diff --git a/epyt_flow/utils.py b/epyt_flow/utils.py index 9e5c4a2..9021a9c 100644 --- a/epyt_flow/utils.py +++ b/epyt_flow/utils.py @@ -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. @@ -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: