Selected Jupyter core packages...
IPython : 7.29.0
ipykernel : 6.4.1
ipywidgets : 7.6.5
jupyter_client : 6.1.12
jupyter_core : 4.8.1
jupyter_server : 1.4.1
jupyterlab : 3.2.1
nbclient : 0.5.3
nbconvert : 6.1.0
nbformat : 5.1.3
notebook : 6.4.5
qtconsole : 5.1.1
traitlets : 5.1.0
storemagic - Don’t lose your variables in Jupyter Notebook
Enviornment
About
This post is about a very important (and highly underrated) Jupyter notebook extension “storemagic”. We will see how we can use it to store our important data, and recover safely even when the unforgivable happens.
Introduction
All Jupyter Notebook lovers remember their petrified moments when they are struck by a [dark terminal] force, their heart skips a beat, and they stare blanky at the screen. When they know that the unforgivable has happened but they still timidly move the cursor to the last cell and press Enter with the faintest hope that it might be still alive. But fate has chosen something else for us, and we poor people are left cursing ourselves as to why we did not save our precious work!
I have been to those despair valleys (many times) till I stumbled upon ‘storemagic’. It seems like archaic magic as so few people know or use it, but I can guarantee once you get used to it you will barely write more than five lines in your notebooks without using it. So let’s see how it works.
Store a variable
Use %store
magic for lightweight persistence. It stores variables, aliases and macros in IPython’s database. Let’s create a variable and then store it using this magic.
Stored 'var_hello' (str)
Now I am going to intentionally restart the kernel. We can check that our created variable is now gone from the memory.
But no worries. We have it stored safely using our magic. So let’s get it back.
Okay, we have our variable back and (with a sigh of relief) we can use it again.
Let’s create a few more variables and do some more magic with them.
Check all stored varaibles
We can check all the varaibles stored using the following magic command.
Remove a variable from store
To remove a variable from our storage is also straight forward. Put its name after %store -d
flag
‘var_hello’ is now gone. Forever …
Remove all variables from store
If you want to remove all the varaibles from store and start clean then use -z
flag
Kaboom! all variables are gone.
Reference
Check the official IPython storemagic
documentation here: https://ipython.readthedocs.io/en/stable/config/extensions/storemagic.html