Storing variables in Jupyter Notebooks using %store magic

We’ve all been there. You’ve just run an expensive computation in your Jupyter Notebook and are about to draw those conclusions which will prove that your theories were right all along (until you find the sixteen bugs in your code which render them invalid, but that’s an issue for a different time). Then at the critical moment, your flatmate begins streaming their Lord Of The Rings marathon in 4k and your already temperamental Wi-Fi severs your connection to the department servers in protest, crashing your Jupyter Notebook, leaving your hopes and dreams in tatters.

It doesn’t have to be this way. Perhaps the sensible thing to do is to save the results of your lengthy computation so you can simply load them up again. The %store magic in Jupyter Notebook allows you to do this in a way that couldn’t be easier. Here’s how:

‘Essential’ information that we can’t afford to lose…

The ‘%store’ command saves the specified variable. Now if we restart the Jupyter Notebook we can recover the variable using the ‘%store -r’ command:

Disaster avoided

Simple!

Author