How to cache in IPython Notebook?

前端 未结 4 1118
借酒劲吻你
借酒劲吻你 2021-02-07 05:30

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

4条回答
  •  独厮守ぢ
    2021-02-07 06:15

    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 :)

提交回复
热议问题