This tutorial is adapted from the Web Age course https://www.webagesolutions.com/courses/WA3174-pragmatic-python-programming.
Google Colaboratory (CoLab) is a free Jupyter notebook interactive development environment (REPL) hosted in Google’s cloud that we are going to use in this course.
In this tutorial, you will learn about the main features of the Google CoLab REPL.
Part 1 – Access the CoLab Environment and Create Your First CoLab Notebook
To use the CoLab environment, you need to have an active Google Account (valid Gmail account).
1. Open your favorite browser (Chrome is highly recommended) and navigate to
https://colab.research.google.com/
2. Quickly review the contents of the What is Colaboratory page.
3. Click the Sign in button in the top right-hand corner of the page, and sign in with your Google Account credentials.
4. After successful signing in, click File >New notebook in the top left-hand corner.
After a few moments, you will be provisioned with a free (at that time deactivated) CoLab notebook, which, by default, is backed by the Python 3 kernel (run-time).
5. In the top right-hand corner, click Connect to activate your CoLab environment.
A new notebook named Untitled0.ipynb will be provisioned. You can change the notebook name by clicking the name input box and typing in the name you feel is more suitable.
6. Rename your notebook to Learning CoLab Lab
7. Hover your mouse over the RAM Disk UI widget to see the resources available to your CoLab REPL.
You will have about 12 GB RAM and 100 GB free disk space available to you (your numbers may differ, though).
Be aware that your CoLab virtual machine will be recycled (and its resources wiped out) after you terminate your working session or after a period of inactivity, so make sure you save your programs so you can recreate them in a new fresh session (Ctrl-S).
Your CoLab environment will be automatically deactivated after about 1.5 hours of idle time (the time may vary).
Part 2 – The CoLab Jupyter Notebook Development Environment
As mentioned earlier, Google Colaboratory (CoLab) is a free Jupyter notebook interactive development environment.
A Jupyter (note the spelling of the word) notebook is a browser-based development environment that represents an evolution of the original IPython (Interactive Python shell) command-line REPL.
Your Jupyter workspace is a collection of notebooks that are basically dynamic interactive HTML5 web pages containing a mix of your code, embedded visualizations, comments, and other such artifacts. You can export your code from Jupyter as a regular Python file that you can then execute from the command line; you can also load external scripts.
Jupyter uses the concept of the kernel which is, essentially, a specific language run-time. In addition to Python, Jupyter supports kernels for R, Go, C#, and others (https://github.com/jupyter/jupyter/wiki/Jupyter-kernels).
The Jupyter development environment can be augmented with additional plugins, like spell checkers.
1. Enter the following command in the active cell and press Shift+Enter to submit it for execution:
print ("Hi there!")
You should see the following output:
Hi there!
Alternatively, you can click the Play icon to the left of the active cell.
When you submit a command for execution, a new working cell will be provided just below the current active cell for your next command(s).
You can add a new command input cell below or above the cell in focus by hovering your mouse just below or above any cell in your notebook or any cell output window until +Code and +Text small popups appear.
Alternatively, you can use these shortcuts to add a new cell:
- Ctrl-a – add a cell above the current cell
- Ctrl-b – add a cell below the current cell
Like in IPython, you can get immediate help on any command or function using the ? operator.
2. Enter the following commands:
import numpy as np np.st*?
A new window pane will open on the right of the page, printing a list of NumPy functions that start with st
To get the Docstring help of a function, use the full function name with the ? operator next to it, e.g.:
np.std?
To get access to the CoLab Command Palette, press Ctrl-Shift-P
To see the list of configured keyboard shortcuts and create new ones, go to Tools > Keyboard shortcuts
Two “SysAdmin”-like commands that you may want to be familiar with are Interrupt execution and Restart runtime that are invoked via these shortcuts:
The Interrupt execution command will help you break from an infinite loop or similar situations; the Restart runtime command causes the back-end Python VM to restart which may be useful in resetting all the variables and starting your program from a clean slate.
As you progress through the labs in the course, you can create a new lab notebook by selecting File > New notebook
You can download your notebook either as a Jupyter notebook or a Python file by going to File > Download
Part 3 – The Shell Terminal is Not Available
The free CoLab environment does not give you a shell terminal for command-line access to the underlying virtual machine — for that, you will need to upgrade to the paid CoLab Pro account. The CoLab Pro account also offers developers additional assurances of service quality like continuing to run the code in your notebook even after you shut down the browser (which is a great capability for long-running jobs).
Part 4 – Command-Line Interfaces in Jupyter Notebooks and Python
You can execute system-level commands from Jupyter notebooks using the ! operator interface as illustrated in the command below:
!ls -l /bin
The output of the system-level commands can be captured and used in your Python code using the check_output function from the subprocess module as shown in the code snippet below:
from subprocess import check_output file_list = (check_output(['ls', '-l', '/bin'])).decode()
The above code will run the ls -l /bin system-level command and then capture the systout (console) output of the command as the output of the check_output Python function.
Part 5 – Uploading Data Files to CoLab Environment
You can upload and download files to and from the CoLab working environment.
Access to the underlying virtual machine’s file system is provided through the Files icon in the left-hand navigation bar.
To upload a file, click Files > Upload to session storage icon and select the file to upload.
Note: CoLab virtual machines are recycled at the end of the working session (when you either shut it down manually or your session times out after a certain period of inactivity). By extension, the file system attached to the VM is ephemeral — all the files you persist on the VM will be purged, so when you re-launch your CoLab Jupyter notebook at a later time, those files will be gone.
While you are allowed to download and persist files on the CoLab machine’s file system (for the duration of your working session), you will only have programmatic IO access to those files from your programs.
Part 6 – The “Magic” Commands
The “magic” commands are simply macros that are prefixed with a ‘%‘ or ‘!‘ character and execute a sequence of commands or provide a shortcut to some system housekeeping functionality.
You can take advantage of the following “magic” commands that are supported by CoLab Jupyter notebooks:
%history - print the command history %quickref - print magic commands quick reference card %who - print all the interactive variables (like loaded module) %run - run a Python script from the local file system %load - load a Python script from the local file system into the working notebook %%timeit - time the execution time of code in the %%timeit-annotated cell (it should be the first command in the cell, with the code to time to follow) ! - execute OS command, e.g. !wget or !python -V
Part 7 – Uploading CoLab Notebooks
When you see a lab instruction to upload a notebook to the CoLab working environment, follow these steps:
- If you are on the Welcome to Colaboratory page, click Upload and follow the instructions there.
- If you have a notebook open in your browser, click File in the menubar, select Upload notebook, and follow the instructions there.
Part 8 – Save Your Work
1. Press Ctrl-S to save your Notebook.
Keep the browser up and running.
This is the last step in this lab.
Part 9 – Review
In this tutorial, you learned the basics of using the CoLab Jupyter development environment.