Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cache in IPython Notebook?

Environment:

  • Python 3
  • IPython 3.2

Every time I shut down a IPython notebook and re-open it, I have to re-run all the cells. But some cells involve intensive computation.

By contrast, knitr in R save the results in a cache directory by default so only new code and new settings would invoke computation.

I looked at ipycache but it seems to cache a cell instead of the notebook. Is there a counterpart of cache of knitr in IPython?

like image 301
Zelong Avatar asked Jul 06 '15 21:07

Zelong


People also ask

Does Jupyter Notebook cache?

Jupyter Book can automatically run and cache any notebook pages. Notebooks can either be run each time the documentation is built, or cached locally so that notebooks will only be re-run when the code cells in a notebook have changed.

How do I clean my Jupyter Notebook?

Press 'control-shift-p', that opens the command palette. Then type 'clear cell output'. That will let you select the command to clear the output.

How do I clean my Jupyter Notebook memory?

Jupyter Lesson 10: How to reset the Kernel (Clear all in memory objects and stop the code) If the notebook or your code is acting weird sometimes it is best to press the “RESET BUTTON”. Reseting the kernel clears all in memory objects and restarts your code from the very top.

Can Jupyter Notebook autofill?

Enable autocomplete featureTo enable code autocomplete in Jupyter Notebook or JupyterLab, you just need to hit the Tab key while writing code. Jupyter will suggest a few completion options. Navigate to the one you want with the arrow keys, and hit Enter to choose the suggestion.


3 Answers

Unfortunately, it doesn't seem like there is something as convenient as an automatic cache. The %store magic option is close, but requires you to do the caching and reloading manually and explicitly.

In your Jupyter notebook:

a = 1
%store a

Now, let's say you close the notebook and the kernel gets restarted. You no longer have access to the local variables. However, you can reload the variables you've stored using the -r option.

%store -r a
print a # Should print 1
like image 96
viswajithiii Avatar answered Oct 05 '22 16:10

viswajithiii


In fact the functionality you ask is already there, no need to re-implement it manually by doing your dumps .

You can use the use the %store or maybe better the %%cache magic (extension) to store the results of these intermittently cells, so they don't have to be recomputed (see https://github.com/rossant/ipycache)

It is as simple as:

%load_ext ipycache

Then, in a cell e.g.:

%%cache mycache.pkl var1 var2
var1 = 1
var2 = 2

When you execute this cell the first time, the code is executed, and the variables var1 and var2 are saved in mycache.pkl in 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.

It saves all graphics, output produced, and all the variables specified automatically for you :)

like image 40
ntg Avatar answered Oct 05 '22 16:10

ntg


Use the cache magic.

%cache myVar = someSlowCalculation(some, "parameters")

This will calculate someSlowCalculation(some, "parameters") once. And in subsequent calls it restores myVar from storage.

https://pypi.org/project/ipython-cache/

Under the hood it does pretty much the same as the accepted answer.

like image 25
wotanii Avatar answered Oct 05 '22 17:10

wotanii