std_out_err_redirect_tqdm#
- caf.toolkit.tqdm_utils.std_out_err_redirect_tqdm()#
Redirect stdout and stderr to tqdm.write.
Code copied from tqdm documentation: tqdm/tqdm
Redirect stdout and stderr to tqdm allows tqdm to control how print statements are shown and stops the progress bar formatting from breaking. Note: warnings.warn() messages still cause formatting issues in terminal.
- Yields:
sys.stdout – Original stdout.
- Return type:
Generator[DummyTqdmFile, None, None]
Examples
To use with tqdm, call like this: >>> from tqdm import tqdm >>> from time import sleep >>> >>> # Removing times from tqdm output for testing only >>> bar_format = “{l_bar}{bar}| {n_fmt}/{total_fmt}” >>> >>> # Redirect stdout to tqdm.write() (don’t forget the as save_stdout) >>> with ( … std_out_err_redirect_tqdm() as orig_stdout … ): # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE … # tqdm needs the original stdout … # and dynamic_ncols=True to autodetect console width … for i in tqdm(range(3), file=orig_stdout, bar_format=bar_format): … sleep(0.5)
0%|…| 0/3
33%|#…| 1/3 67%|#…| 2/3
100%|#…| 3/3 100%|#…#| 3/3