Download pool/shard002/Python/0017720_progress_bar.py from Variable-role/sajaniemi_variable_dataset_large: direct link, hf CLI and curl.
- Browser
- Download file 965 Bytes
-
https://huggingface.co/datasets/Variable-role/sajaniemi_variable_dataset_large/resolve/main/pool/shard002/Python/0017720_progress_bar.py
- Command line
-
hf download hf://datasets/Variable-role/sajaniemi_variable_dataset_large/pool/shard002/Python/0017720_progress_bar.py
-
curl -L -o 0017720_progress_bar.py https://huggingface.co/datasets/Variable-role/sajaniemi_variable_dataset_large/resolve/main/pool/shard002/Python/0017720_progress_bar.py
965 Bytes
| from functools import partial | |
| import tqdm | |
| def tqdm_callback(N, notebook=True): | |
| """ | |
| Return a :module:`tqdm` progress bar expecting *N* iterations, | |
| either suitable with jupyter if *notebook* is true and for the | |
| terminal otherwise. The progress bar includes an additional method | |
| :function:`callback` (function of one ignored parameter) meant to | |
| be past as a callback function called to update the progress bar. | |
| """ | |
| if notebook: | |
| progress_bar = tqdm.tqdm_notebook(total=N) | |
| else: | |
| progress_bar = tqdm.tqdm(total=N) | |
| def callback(self, i): | |
| self.update() | |
| progress_bar.callback = partial(callback, progress_bar) | |
| return progress_bar | |
| if __name__ == '__main__': | |
| from time import sleep | |
| N = 10 | |
| def test(N, callback): | |
| for i in range(N): | |
| sleep(0.2) | |
| callback(i) | |
| with tqdm_callback(N, notebook=False) as progress_bar: | |
| test(N, progress_bar.callback) | |