Google Colab

Anh-Thi Dinh

URLs

Colab & Github

👉 Check this section too.

Hotkeys / Shortcuts

Check the command shortcuts in Tools > Keyboard shortcuts (Ctrl + M H), below are the most popular ones (If you use MacOS, replace Ctrl with cmd):
We can use system commands in Colab with !<command>. For example, !git clone ...

Import libraries

1!pip install -q matplotlib-venn
2# or
3!apt-get -qq install -y libfluidsynth1

Install permanently

Upgrade/Switch TensorFlow versions

1# To determine which version you're using:
2!pip show tensorflow
1# For the current version:
2!pip install --upgrade tensorflow
1# For a specific version:
2!pip install tensorflow==1.2
1# For the latest nightly build:
2!pip install tf-nightly

Git with Colab

Check out Git
1# Initialize the git repository (optional)
2!git init
1# Set the global username and email
2!git config --global user.email "[email protected]"
3!git config --global user.name "Your Name"
1# Add all the files
2!git add -A
3# or
4!git add .
1# Commit
2!git commit -m "Comment for that commit"
1# Pass your Github credentials
2!git remote rm origin # in the case you meet "fatal: remote origin already exists"
3!git remote add origin <https://<github-username>:<github-password>@github.com/><github-username>/<repository-name>.git
1# Push to origin
2!git push -u origin master
If you don't want to use your username andd password, you can use "Personal access tokens" on Github. Create one here and then use,
1!git git remote add origin <https://<username>:<access-token>@github.com/><username>/<repo>.git

Keep Colab awake

F1 then Console and type,
1function ClickConnect(){
2  console.log("Working");
3  document.querySelector("colab-connect-button").shadowRoot.getElementById("connect").click()
4}
5setInterval(ClickConnect,60000)

Change to current working directory

By default, the working directory is /content/. One can use below command to change to another place,
1%cd /content/data-science-learning
From that point, we are working on /content/data-science-learning.

Upload a file to Colab

Each user has a "machine" in /content/.
Create a new cell and paste,
1from google.colab import files
2
3uploaded = files.upload()
4
5for fn in uploaded.keys():
6  print('User uploaded file "{name}" with length {length} bytes'.format(
7      name=fn, length=len(uploaded[fn])))
Run 2 times this cell, at the 2nd time, you can choose your file.

Using Google Drive

Run a cell containing following codes,
1from google.colab import drive
2drive.mount('/content/drive')
and then follow the guide on the screen. In order to access to the drive,
1with open('/content/drive/My Drive/foo.txt', 'w') as f:
2  f.write('Hello Google Drive!')

Clone a repo from Github

1!git clone <https://github.com/dinhanhthi/data-science-learning.git>
The cloned folder are stored in /content/. If you wanna pull requests, use,
1%cd /content/data-science-learning
2!git pull

PyTorch with GPU

To enable GPU backend for your notebook, go to EditNotebook Settings and set Hardware accelerator to GPU. (ref)

Install 7zip reader, GraphViz, PyDot, cartopy

1# <https://pypi.python.org/pypi/libarchive>
2!apt-get -qq install -y libarchive-dev && pip install -q -U libarchive
3import libarchive
1# <https://pypi.python.org/pypi/pydot>
2!apt-get -qq install -y graphviz && pip install -q pydot
3import pydot
1!apt-get -qq install python-cartopy python3-cartopy
2import cartopy

Save as HTML

Jupyter Notebook has an option to 'Download as' HTML (or other) format. Google Colaboratory does not.
  1. Install the nbconvert package.
  1. Save your Colab notebook.
  1. In your terminal:
    1. 1jupyter nbconvert --to <output format> <filename.ipynb>
      2# jupyter nbconvert --to html mynotebook.ipynb

Other functions

  • Interrupt a long running python process: Runtime > Interrupt execution (Alt + M + I).
  • Support Jupyter magic commands, check full list here.
Loading comments...