Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IPython Notebook - Keep printing to notebook output after closing browser

I'm doing long-running experiments in an IPython notebook running on a server, where the typical work cycle is: launch experiment, go to lunch, come back, check progress, check Facebook, check email, check Facebook again, turn off computer, come back, check Facebook, check progress, ...

The problem is that when I close the browser window where the notebook is running, the print/logging outputs are no longer saved in the notebook.

For instance, in my simple experiment:

import time
start_time = time.time()
for i in xrange(5):
    print '%s seconds have passed' % (time.time()-start_time)
    time.sleep(2)
print 'Done!'

If I run, close the tab, and come back 10 seconds later, I just see whatever the output was when the notebook was last saved. What I expect to see is:

0.000111818313599 seconds have passed
2.00515794754 seconds have passed
4.01105999947 seconds have passed
6.0162498951 seconds have passed
8.01735782623 seconds have passed
Done!

Presumably this will be fixed at some point in the future, but in the mean time is there some easy hack to make it continue printing to notebook output after closing the browser? Bonus points if it works for inline images.

like image 536
Peter Avatar asked Mar 18 '15 10:03

Peter


People also ask

How do I run Jupyter notebook after closing browser?

This can be done by typing jupyter notebook in the terminal, which will open a browser. Then, navigate to the respective jupyter notebook file in the browser and open it. Click Cell > Run All on the toolbar. All done!

How do you stop a Jupyter notebook output?

If you want to stop the messages from print function with Jupyter Notebook you can use: %%capture.

Does Jupyter notebook run in sleep mode?

Linux does indeed log out, but jupyter notebook calculations run further and Linux does not go to sleep unless they are done. If you wake the computer in the morning, you will find the finished calculations with all variables still in memory.

Can I run Jupyter notebook without browser?

Step 1: Run Jupyter Notebook from remote machine Log-in to your remote machine the usual way you do. In most cases, this is simply done via an ssh command. Once the console shows, type the following: remoteuser@remotehost: jupyter notebook --no-browser --port=XXXX # Note: Change XXXX to the port of your choice.


1 Answers

Well, found an ok solution. Solution is in this file: https://github.com/QUVA-Lab/artemis/blob/master/artemis/fileman/persistent_print.py

With example use: https://github.com/QUVA-Lab/artemis/blob/master/artemis/fileman/test_persistent_print.py

The demo now looks like:

import time
from general.persistent_print import capture_print, reprint
capture_print()
start_time = time.time()
for i in xrange(5):
    print '%s seconds have passed' % (time.time()-start_time)
    time.sleep(2)
print 'Done!'

And if I run

reprint()

In the next cell, it will redisplay all the print statements made since capture_print was called. Obviously it would be better if this were unnecessary, but it works for now.

like image 117
Peter Avatar answered Oct 06 '22 23:10

Peter