17

While working in an ipython notebook, eventually I had to Ctrl+C as the kernel seemed to be halted.

The console gave me a message like:

[NotebookApp] Kernel  shutdown: 5faa86bf-........f6 
[NotebookApp] Kernel  shutdown: 71........22 
[NotebookApp] ....

(I had three notebooks running)

But something went wrong and my notebook file.ipynb is empty (actually only the one I was actively using).

Is there a way to recover that file before it was deleted? Some place where automatically-saved o manually-saved versions are stored?

(Running python 2.7 (Anaconda) in Windows 7)

alberto
  • 2,625
  • 4
  • 29
  • 48

3 Answers3

50

If none of the above helped, I found a workaround to recover most of the changes I did since the last checkout - by simply calling this command in your notebook:

%history -g

If you want your IPython history in a plain-text file, you can export it yourself.

You can also do it for a specific filename:

%history -g -f filename 

What does -g do? – Without -g it exports the history for the current session. With -g it exports history for all sessions.

Serendipity
  • 2,216
  • 23
  • 33
25

You can check in .ipynb_checkpoints/ in the folder where your notebook was for recent enough version of IPython.

Matt
  • 27,170
  • 6
  • 80
  • 74
  • 2
    Don't forget to version control you data and to regularly commit ! :-) – Matt Dec 04 '13 at 12:11
  • I do, but the last commit had been 5 hours before :-) – alberto Dec 05 '13 at 08:01
  • Where on my computer might I find the `.ipynb_checkpoints/`. I just deleted some important cells. @Matt – tmthyjames Oct 09 '14 at 19:17
  • 1
    In the same folder as the notebook you were working on. if workign on /foo/bar/qux.ipynb the save are on /foo/bar/.ipynb_checkpoints/qux-checkpoint.ipynb. You can also go to "file">"revert to checkpoint". Good time to also put your notebook on git or alike. – Matt Oct 10 '14 at 06:02
  • @Matt thanks, this was a life saver. I had actually committed, but the issue was that I reverted to a previous commit (hard reset), and by mistake overwrote my notebook. Using the checkpoints helped a lot – Sos May 10 '19 at 10:48
0

There is a great writeup about different recovery options from "Jupyter Disasters" at [1].

I want to quote one technique from there, namely opening $HOME/.ipython/profile_default/history.sqlite in the sqlite tool of your choice (e.g. sqlitebrowser) and digging around in there. This can be an option if there is no usable checkpoint file (as discussed in other answers).

[1] https://medium.com/flatiron-engineering/recovering-from-a-jupyter-disaster-27401677aeeb

mnagel
  • 6,729
  • 4
  • 31
  • 66