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.
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.
来源:https://stackoverflow.com/questions/29119657/ipython-notebook-keep-printing-to-notebook-output-after-closing-browser