How to restore a deleted Jupyter notebook file - jupyter-notebook

I accidentally deleted a jupyter notebook file on my Google Cloud instance. I wonder if there's anyway to restore/recover the file?

Thanks to this link, I found the solution. Files deleted in the browser should probably be in a Trash folder. In my case and on my Google Cloud instance, the deleted files were in the following path.
cd ~/.local/share/Trash/files/
By using ls, list the files and see if your file is in this folder. If yes, then simply using the mv command you can move your deleted file to the path you want.

Related

I've downloaded a file via git clone to my notebook on google colab, how do I determine that file's path now?

To be clear this file was NOT imported from Google Drive, instead it was downloaded directly.
Use %pwd to show the current directory, %cd to switch directories, and !ls to list directories. (Or, use the file browser GUI on the left hand side.)
Here's an example:

how to reset jupyter notebook meta data, keep content. So it will be easier to manage with git

Is is possible to reset notebook run information, only keep content of a jupyter notebook?
Because every time I run a notebook, git will think this file is changed. I don't remember if I change the content of this notebook(some time I open a notebook for days), so I can't just checkout this notebook file for git history. If I just commit notebook to git server no matter if I make "real" change of it, it makes my git log very messy.
There some execution information is not keep in .ipynb_checkpints directory:
for example:
another real messy content is the output of cells.
You could add the following line to your .gitignore to simply make Git ignore the Jupyter Notebook checkpoint files:
.ipynb_checkpoints
If you don't already have a .gitignore file, this is a file that tells Git which files it can safely ignore. You can create it by simply making a file named ".gitignore", then adding the line above to the file.
If you're using Windows, this is a little harder than it should be, since the ".gitignore" file doesn't technically have a file name (only a file extension). Here's how you can do it anyway.
Also note that if you have already added any .ipynb_checkpoints files to your Git repo, you need to manually remove them before this will work. The .gitignore file does not work on files that are already tracked.

How to save Jupyter notebooks from GitHub

When I download an ipynb file using the RAW button in GitHub it displays the text (json) in the browser.
Should I just copy this text into a file and name it xxx.ipynb? What's the best way to do it?
First click on Raw
Then, press ctrl+s to save it as .ipynb (Note that you'll have to manually type '.ipynb' after the file name to make this work, as files from GitHub are saved as text files as default.)
Open jupyter notebook
Go to location where you saved .ipynb file
Open file, you will see the code
Hope this helps
Here is the Lifesaver Extension developed by me for both
Chrome
Firefox
The project is open-sourced here.
The extension not only opens github hosted notebooks in Colab but also in nbviewer!
And you can open the github repo from Colab and nbviewer
And go to nbviewer from Colab and github
Works all 3 ways!!
A new feature of opening new notebooks in one-click is already developed in the master branch, just need to push it to the extension platforms :)
Firefox extension
Chrome extension
The following steps worked for me:
Click on Raw in git repository.
Save the file. The file was saved as *.ipynb.txt format for me.
Then, in the jupyter directory tree (not in local directory), I selected, removed the .txt at the end and renamed the file as *.ipynb.
Finally I was able to run the file as jupyter notebook.
Note that, when I tried to rename the *.txt file in local directory to *.ipynb, it did not work. This had to be renamed in directory in jupyter itself.
True to 2020:
Click Download
Wait for JSON to finish loding in your browser
Ctrl S (save as .txt file)
remove .txt extension
Run locally
I saved the file following the instructions from this post. My destination however was a folder on google drive. I opened google drive on my browser and located the file. From there, I renamed the name of my file by just removing the txt extension, leaving the ipynb extension. That worked for me.

How to recover deleted iPython Notebooks

