我正在服务器上运行的 IPython 笔记本中进行长时间运行的实验,其中典型的工作周期是:启动实验、去吃午饭、回来、检查进度、检查 Facebook、检查电子邮件、再次检查 Facebook、关闭计算机,回来,查看 Facebook,查看进度,...
问题是,当我关闭运行笔记本的浏览器窗口时,打印/日志输出不再保存在笔记本中。
例如,在我的简单实验中:
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!'
如果我运行,关闭选项卡,并在 10 秒后返回,我只会看到上次保存笔记本时的输出。我希望看到的是:
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!
据推测,这将在 future 的某个时候修复,但同时是否有一些简单的技巧可以让它在关闭浏览器后继续打印到笔记本输出?如果它适用于内嵌图像,则加分。
最佳答案
嗯,找到了一个好的解决方案。解决方案在此文件中:
https://github.com/QUVA-Lab/artemis/blob/master/artemis/fileman/persistent_print.py
使用示例:
https://github.com/QUVA-Lab/artemis/blob/master/artemis/fileman/test_persistent_print.py
演示现在看起来像:
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!'
如果我跑
reprint()
在下一个单元格中,它将重新显示自
capture_print
以来所做的所有打印语句。被称为。显然,如果这是不必要的会更好,但它现在有效。
关于ipython-notebook - IPython Notebook - 关闭浏览器后继续打印到笔记本输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29119657/