16 Google Colab Tricks

Table of contents:

Google Colaboratory (Colab) gives the ability to build complex and heavy machine learning and deep learning models without having to expend our machine’s limited resources, but it is even more than that.

1. Use GPU

To use GPU and speed up machine learning tasks you need to enable it.

Runtime -> Change runtime type Protocols Protocols

2. Use Google drive

At some point you may need to mount use Google drive to your Colab notebook. This is often because you need data. The next code will make this possible:

from google.colab import drive

The process has the authentication step but it’s fairly easy.

3. Execute Linux commands

To execute any Linux shell command just use ! in front of the command. This way you can list the content of a current folder, for instance.


4. Get interactive shell

You can also use the interactive shell:



I prefer to use just the ! command to run bash commands.

5. Clone the entire repo from GitHub

To bring data to Colab notebook is to clone some data from GitHub:

!git clone -l -s https://github.com/pytorch/pytorch cloned-repo
%cd cloned-repo

Note that you need to set the ! sign to execute Linux command.

6. Get data using wget

If you have a file on web the simplest way to get that file is to use wget:

!wget https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/Kust_till_kust_banan.JPG/1200px-Kust_till_kust_banan.JPG -O image.jpg

Now, your Google Colab notebook will have the image.jpg


7. Upload file from local file system

from google.colab import files

uploaded = files.upload()

for fn in uploaded.keys():
  print('User uploaded file "{name}" with length {length} bytes'.format(
      name=fn, length=len(uploaded[fn])))

This script is officially part of Google Colab.


and importing .py files in Google Colab

from google.colab import files
src = list(files.upload().values())[0]
import mylib

upload import

8. Navigate using %cd command

To navigate to a folder you can use %cd command:

%cd /
%cd /content



9. Use gdown to download Google drive resources

gdown tool is already installed Linux command on Google Colab so you just need to call:

!gdown --id 1uWdQ2kn25RSQITtBHa9_zayplm27IXNC -O clothing.json

The next image shows how to find the document id

document id

To can also download documents Python way:

import gdown
gdown.download("https://drive.google.com/uc?id=1uWdQ2kn25RSQITtBHa9_zayplm27IXNC", output="cloting.json", quiet=True)

10. Preview text file or image

This is one of the best tricks. You can actually open and edit text files if you just double click them:

open file

Quite similar you can preview the images.

11. Rename files

To simple rename file you don’t need to use anything other than right click the file name.

rename file

12. Delete file

To delete a file just right click the file, select “Delete” and confirm.

delete file

13. Stop Google Colab From Disconnecting

Open browser console F12 on Firefox or Ctrl+Shift+I and write down the following JavaScript code:

function KeepClicking(){

This way your notebook will be open for the 12 hours.

14. Basic Preview of a website

I found it nice to create basic website preview if you run this code.

import requests
url = "https://programming-review.com"
    r = requests.get(url) # requests.models.Response
    from IPython.display import HTML
    print("Error with the request")


15. Use Conda without installing

For some project you need to have Conda, and then to install these projects you need to run conda install.

To save some time you can specify the directory to install Conda in on Google drive:

conda install -p path_to_your_dir

So, you can just mount your Google drive and use Conda without installing each time.

16. Combine two audio files together

Nice option you will have when working with Google Colab is to use run resource intensive tasks such as ffmpeg:

! ffmpeg -i "concat:first.mp3|second.mp3" -acodec copy combined.mp3

tags: tricks - google & category: colab