How To Restore Your Jupyter Notebook Session | by Roman Orac | Towards Data Science

Has it ever happened to you that you’ve rebooted your computer and forgot about that important Jupyter Notebook you’ve intended to use later? “It took so long to finish and all is lost now”.
I feel you! It happened to me many times.
Well, the only advice I can give you is to “Restart the Kernel and rerun all the cells”. But it doesn’t have to be like that next time.
There is a tool that enables you to save the state of a Jupyter Notebook Session and restore it later in a single command.
Here are a few links that might interest you:
- Labeling and Data Engineering for Conversational AI and Analytics- Data Science for Business Leaders [Course]- Intro to Machine Learning with PyTorch [Course]- Become a Growth Product Manager [Course]- Deep Learning (Adaptive Computation and ML series) [Ebook]- Free skill tests for Data Scientists & Machine Learning Engineers
Some of the links above are affiliate links and if you go through them to make a purchase I’ll earn a commission. Keep in mind that I link courses because of their quality and not because of the commission I receive from your purchases.
In case you’ve missed my other posts about Jupyter Notebooks:
towardsdatascience.com
towardsdatascience.com
towardsdatascience.com
towardsdatascience.com
towardsdatascience.com
Dill enables you to save the state of a Jupyter Notebook session and restore it with a single command.
To install Dill is as simple as installing any other Python package:
pip install dill
To a save a Jupyter Notebook Session simply execute the command below:
import dilldill.dump_session('notebook_env.db')
To restore a Jupyter Notebook session:
import dilldill.load_session('notebook_env.db')
Dill also works with python interpreter sessions.
Dill has a few shortcomings that you should be aware of:
This approach doesn’t work with generators.
The saved session doesn’t include graphs that were generated. So you need to rerun the code.
Some users report that the restoring session doesn’t work on another machine.
Dill extends python's pickle module for serializing and de-serializing Python objects to the majority of the built-in python types. Serialization is the process of converting an object to a byte stream, and the inverse of which is converting a byte stream back to a python object hierarchy.
Dill provides the user with the same interface as the pickle module, and also includes some additional features. In addition to pickling python objects, Dill provides the ability to save the state of an interpreter session in a single command. Hence, it would be feasible to save an interpreter session, close the interpreter, ship the pickled file to another computer, open a new interpreter, unpickle the session and thus continue from the 'saved' state of the original interpreter session.
Dill can be used to store python objects to a file, but the primary usage is to send python objects across the network as a byte stream. Dill is quite flexible and allows arbitrary user-defined classes and functions to be serialized. Thus Dill is not intended to be secure against erroneously or maliciously constructed data. It is left to the user to decide whether the data they unpickle is from a trustworthy source.
Dill can pickle the following standard types[1]:
none, type, bool, int, long, float, complex, str, unicode,
tuple, list, dict, file, buffer, builtin,
both old and new style classes,
instances of old and new style classes,
set, frozenset, array, functions, exceptions
Dill can also pickle more 'exotic' standard types[1]:
functions with yields, nested functions, lambdas
cell, method, unboundmethod, module, code, methodwrapper,
dictproxy, methoddescriptor, getsetdescriptor, memberdescriptor,
wrapperdescriptor, xrange, slice,
notimplemented, ellipsis, quit
Dill cannot yet pickle these standard types[1]:
frame, generator, traceback
Dill also provides the capability to[1]:
save and load python interpreter sessions
save and extract the source code from functions and classes
interactively diagnose pickling errors
[1] https://github.com/uqfoundation/dill
Dill is most certainly a tool that is helpful to have in your toolbox. I use it when I need to reboot my computer in another OS or when I expect that the Jupyter Kernel might crash with the next command.
IMO Dill is not a tool that you should automatically use in every Jupyter Notebook. Use it wisely, only when you need it. Also, be careful if any of the underlying code uses generators as cannot pickle it yet.
Follow me on Twitter, where I regularly tweet about Data Science and Machine Learning.