diff --git a/README.rst b/README.rst index ab157595..527411b6 100644 --- a/README.rst +++ b/README.rst @@ -657,10 +657,8 @@ A reusable canonical example is given below: .. code:: python from time import sleep - import contextlib import sys - from tqdm import tqdm class DummyTqdmFile(object): @@ -674,6 +672,9 @@ A reusable canonical example is given below: if len(x.rstrip()) > 0: tqdm.write(x, file=self.file) + def flush(self): + return getattr(self.file, "flush", lambda: None)() + @contextlib.contextmanager def stdout_redirect_to_tqdm(): save_stdout = sys.stdout @@ -692,11 +693,11 @@ A reusable canonical example is given below: # Redirect stdout to tqdm.write() (don't forget the `as save_stdout`) with stdout_redirect_to_tqdm() as save_stdout: - # tqdm call need to specify sys.stdout, not sys.stderr (default) + # tqdm needs the original sys.stdout # and dynamic_ncols=True to autodetect console width for _ in tqdm(range(3), file=save_stdout, dynamic_ncols=True): - blabla() sleep(.5) + blabla() # After the `with`, printing is restored print('Done!') diff --git a/examples/redirect_print.py b/examples/redirect_print.py index 7ef4aae6..5addc865 100644 --- a/examples/redirect_print.py +++ b/examples/redirect_print.py @@ -29,6 +29,9 @@ class DummyTqdmFile(object): if len(x.rstrip()) > 0: tqdm.write(x, file=self.file) + def flush(self): + return getattr(self.file, "flush", lambda: None)() + @contextlib.contextmanager def stdout_redirect_to_tqdm(): @@ -50,11 +53,11 @@ def blabla(): # Redirect stdout to tqdm.write() (don't forget the `as save_stdout`) with stdout_redirect_to_tqdm() as save_stdout: - # tqdm call need to specify sys.stdout, not sys.stderr (default) + # tqdm needs the original sys.stdout # and dynamic_ncols=True to autodetect console width for _ in tqdm(range(3), file=save_stdout, dynamic_ncols=True): - blabla() sleep(.5) + blabla() # After the `with`, printing is restored print('Done!')