Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make ipython notebook print in real time

Tags:

Ipython Notebook doesn't seem to print results in real time, but seems to buffer in a certain way and then bulk output the prints. How can I make ipython print my results as soon as the print command is processed?

Example code:

import time   def printer():     for i in range(100):         time.sleep(5)         print i 

Supposing that the above code is in a file that is imported. How could I make it that when I call the printer function it prints a number every 5 seconds and not all the numbers at the very end?

Please note that I cannot edit the function printer() because I get it from some external module. I want the to change the configs of ipython notebook somehow so that it doesn't use a buffer. Therefore, I also do not wish to use sys.stdout.flush(), I want to do it in real-time according to the question, I don't want any buffer to start with.

I also tried loading ipython notebook with the command:

ipython notebook --cache-size=0 

but that also doesn't seem to work.

like image 556
patapouf_ai Avatar asked Apr 21 '15 12:04

patapouf_ai


People also ask

How do I print from IPython notebook?

In JupyterLab select Help -> Launch classic notebook, then from your browser menu (e.g. 3 dot menu) choose print and print to pdf.

What is the difference between Jupyter Notebook and IPython notebook?

The IPython Notebook is now known as the Jupyter Notebook. It is an interactive computational environment, in which you can combine code execution, rich text, mathematics, plots and rich media. For more details on the Jupyter Notebook, please see the Jupyter website.

How do I save an IPython notebook as a PDF?

The Jupyter Notebook has an option to export the notebook to many formats. It can be accessed by clicking File -> Download as -> PDF via LaTeX (or PDF via HTML - not visible in the screenshot).

Can Jupyter notebooks be interactive?

Jupyter Notebook has support for many kinds of interactive outputs, including the ipywidgets ecosystem as well as many interactive visualization libraries.


1 Answers

This is merely one of the answers to the question suggested by Carsten incorporating the __getattr__ delegation suggested by diedthreetimes in a comment:

import sys oldsysstdout = sys.stdout class flushfile():     def __init__(self, f):         self.f = f     def __getattr__(self,name):          return object.__getattribute__(self.f, name)     def write(self, x):         self.f.write(x)         self.f.flush()     def flush(self):         self.f.flush() sys.stdout = flushfile(sys.stdout) 

In the original answer, the __getattr__ method is not implemented. Without that, it fails. Other variants in answers to that question also fail in a notebook.

In a notebook, sys.stdout is an instance of IPython.kernel.zmq.iostream.OutStream and has a number of methods and attributes not present in the usual sys.stdout. Delegating __getattr__ allows a flushfile to masquerade as a ...zmq.iostream.OutStream duck.

This works in a python 2.7 notebook run with ipython 3.1.0

like image 60
drevicko Avatar answered Oct 08 '22 18:10

drevicko