如何腌制或存储 Jupyter (IPython) 笔记本会话以备后用 [英] How to pickle or store Jupyter (IPython) notebook session for later

查看:19
本文介绍了如何腌制或存储 Jupyter (IPython) 笔记本会话以备后用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我正在 Jupyter/Ipython notebook 中进行更大的数据分析,并完成了大量耗时的计算.然后,出于某种原因,我不得不关闭我的 jupyter 本地服务器,但我想稍后返回进行分析,而不必再次进行所有耗时的计算.

Let's say I am doing a larger data analysis in Jupyter/Ipython notebook with lots of time consuming computations done. Then, for some reason, I have to shut down the jupyter local server I, but I would like to return to doing the analysis later, without having to go through all the time-consuming computations again.

喜欢喜欢做的是pickle或存储整个Jupyter会话(所有pandas数据帧、np.arrays、变量等),这样我就可以安全地关闭服务器,知道我可以以与以前完全相同的状态返回到我的会话.

What I would like love to do is pickle or store the whole Jupyter session (all pandas dataframes, np.arrays, variables, ...) so I can safely shut down the server knowing I can return to my session in exactly the same state as before.

技术上是否可行?是否有我忽略的内置功能?

基于这个的答案,有一个%store magic 应该是轻量级泡菜".但是,您必须像这样手动存储变量:

based on this answer there is a %store magic which should be "lightweight pickle". However you have to store the variables manually like so:

#inside a ipython/nb session
foo = "一个虚拟字符串"
%store foo
关闭查看,重新启动内核
%store -r foo # r 用于刷新
print(foo) # "一个虚拟字符串"

这与我想要的非常接近,但是必须手动完成并且无法区分不同的会话使其不太有用.

which is fairly close to what I would want, but having to do it manually and being unable to distinguish between different sessions makes it less useful.

推荐答案

我认为 Dill 很好地回答了您的问题.

I think Dill answers your question well.

pip install dill

保存笔记本会话:

import dill
dill.dump_session('notebook_env.db')

恢复笔记本会话:

import dill
dill.load_session('notebook_env.db')

来源

这篇关于如何腌制或存储 Jupyter (IPython) 笔记本会话以备后用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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