title: Redirect print command in python script through tqdm.write()
body: <p>I'm using <code>tqdm</code> in Python to display console-progressbars in our scripts.
  However, I have to call functions which <code>print</code> messages to the console as well and which I can't change.
  In general, writing to the console while displaying progress bars in the console messes up the display like so:</p>
  
  <pre><code>from time import sleep
  from tqdm import tqdm
  
  def blabla():
    print "Foo blabla"
  
  for k in tqdm(range(3)):
    blabla()
    sleep(.5)
  </code></pre>
  
  <p>This creates the output:</p>
  
  <pre><code>0%|                                           | 0/3 [00:00&lt;?, ?it/s]Foo
  blabla
  33%|###########6                       | 1/3 [00:00&lt;00:01,  2.00it/s]Foo
  blabla
  67%|#######################3           | 2/3 [00:01&lt;00:00,  2.00it/s]Foo
  blabla
  100%|###################################| 3/3 [00:01&lt;00:00,  2.00it/s]
  </code></pre>
  
  <p><a href="https://github.com/tqdm/tqdm#writing-messages" rel="noreferrer">According to the documentation of <code>tqdm</code></a> the method <code>tqdm.write()</code> provides a means to write messages to the console without breaking the displayed progressbars.
  Thus, the right output is provided by this snippet:</p>
  
  <pre><code>from time import sleep
  from tqdm import tqdm
  
  def blabla():
    tqdm.write("Foo blabla")
  
  for k in tqdm(range(3)):
    blabla()
    sleep(.5)
  </code></pre>
  
  <p>And looks like this:</p>
  
  <pre><code>Foo blabla
  Foo blabla
  Foo blabla
  100%|###################################| 3/3 [00:01&lt;00:00,  1.99it/s]
  </code></pre>
  
  <p>On the other hand, there is this <a href="https://stackoverflow.com/a/2829036/3434933">solution which permits to silence those functions</a> by quite elegantly redirecting <code>sys.stdout</code> into the void.
  This works perfectly well for silencing the functions.</p>
  
  <p>Since I want to display the messages from these functions nonetheless without breaking the progress bars, I tried to merge both solutions into one by redirecting <code>sys.stdout</code> to <code>tqdm.write()</code> and, in turn, letting <code>tqdm.write()</code> write to the <strong>old</strong> <code>sys.stdout</code>.
  This results in the snippet:</p>
  
  <pre><code>from time import sleep
  
  import contextlib
  import sys
  
  from tqdm import tqdm
  
  class DummyFile(object):
    file = None
    def __init__(self, file):
      self.file = file
  
    def write(self, x):
      tqdm.write(x, file=self.file)
  
  @contextlib.contextmanager
  def nostdout():
      save_stdout = sys.stdout
      sys.stdout = DummyFile(save_stdout)
      yield
      sys.stdout = save_stdout
  
  def blabla():
    print "Foo blabla"
  
  for k in tqdm(range(3)):
    with nostdout():
      blabla()
      sleep(.5)
  </code></pre>
  
  <p>However, this actually creates an even more messed up output as before:</p>
  
  <pre><code>0%|                                           | 0/3 [00:00&lt;?, ?it/s]Foo
  blabla
  
  
  33%|###########6                       | 1/3 [00:00&lt;00:01,  2.00it/s]Foo
  blabla
  
  
  67%|#######################3           | 2/3 [00:01&lt;00:00,  2.00it/s]Foo
  blabla
  
  
  100%|###################################| 3/3 [00:01&lt;00:00,  2.00it/s]
  </code></pre>
  
  <p>FYI: calling <code>tqdm.write(..., end="")</code> inside <code>DummyFile.write()</code> creates the same result as the first output which is still messed up.</p>
  
  <p>I can't understand why this wouldn't work, since <code>tqdm.write()</code> is supposed to manage clearing the progress bar before writing the message and then rewriting the progress bar.</p>
  
  <p>What am I missing?</p>