%%cache cell magic
Defines a %%cache cell magic in the IPython notebook to cache results and outputs of long-lasting computations in a persistent pickle file. Useful when some computations in a notebook are long and you want to easily save the results in a file.
pip install ipycache
pip install git+https://github.com/rossant/ipycache.git(for the latest development version)
Then, create a cell with:
%%cache mycache.pkl var1 var2 var1 = 1 var2 = 2
When you execute this cell the first time, the code is executed, and the variables
var2are saved in
mycache.pklin the current directory along with the outputs. Rich display outputs are only saved if you use the development version of IPython. When you execute this cell again, the code is skipped, the variables are loaded from the file and injected into the namespace, and the outputs are restored in the notebook.
file_nameis a variable holding the path to the file used for caching.
-foption to force the cell's execution and overwrite the file.
-roption to prevent the cell's execution and always load the variables from the cache. An exception is raised if the file does not exist.
-doption to specify the cache directory. You can specify a default directory in the IPython configuration file in your profile (typically in
~\.ipython\profile_default\ipython_config.py) by adding the following line:
c.CacheMagics.cachedir = "/path/to/mycache"
If both a default cache directory and the
--cachediroption are given, the latter is used.