Digital Education Resources - Vanderbilt Libraries Digital Lab
Previous lesson: Programming environments
This lesson is for people who want to get started programming Python as quickly as possible using a Colab notebook with no installation required.
Learning objectives At the end of this lesson, the learner will:
Total video time: 13m 30s (19m 37s including optional videos)
Technically, Colab notebooks are not exactly the same as Jupyter notebooks, but they are similar enough that we will consider them to be the same thing. A Jupyter notebook can be uploaded to Colab and a Colab notebook can be downloaded as a Jupyter notebook.
NOTE: If for some reason you cannot use Colab notebooks (e.g. you can’t get a Google account or don’t have regular access to the Internet), you can run all of the subsequent Python lesson exercises using a local Jupyter notebook installation. See Installing a programming environment for more information on this option, then view the Jupyter architecture section below to get started.
Google Colaboratory landing page. If presented with an options screen, you can click New Notebook to create a blank notebook or cancel to open an actual Colab notebook that will run. See the next section for information about how to copy this notebook to your own Google Drive.
Once you have stored your own notebooks, you can select a Recent notebook, or open notebooks from your Google Drive.
Example notebook in Colab. The link will open the notebook in read-only mode. To run the notebook without saving, click Open in playground
, and agree to the warning. Click the play button at the left of each cell in order to run the script.
In order to be able to save any changes you’ve made to the script, you need to copy it to your own Google Drive by clicking the Copy to Drive
link at the upper left, or select Save a copy in Drive
from the file menu.
To open a notebook cloned to your own Google Drive, go to your Drive, click on the Colab Notebooks
folder, then a notebook name. Then click on the Open with Google Colaboratory
button at the top of the screen to open the notebook at the colab.research.google.com
site.
Because Colab notebooks are running in the cloud, nothing is actually happening on your local computer except that your browser is showing you what’s happening on the Colab server. The instance of Python that’s running on the server and being displayed in the notebook is called a runtime. The runtime can be reset or restarted using the notebook controls but it’s terminated by closing the browser window, or after a certain amount of inactivity. (To compare this with how Jupyter notebooks work, see the Jupyter architecture section below.)
.
To run a cell in a notebook, click the triangular “run” button at the left of the cell or hold down the Ctrl
key and press the Enter
(or Return
) key. (When running the first cell, there is a delay as the runtime is initiated. This is made apparent by the rotating dashes around the run button.)
While the cell is running, there will be a rotating arc at the left of the cell.
To clear the output of a cell, click the “hamburger” at the upper right of the cell or right-click, then select Clear output
. To clear the output of all cells, choose Select all cells
from the Edit
menu (or press ctrl-shift-A), then clear output as above. NOTE: clearing the output does not interrupt execution of a cell or reset the runtime!
To terminate execution of a cell, press the square “stop” button inside the circle, or select Interrupt execution
from the Runtime
menu.
If you are still having problems and aren’t able to terminate execution, go to the Runtime
menu and select Restart runtime
. That will not only terminate execution, but will clear variable values in the environment.
It is not necessary to shut down the runtime at the end of a session. The runtime will shut down automatically when you close the tab or after a long period of inactivity.
If you are using Colab exclusively, it is not necessary to view the remaining parts of this page. However, if you want to use conventional Jupyter notebooks, or if you are curious about how they differ from Colab notebooks, you may find the information helpful.
A normal Jupyter notebook installation operates via a Jupyter web server that is running on the local computer (localhost). The server is started up from the command line console (Terminal on Mac or Command Prompt on Windows) and can be killed there using ctrl-C. It is also possible to shut down the server using the Quit
button on the navigation web page. IMPORTANT NOTE: Simply closing the console window does NOT shut down the server. It will continue to run and use resources on the computer until the next time the computer is restarted. So the server should always be shut down by one of these two methods before closing the console window.
Instances of Python that are running in a browser tab are called kernals. They can be launched from the navigation web page using the New
button or by clicking on a Jupyter notebook file (.ipynb
) link. The kernal for a particular tab should be shut down using the Shutdown
item from the Kernal
menu before closing the tab. If the kernal is not shut down, it will continue to run after the tab is closed. However, shutting down the Jupyter server will close all running kernals regardless of whether their tabs are open or not.
Remotely hosted Jupyter notebook servers (e.g. Wikimedia Foundation’s PAWS) operate in a similar manner, except that you probably will not be able to shut down the Jupyter server (only individual kernals).
.
Cells are run by clicking on the Run
button (or holding the Shift
key, then pressing the return
or Enter
key). When a cell is running, an asterisk (*
) appears to the left of the cell.
You can clear the output of cells by going to the Cell
menu and selecting Current Outputs
then Clear
, or All Output
then Clear
. NOTE: clearing the output does not interrupt execution of a cell or restart the kernal!
To interrupt a running cell, click the square stop button at the top of the page. If the page is unresponsive, select Restart
(or Restart & Clear Output
) from the Kernal
menu. Restarting the kernal will also clear the values of all variables in the environment.
When you are finished using a notebook, you should shut down its kernal by selecting Shutdown
from the Kernal
menu before you close the notebook’s tab.
Next Python lesson: Python programming basics
Revised 2022-01-07
Questions? Contact us
License: CC BY 4.0.
Credit: "Vanderbilt Libraries Digital Lab - www.library.vanderbilt.edu"