11

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.

Peter
  • 12,274
  • 9
  • 71
  • 86

1 Answers1

4

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.

Peter
  • 12,274
  • 9
  • 71
  • 86
  • Code no longer seems to exist. – Poik Apr 25 '16 at 16:36
  • Hi, I'm very interested in your answer but I don't understand what I need to install to make it work. The file `persistent_print.py` by itself is clearly not enough, because the two imported functions `capture_print` and `reprint` both depend on `CaptureStdOut` which is not in the file. So, do I need to install the _whole_ `artemis` framework just to make this single module work? – DeltaIV Nov 05 '18 at 12:38
  • Hi, I never bothered to make this a stand-alone piece of code. However, it should be easy to just remove other references to artemis (the module containing `CaptureStdOut` is stand-alone and you could edit the code to get rid of `get_artemis_data_path`). Alternatively, you can just install artemis with `pip install artemis-ml` – Peter Nov 05 '18 at 16:40