title: stdout redirect from Jupyter notebook is landing in the terminal
body: <p>So I was trying to redirect <code>stdout</code> for a some of the cells in my Jupyter Notebook to a file with <a href="https://stackoverflow.com/questions/4675728/redirect-stdout-to-a-file-in-python#4675744">this</a> and then cancel it with <a href="https://stackoverflow.com/questions/14245227/python-reset-stdout-to-normal-after-previously-redirecting-it-to-a-file">this</a> for the rest of the cells. The output from the first set of cells landed in the file like it was meant to. The second set of cells after the cancel command <code>sys.stdout = sys.__stdout__</code> was giving no output, appearing to do nothing, but I later realised it was landing in the terminal where the notebook was launched. </p>
  
  <p>It works perfectly in the Python interpreter with exactly the same Python:</p>
  
  <pre><code>(miniconda3-latest) cardamom@pegasus:~/Documents/project1 $ python
  Python 3.6.0 |Continuum Analytics, Inc.| (default, Dec 23 2016, 12:22:00) 
  [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
  Type "help", "copyright", "credits" or "license" for more information.
  &gt;&gt;&gt; import sys
  &gt;&gt;&gt; sys.stdout = open('stdout.txt', 'w')
  &gt;&gt;&gt; print("a")
  &gt;&gt;&gt; print("a")
  &gt;&gt;&gt; print("a")
  &gt;&gt;&gt; sys.stdout = sys.__stdout__
  &gt;&gt;&gt; print ("b")
  b
  &gt;&gt;&gt; print ("b")
  b
  &gt;&gt;&gt; print ("b")
  b
  &gt;&gt;&gt; # lots of a's are in the file and no b's
  </code></pre>
  
  <p>A similar approach in the Jupyter notebook is giving this in the terminal:</p>
  
  <pre><code>[W 22:05:34.192 NotebookApp] 404 GET /nbextensions/widgets/notebook/js/extension.js (127.0.0.1) 1.71ms referer=http://localhost:8889/notebooks/test_stdout.ipynb
  b
  b
  b
  </code></pre>
  
  <p>How can this code be adapted so that after resetting it, I get the b's appearing under the cells where the output usually goes instead of in the terminal?</p>