Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recovering command history in Jupyter Notebook?

I have been editing a Jupyter Notebook for the past week, and tried saving it today. While attempting to save it, I got an error, so I refreshed the page and successfully saved it.

However, to my dismay almost all of my command history was lost! I still have access to the variables (the kernel never died), but I don't have access to any of the code.

Is there any way to recover the code? The kernel is still running, but I do not see any checkpoints in my notebook.

like image 594
mgoldwasser Avatar asked Jan 22 '18 01:01

mgoldwasser


2 Answers

You can get the IPython history, which Jupyter Notebook uses as its kernel. In one of the cells run:

%history

This will dump a history (good, bad, and ugly) of every command you have run in the current IPython session. It is probably more than you want, but it is better than losing all your work.

like image 185
James Avatar answered Sep 25 '22 03:09

James


Something similar happened to me where I could not save what I did, refreshed the page, and lost all my commands.

You can get your entire notebook history and redirect the output to any given file by running

%history -g -f filename

In my case, I redirected it to a (.py) file. You can find more documentation here https://ipython.readthedocs.io/en/stable/interactive/magics.html under "%history".

like image 25
TonyV Avatar answered Sep 24 '22 03:09

TonyV