I have iPython Notebook through Anaconda. I accidentally deleted an important notebook, and I can't seem to find it in trash (I don't think iPy Notebooks go to the trash).
Does anyone know how I can recover the notebook? I am using Mac OS X.
Thanks!
This is bit of additional info on the answer by Thuener,
I did the following to recover my deleted .ipynb file.
The cache is in ~/.cache/chromium/Default/Cache/ (I use chromium)
used grep in binary search mode, grep -a 'import math' (replace search string by a keyword specific in your code)
Edit the binary file in vim (it doesn't open in gedit)
The python ipynb should file start with '{ "cells":' and
ends with '"nbformat": 4, "nbformat_minor": 2}'
remove everything outside these start and end points
Rename the file as .ipynb, open it in your jupyter-notebook, it works.
The "delete" functionality now sends the file to OS trash rather than permanently deleting it, see this PR: https://github.com/jupyter/notebook/pull/1968. So you can just open your Trash (wherever that is on your system) and restore it.
I think the easiest way (until developers handle this issue) to retrieve your Ipython history is to write them all into an empty file.
You need to check by the date you created your last script. Obviously, it is going to be the last part of your Ipython history.
To write your Ipython history into a file:
%history -g -f anyfilename
On linux:
I did the same error and I finally found the deleted file in the trash
/home/$USER/.local/share/Trash/files
If you deleted it through the OS (rm file.ipynb) then you can probably get it from ~/.ipython_checkpoints/ However, if you deleted it from the browser menu option, it is gone (by design!).
See discussion here: https://github.com/jupyter/notebook/issues/405
If you use PyCharm, you can do the following.
Open the Local History view.
Select the version you want to roll back to.
On the context menu of the selection, choose Revert.
Worked for me!
Source: here
For the unlucky ones like me, that delete some files on JuliaBox(jupyter for julia), there is a solution. I successifly recovery all my deleted files.
The browsers strore cache information about the pages you visit. You have to find your cache browser folder (in ubuntu with crhome was ~/.cache/google-chrome/Default/Cache) and grep for some text of your notebook in the binarys. Then, cut the text part of the file that is correspond to your ipynb.
https://groups.google.com/forum/#!searchin/julia-box/delete%7Csort:relevance/julia-box/Rt9LG9RldrU/3s_vVSrivJEJ
If you're using windows, it sends it to the recycle bin, thankfully. Clearly, it's a good idea to make checkpoints.
As long as your Kernel is active, the code of each executed cell is stored in input history list. This will come in handy when you accidentally deleted a cell and want to retrieve its content.
_ih[-10:] *# code of the 10 most recently run cells (Even if those cells are deleted now)*
If you are running on Jupyterlab on linux like me. What I did is went into command prompt and went to my trash folder.
Trash directories on linux are typically
/home/$USER/.local/share/Trash
or
If you deleted something as root (e.g. deleted a file using Nautilus invoked via gksu), it is at
/root/.local/share/Trash
I ended up changing directories to /home/$USER/.local/share/Trash/files and my deleted notebook was there! depending on how you access your backend you could also try /home/jupyter/.local/share/Trash/
ps
If you are having issues changing directories from Trash to files due to permissions dont forget to become root:
sudo -i
then after sudo -i, go up with:
cd ..
and then
cd home/jupyter/.local/share/Trash
cd files
Best of luck,
Sadly my file was neither in the checkpoints directory, nor chromium's cache. Fortunately, I had an ext4 formatted file system and was able to recover my file using extundelete:
Figure out the drive your missing deleted file was stored on:
df /your/deleted/file/diretory/
Switch to a folder located on another you have write access to:
cd /your/alternate/location/
It is proffered to run extundlete on an unmounted partition. Thus, if your deleted file wasn't stored on the same drive as your operating system, it's recommended you unmount the partition of the deleted file (though you may want to ensure extundlete is already installed before proceeding):
sudo umount /dev/sdax
where sdax is the partition returned by your df command earlier
Use extundelete to restore your file:
sudo extundelete --restore-file /your/deleted/file/diretory/delted.file /dev/sdax
If successful your recovered file will be located at:
/your/alternate/location/your/deleted/file/diretory/delted.file
I had the very problem and I ended up solving it this way. It might be the case for some of the folks.

Why a folder name is different in Windows Explorer than command line or cygwin?

I have been using Robocopy to backup my computer files. I have been using the following command:
robocopy C:\Users\ J:\backup\ *.* /a /XD AppData /XJD /R:0 /s
When I look into my J drive in Windows Explorer I see folder named J:\Users\ but when I use dir from the command line I see a folder named j:\backup. The backup and user folder seem to be the same. I also tried looking at the content of the J drive in cygwin and see the backup folder but do not see a Users folder. What is going on? Is robocopy the culprit?
Just go into the backed up folder and delete the desktop.ini file. The folder name will revert back to the directory name. You copied the desktop.ini file from the source "users" folder that is causing the issue. You can and desktop.ini to your exclusions list with out any issues to your back up. They get created on the fly anyway if they are not present.
I tested this on a windows 7 pro machine just to verify.
The problem is not related to robocopy. The link below describes the problem.
https://superuser.com/questions/381110/windows-explorer-sees-different-file-name-from-cmd/381159#381159

Resources