从自身内部以编程方式保存IPython笔记本吗? [英] Save an IPython notebook programmatically from within itself?
问题描述
我想让IPython笔记本运行以进行一些计算并显示一些视觉效果.
I would like to leave an IPython notebook running to do some computation + show some visuals.
一旦IPython笔记本完成,我希望IPython笔记本中的最后一个单元以编程方式保存IPython笔记本.然后,我想将笔记本(包含所有输出)复制到另一个目录中,以记录结果.
Once the IPython notebook has finished, I want the last cell in the IPython notebook to programmatically save the IPython notebook. Then I want to copy the notebook (with all output) to another directory to keep a record of results.
我可以轻松地编写复制位,但是我不确定如何获得IPython笔记本以编程方式保存自身?这可能吗?预先感谢!
The copying bit I can code up easily, but I am not sure how to get an IPython notebook to programatically save itself? Is this possible? Thanks in advance!
推荐答案
我接受@Taar的评论,并使其成为实际的答案,因为它适用于提出问题的原始人和我自己.
I am taking @Taar's comment and making it an actual answer since it worked for the original person who asked the question and for myself.
from IPython.display import display, Javascript
display(Javascript('IPython.notebook.save_checkpoint();'))
这将创建检查点-与 CTRL-s
相同.
This will create checkpoints - same thing as CTRL-s
.
注意:在Jupiter中,CTRL-s触发一个异步过程,并且文件保存实际上仅在几秒钟后完成.如果要在笔记本中执行阻止保存操作,请使用此小功能( file_path
是笔记本文件的路径):
Note: in Jupiter, CTRL-s triggers an async process and the file save is actually completed only a few seconds later. If you want a blocking save operation in a notebook, use this little function (file_path
is the path to the notebook file):
import time
from IPython.display import display, Javascript
import hashlib
def save_notebook(file_path):
start_md5 = hashlib.md5(open(file_path,'rb').read()).hexdigest()
display(Javascript('IPython.notebook.save_checkpoint();'))
current_md5 = start_md5
while start_md5 == current_md5:
time.sleep(1)
current_md5 = hashlib.md5(open(file_path,'rb').read()).hexdigest()
这篇关于从自身内部以编程方式保存IPython笔记本吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!