Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recover accidentally deleted cells in Jupyter Notebook

I'm working in a .ipynb file in VSCode and I tried to undo some lines by using ESC + Z, but for some reason either I undid too much or I pressed something else, but around 30 of my cells all got deleted. I don't know how to redo and I'm scared I lost all my work!

How can I recover these cells???

Thanks

edit: I found the code that I've run using %history but I was hoping to get my Markdown cells as well because I wrote a lot of information there

edit 2: I noticed that my cells weren't just deleted, but my files actually just changed to a different spot in history, as my first few cells contained something different than it did before the cells disappeared. Does that change the answer to this question at all?

I did post a workaround to recover some stuff, but I'd still appreciate if someone let me know what happened in my 2nd edit, thanks!

like image 379
chung Avatar asked May 05 '20 00:05

chung


People also ask

How do I recover a deleted cell from Jupyter notebook?

If you go to "Edit", there's an option for "Undo Delete Cells". If you are familiar with shortcuts, you can do cmd + shift + p and then type in undo to recover as well.

How do I recover a deleted cell in Jupyter notebook Vscode?

(this will open file explorer) Navigate to . vscode-server\data\User\History\. There will be multiple folders, pick one for the file you want to restore and Voila!

How do you undo changes in Jupyter notebook?

If you click inside the cell you edited, pressing Ctrl-Z should undo the changes.


5 Answers

I found a way to get the lost cells back, which saved me an enormous amount of time as I did not have to recreate those cells. If you open the timeline (bottom left corner of the screen), you can go back in time when you still had those cells, copy the content and paste it in a new cell. Update: you might have to upgrade to the latest version if Timeline is not available for you.

like image 192
Akos Avatar answered Sep 18 '22 14:09

Akos


If you hit the 'Z' key it will undo moves and deletes. Note, not CTRL+Z, but 'Z' by itself. link to the GitHub issue.

like image 30
Sadegh Salehi Avatar answered Oct 18 '22 01:10

Sadegh Salehi


This happens constantly for me. So incredibly annoying! It happens when I hit or mishit CMD+Z. No amount of ESC-Z will recreate the deleted cell. Nothing in any menu have so far recreated the lost. I have deleted all shortcuts for 'delete cell' but it makes no difference at all. Just about to give up on VSCode for this reason alone.

like image 9
UlrikP Avatar answered Oct 18 '22 00:10

UlrikP


I had a similar issue where I was hitting CTRL+Z outside of the cell when I thought I was inside the cell (usually because running current cell makes you lose cell focus). Sometime it just "undid" the last created cell somewhere else in my notebook, and I actually lost some cells because I didn't realized right away what happened and just continued working.

I thought I would share my solution to this problem (because Google sent me on this post, and more people might be interested).

Redefining the Keyboard Shortcuts worked for me. I removed the CTRL+Z shortcut when I'm not inside a cell (you can still access it with the menu Edit > Undo). I did the same for CTRL+SHIFT+Z, and so on.

To do this, I added theses rules to my keybindings.json:

{ "key": "ctrl+z", "command": "-undo" },
{ "key": "ctrl+z", "command": "undo", "when": "!notebookEditorFocused || inputFocus" },
{ "key": "ctrl+shift+z", "command": "-redo" },
{ "key": "ctrl+shift+z", "command": "redo", "when": "!notebookEditorFocused || inputFocus" },
{ "key": "ctrl+y", "command": "-redo" },
{ "key": "ctrl+y", "command": "redo", "when": "!notebookEditorFocused || inputFocus" },

I also removed the Z shortcut from Jupyter extension because I don't like it.

{ "key": "z", "command": "-undo", "when": "notebookEditorFocused && !inputFocus" },
like image 9
User9123 Avatar answered Oct 18 '22 00:10

User9123


Ok, this is crazy but since I'm in Visual Studio Code, I tried right clicking on my .ipynb file and clicking "Convert to Python Script".

This recovered every single line including Markdown Code that I had before I did something to delete my cells.

Might be worth a try if you deleted your Jupyter Cells in Visual Studio Code

like image 5
chung Avatar answered Oct 17 '22 23:10

chung