从内部以编程方式保存 IPython 笔记本? [英] Save an IPython notebook programmatically from within itself?

查看:33
本文介绍了从内部以编程方式保存 IPython 笔记本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让 IPython notebook 运行以进行一些计算 + 显示一些视觉效果.

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.

注意:在 Jupyter 中,CTRL-s 会触发一个异步过程,文件保存实际上仅在几秒钟后完成.如果您想在笔记本中进行阻塞保存操作,请使用这个小函数(file_path 是笔记本文件的路径):

Note: in Jupyter, 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