问题描述
假设我在Jupyter / Ipython笔记本中进行了更大规模的数据分析,并完成了大量耗时的计算。然后,出于某种原因,我必须关闭jupyter本地服务器I,但是我想稍后返回进行分析,而不必再次进行所有耗时的计算。
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 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.
技术上是否可行?是否有我忽略的内置功能?
编辑基于在答案中,有%商店
应该是轻量级泡菜。但是你必须像这样手动存储变量:
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 for refresh
print(foo)#一个虚拟字符串
#inside a ipython/nb session
foo = "A dummy string"
%store foo
closing seesion, restarting kernel%store -r foo
# r for refreshprint(foo) # "A dummy string"
这与我想要的非常接近,但是必须手动完成并且无法区分不同的会话会使它变得不那么有用。
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)笔记本会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!