Jupyter Notebook stands as an important tool for its interactive and dynamic environment in the arena of data science and artificial intelligence. These Notebooks are a versatile tool that can be used for a variety of purposes and are a great tool for documenting and sharing your work, learning new skills, and training machine/deep learning models that can be resource intensive. They are a valuable asset for data scientists, students, and anyone who wants to learn new programming languages and concepts, especially Python.
Some Benefits of Jupyter Notebook
- Interactive: Notebooks can run code and see the results immediately which makes it easy to debug code.
- Shareable: Notebooks can be saved as a single file, which includes the code, data, and output. This makes it easy to reproduce your results and share your work with others.
- Extensible: Jupyter Notebooks can be extended with a variety of plugins and libraries. This makes them a versatile tool for a variety of tasks.
- Documented: Jupyter Notebooks can be annotated with text and images. This makes them a great way to document your work and explain your thought process.
However, the occasional mishap of deleting essential cells does sometimes happen. In this article, we will explore all possible methods to recover those accidentally deleted cells in Jupyter Notebook or if you might have knowingly deleted the cells but now want them back.
How to Recover a Deleted Notebook
If you accidentally deleted a notebook in the browser or on your local machine, it can still be recovered.
For Windows
Open the recycle bin it’s still there even if you deleted it from the browser session.
For Mac / Linux
In the terminal type find ~/ | grep notebook_file.ipynb
If the notebook still exists somewhere it will be restored
How to Delete a cell
- On the right end of the notebook, you have a delete icon.
- Click the icon to delete the cell.
- Keyboard Shortcut: press D to delete a selected cell
How to undo the delete operation
WARNING : Considering you haven’t saved the notebook after deleting the cell
Let’s say you delete the cell 2 (to delete a cell press ‘d’ or click the delete icon which is the rightmost icon in a cell row ) now the notebook looks like this :
Steps to undo the delete operation
- In the command palette
- Click on View
- This opens up the view menu
- Click on the undo button
- Or click on the button undo cell operation
Keyboard Shortcuts
- To undo : CTRL + Z
- To undo cell operation : press Z
How to Recover Deleted Cells
To recover deleted cells from the notebook follow these steps to being them back. But before proceeding make a copy of the current state of your notebook.
- In the command palette
- Click on file button
- Click on revert notebook to checkpoint
- Or Click revert notebook from disk ( if you have saved the notebook in your local machine )
This will change the state of the notebook when it was last saved and all deleted cells have been recovered just copy the deleted cells to the copy of this notebook you made in the beginning of this procedure.
Another method to recover deleted cells
This only works when after deleting cell you haven’t closed the notebook(ended the session)
- Create a new cell
- Type %history
- Run the cell
- All command executed with their outputs in the current session will be printed (even the deleted ones)
Using GIT
If you’ve been using version control with Git and have committed your changes regularly, you can check the commit history and revert to a previous version that still contains the deleted cell. But do save the current work somewhere else you have to redo the same thing again.
Follow these steps:
- Create a copy of the jupyter notebook
- open git bash in the root folder of the notebook
- Type git log –oneline
- All previously commit id’s will be printed
- Copy the id of the last commit ( or whichever commit you would like to proceed with )
- Type git reset <commitid> –hard
- View your file all changes have been reverted
Prevention
Here are some guidelines to look forward to :
- Use version control system to track changes and prevent accidental deletion of files/folders.
- Always create multiple checkpoints in your notebook session to recover deleted cells without much hustle.
- Use a file recovery software to recover permanently delete files