Colab is one of the best products to come from Google. It has made GPUs freely accessible to learners and practitioners like me who otherwise wouldn't be able to afford a high-end GPU.
While the interface is very easy to use, there are many lesser-known and undocumented features in colab. In this post, I will share those features that I've discovered from their official talks and basic usage.
It's a pretty common scenario that we have a bunch of cluttered untitled notebooks created when we try out temporary stuff on colab.
It's pretty common that we manually calculate the difference between start and end times of a piece of code to gauge the time taken.
Colab provides an inbuilt feature to do this. After a cell is executed, just hover over the cell run icon and you will get an estimate of the execution time taken.
You can also run only a part of the cell by selecting it and pressing the
Runtime > Run Selection button or using the keyboard shortcut
Ctrl + Shift + Enter.
If you are familiar with keyboard shortcuts from Jupyter Notebook, they don't work directly in Colab. But I found a mental model to map between them.
Ctrl + M before whatever keyboard shortcut you were using in Jupyter. This rule of thumb works for the majority of common use-cases.
|Action||Jupyter Notebook||Google Colab|
|Add a cell above||A||Ctrl + M + A|
|Add a cell below||B||Ctrl + M + B|
|See all keyboard shorcuts||H||Ctrl + M + H|
|Change cell to code||Y||Ctrl + M + Y|
|Change cell to markdown||M||Ctrl + M + M|
|Interrupt the kernel||II||Ctrl + M + I|
|Delete a cell||DD||Ctrl + M + D|
|Checkpoint notebook||Ctrl + S||Ctrl + M + S|
Below are some notable exceptions to this rule for which either the shortcut is changed completely or kept the same.
|Action||Jupyter Notebook||Google Colab|
|Restart runtime||00||Ctrl + M + .|
|Run cell||Ctrl + Enter||Ctrl + Enter|
|Run cell and add new cell below||Alt + Enter||Alt + Enter|
|Run cell and goto the next cell below||Shift + Enter||Shift + Enter|
|Comment current line||Ctrl + /||Ctrl + /|
Similar to an IDE, you can go to a class definition by pressing
Ctrl and then clicking a class name. For example, here we view the class definition of the Dense layer in Keras by pressing Ctrl and then clicking the
Dense class name.
The Google Colab team provides an official chrome extension to open notebooks on GitHub directly on colab. You can install it from here.
Alternatively, you can also manually open any GitHub notebook by replacing
With a library called flask-ngrok, you can easily expose a Flask web app running on colab to demo prototypes. First, you need to install
!pip install flask-ngrok flask==0.12.2
Then, you just need to pass your flask app object to
run_with_ngrok function and it will expose a ngrok endpoint when the server is started.
from flask import Flask from flask_ngrok import run_with_ngrok app = Flask(__name__) run_with_ngrok(app) @app.route('/') def hello(): return 'Hello World!' if __name__ == '__main__': app.run()
You can try this out from the package author's official example on Colab.
You can easily switch between Tensorflow 1 and Tensorflow 2 using this magic flag.
To switch to Tensorflow 1.15.2, use this command:
To switch to Tensorflow 2.2, run this command:
You will need to restart the runtime for the effect to take place. Colab recommends using the pre-installed Tensorflow version instead of installing it from
pip for performance reasons.
Colab also provides a magic command to use Tensorboard directly from the notebook. You just need to set the logs directory location using the
--logdir flag. You can learn to use it from the official notebook.
%load_ext tensorboard %tensorboard --logdir logs
Colab provides the following specs for their free and pro versions. Based on your use case, you can switch to the pro version at $10/month if you need a better runtime, GPU, and memory.
|Version||GPU||GPU Ram||RAM||Storage||CPU Cores||Idle Timeout||Maximum Runtime|
|Free||Tesla K80||11.44GB||13.7GB||37GB||2||90 min||12 hrs|
|Pro||Tesla P100||16GB||27.4GB||37GB||4||90 min||24 hrs|
You can view the GPU you have been assigned by running the following command
For information on the CPU, you can run this command
Similarly, you can view the RAM capacity by running
import psutil ram_gb = psutil.virtual_memory().total / 1e9 print(ram_gb)
There is no built-in interactive terminal in Colab. But you can use the
bash command to try out shell commands interactively. Just run this command and you will get an interactive input.
Now, you can run any shell command in the given input box.
To quit from the shell, just type
exit in the input box.
Colab provides an indicator of RAM and disk usage. If you hover over the indicator, you will get a popup with the current usage and the total capacity.
In the markdown code, we're loading an SVG image and then linking it to a colab notebook.
[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/notebooks/basic_features_overview.ipynb)
Colab provides a notebook extension to add interactive sorting and filtering capabilities to pandas dataframes. To use it, run the following code.
If you use miniconda as your python environment manager, you can setup it on colab by running this command at the top of your notebook.
# Download Miniconda installation script !wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh # Make it executable !chmod +x Miniconda3-latest-Linux-x86_64.sh # Start installation in silent mode !bash ./Miniconda3-latest-Linux-x86_64.sh -b -f -p /usr/local # Make conda packages available in current environment import sys sys.path.append('/usr/local/lib/python3.7/site-packages/')
After the cell is executed, you can use conda to install packages as usual.
!conda install -y flask
You can use a library called colab-cli to easily create and sync colab notebooks with your local notebooks.
There are use-cases when we need to start some web server or background tasks before we can execute our regular program.
To run background tasks, use the
nohup command followed by your regular shell command and add
& to the end to run it in the background. This makes sure that you can run cells afterward in the notebook without your background task blocking it.
!nohup bash ping.sh &
If you're running a long task such as training a model, you can setup Colab to send a desktop notification once it's completed.
To enable that, goto Tools ⮕ Settings ⮕ Site and enable
Show desktop notifications checkbox.
You will get a popup to enable browser notification. Just accept it and colab will notify you on task completion even if you are on another tab, window or application.