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

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:

Related

Set Jupyter Lab working directory to the directory where Jupyter was started, not the parent directory of the notebook? [duplicate]

Question: How can one set the working directory of all notebooks opened in Jupyter Lab with a double-click, to be the project's folder, /myproject/, regardless of the notebook's subfolder within that parent folder? The working directory is identified by !pwd on Linux/Mac or !cd on Windows.
Context:
The Jupyter Lab sessions is initiated from the project's folder, by: /myproject/jupyter lab.
I am not looking for changing the working directory within the notebook with code (e.g. with !cd.. or using os), but change the settings of Jupyter Lab, such that the all kernels will start with the folder from where Jupyter Lab was initiated.
This is useful for being able to use consistent relative folders, regardless of the subfolder they are referred from. For example, for loading data in a specific subfolder.
I agree that this is often a preferred approach! I always have my notebooks configured to work like that because it:
makes it easy to specify paths to data and for outputs and
allows moving a notebook between directories without the need to change the paths
makes jupyterlab-lsp code intelligence work more reliably
I have a python module called make_paths_absolute.py with the following contents:
from os import chdir
from pathlib import Path
def make_paths_relative_to_root():
"""Always use the same, absolute (relative to root) paths
which makes moving the notebooks around easier.
"""
top_level = Path(__file__).parent
chdir(top_level)
make_paths_relative_to_root()
And in the first cell of every notebook, I add a line import make_paths_absolute. I like it this way because it makes it:
reproducible: anyone who copies/clones my project will be able to run the code without the need to customize anything in their Jupyter environment
work with all notebook interfaces (whether JupyterLab/RetroLab/classic Notebook)
is quite obvious for anyone reading the notebook that they should expect the paths to be absolute (=relative to the root).
To make that work you first need to set PYTHONPATH pointing to the root of the repository when starting JupyterLab. To do so on Linux/Mac you can prepend the jupyter lab command with:
PYTHONPATH=/path/to/your/lab/root:$PYTHONPATH jupyter lab
Alternatively, you can specify PYTHONPATH in kernel.json (either by modifying the default kernel specification or creating a copy of it first, see https://stackoverflow.com/a/53595397).
PS. The above is a simplification of my setup. In reality, I store make_paths_absolute.py in helpers package (so there is also an empty helpers/__init__.py and there is extra .parent) together with other initialization-time code, including a notebook called notebook_setup.ipynb which contains things like %matplotlib inline, pandas customizations (like making sure it uses stable sort), warning filters etc. So my every notebook starts with:
import helpers.make_paths_absolute
%run helpers/notebook_setup.ipynb
and I am really liking this setup working like that for two years now without any problems.
There is a feature request for making this easier at: https://github.com/jupyterlab/jupyterlab/issues/11619.
The most easiest solution in my opinion
Open Notepad
Paste the command "jupyter notebook --NotebookApp.iopub_data_rate_limit=1.0e10"
Save the notepad file with an extension of ".bat" instead of ".txt"
Paste the file in which directory you want to initialize your jupyter
Double click and open the ".bat" file
Jupyter opens with desired directory as base
This way you control the jupyter root directory as and when required and don't
really have to perform any manual settings
Hope this helps
".bat" file created on Desktop
enter image description here
".bat" file double clicked and executed
enter image description here
jupyter opens with Desktop as the intended base directory
enter image description here

How to restore a deleted Jupyter notebook file

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.

Atom portable location

When we run:
C:\> atom --portable
the relavent contents from ~/.atom are copied to a new home directory in the Portable Mode location. But where is the Portable Mode location?
Your environment variable ATOM_HOME is where the relevant contents will be copied. Also you need the .atom/ and Atom to be sibling folders. So if you're using dropbox for your portable atom you would need
dropbox/.atom
dropbox/Atom/atom.exe

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.

Resources