Resources
- Introduction to Colab and Python
- Getting Started with Google Colab
- External data: Local Files, Drive, Sheets, and Cloud Storage
- How to Connect Google Colab with Google Drive
- Embedding your image in google colab markdown
What is Google Colab
“Colaboratory is a free Jupyter notebook environment that requires no setup and runs entirely in the cloud. With Colaboratory you can write and execute code, save and share your analyses, and access powerful computing resources, all for free from your browser… As the name suggests, Google Colab comes with collaboration backed in the product. In fact, it is a Jupyter notebook that leverages Google Docs collaboration features. It also runs on Google servers and you don’t need to install anything. Moreover, the notebooks are saved to your Google Drive account.” [source]
Advantages over Jupyter Notebook
- Simple installation and setup
- Easy integration with Google Drive and Github
- Cloud computing (use google servers and GPUs instead of your hardware)
- Automatically saves progress (in the event of power outage or computer freezes)
- Collaborative environment for group coding projects
- Persistent notebook version for all teammates
- Analogous to using Google Docs instead of Microsoft Word for group reports
- Plenty of quality of life tools without needing to install extensions
- Collapsible headings
- Live markdown preview
- Table of contents
- Coding tooltips: autofill defined variables
- Integrated Widget Cells: forms and sliders for easy data entry
- Scratch cell: A cell to run test code but which isn’t saved in your notebook.
Initial Setup
Installing Google Colabatory
- In google drive, go to New>More>Connect more apps
- Search for “Colaboratory”
- Install the drive add on
Starting new Google Colab Notebook
- In google drive, go to New>More>Google Colaboratory
- This opens up a fresh Google Colab Notebook in the drive directory you were currently in
- The file will be a
.ipynb
file and can be downloaded as a Jupyter Notebook to run on a local instance if needed (File>Download .ipynb)
Importing Juypiter Notebook File
- In google drive, go to New>File Upload
- Double click the .ipynb file
- If it does not open the file with collabatory by default; right click>Open With>Google Collaboratory
Installing Dependencies
pip Install (Recommended)
Google Colab comes with most standard packages by default but we can install additional packages using an exclamation mark. In Google Colab, the exclamation mark “!” is used for executing commands from the underlying operating system. This means that we can use this to execute the pip command and install packages for the current notebook. For example, we can install the standard packages from the Foldable Robotics Website by following the same process but adding the modifier “!”.
RUN PIP INSTALL CODE BLOCK
-
Install standard packages…
!pip install meshio pygmsh ezdxf pandoc-fignos pandoc-eqnos pypdf4 service_identity ftd2xx pygithub twine paho-mqtt !pip install ntplib pygame pysftp pyserial
-
Install foldable-robotics-specific packages.
!pip install pypoly2tri idealab_tools foldable_robotics pynamics
Manual Install
A package can be manually installed by adding the .whl file to Google Drive and using the pip command on this file. For convenience, it is probably best to add it to the same directory as the Google Colab file in your drive. To pip install a .whl file we just have to run the pip command and referencing the path to that file.
Note: To access a file stored in your Google Drive, your google drive must be mounted to the google Colab notebook. For details regarding mounting our drive see the “Mounting Drive to Google Colab” section.
DO NOT RUN IF ALREADY RAN COD IN PIP INSTALL SECTION
from google.colab import drive
Where the drive will be mounted in the Colab notebook
ROOT = '/content/drive'
Path to the file in YOUR DRIVE. Will be different for you
FILE_PATH = '/MyDrive/GoogleColabExample/pynamics-0.0.8-py2.py3-none-any.whl'
Accessing a file on your drive
drive.mount(ROOT, force_remount=True) # Mount the drive to this notebook
with open(ROOT+FILE_PATH,'w') as f: # Open the file in the drive
!pip install f # Run the pip install command for the file
This next command is to unmount my drive from th notebook so that it my personal files will not be made available if this Google Colab File is shared
drive.flush_and_unmount()
Verifying Imported Packages
RUN THIS COD BLOCK TO ENSURE ALL PACKAGES ARE INSTALLED
import pynamics
from pynamics.frame import Frame
from pynamics.variable_types import Differentiable,Constant
from pynamics.system import System
from pynamics.constraint import Constraint
from pynamics.output import PointsOutput
import sympy
import numpy as np
import matplotlib.pyplot as plt
import scipy.optimize
from math import pi
If there are no errors in the output, the packages have been properly installed
Note: Packages that do not come installed by default will have to be reinstalled using the pip command for each new notebook.
Mounting Drive to Google Colab**
This section is only needed if you need to access external files that are located in your google drive
In order for your Google Colab notebook to have access to the files on your Google Drive, you must first “mount” your drive to the notebook. This process can only mount the top-level drive to a notebook and it will expose all the files in your drive to the Google Colab notebook until it is unmounted. Therefore, it is recommended to always follow mounting your drive with the unmount command if you do not wish to leave the entirety of your drive accessible by a Google Colab notebook. As long as the variable used to store the imported file is not overwritten later in the script, the imported object will remain saved in the notebook.
Note: When running the “drive.mount(ROOT, force_remount=True)” mount command, it will ask for authorization to mount the drive associated with the selected google acount. The output of the code cell will provide instructions to complete this process.
Disclaimer: Your drive will remain mounted to the Google Colab notebook until you unmount it. That means people with access to the notebook will be able to access your drive until it is unmounted. Once unmounted, another party will not be able to access your drive since they would need to perform the authorization process which requires a sign in from your google account.
Mounting and Unmounting a Drive Using a Code Block (Reccomended)
from google.colab import drive
Where the drive will be mounted in the Colab notebook
ROOT = '/content/drive'
Path to the file in your drive. Will be different for you
FILE_PATH = '/MyDrive/Folder1/Folder2/file'
#FILE_PATH = '/Shareddrives/Folder1/Folder2/file'
Mount the drive to this notebook
drive.mount(ROOT, force_remount=True)
Append full path
full_path_to_file = ROOT+FILE_PATH
############################################
#### INSERT CODE TO PROCESS/IMPORT FILE ####
# EX1
import csv
data = csv.reader(full_path_to_file, delimiter=',')
Example Code
# EX2
import cv2
img = cv2.imread(full_path_to_file)
#### INSERT CODE TO PROCESS/IMPORT FILE ####
############################################
Unmount Drive
drive.flush_and_unmount()
As long as the variable used to import the file (data/img) is not overwritten or deleted, the variable can be used later in the code with an unmounted drive
Mounting and Unmounting a Drive Using the UI
Alternatively, you can use the toolbar on the left to mount the drive to a notebook. This does not require account verification since you are accessing the notebook through your google account. If you do not have any code cells that mount a drive. This can be done by
- Selecting the files button (folder icon)
- Selecting the Mount Drive button (dark folder with google drive icon)
- Giving permission for the notebook to access your Google Drive
Note: This is not recommended since you will have to manually unmount your drive (using the mount button again) or execute the unmount command in a code block.
Version Control
Google Colab’s Revision History
Google Colab has an integrated version control through the ability to view revision history. This is the same process as Google Docs in which you can restore any versions that have been automatically saved at regular intervals. You can view the revision history by going to:
File>Revision History
Pushing Google Colab Notebook to Github
Also, there is a github integration that commits the file to our github repository which automatically does version control for us through its commit history functionality. To commit a google colab file to github:
-
In colab go to “File>Save Copy in Github”
-
Sign into your github account
-
Select the repository where you want your notebook to be saved by using the dropdown box
-
You can append subdirectories in the “File Path” dialog box by appending “folder_name/” to the notebook’s file name “file_name.ipynb”"
EX: "folder/file_name.ipynb"
-
Select “ok” and the github repository will be updated with the new notebook
Submitting Google Colab Notebook to Canvas
Submitting a Compiled .ipynb File
- Download the Google Colab Notebook as a Jupyter Notebook file (.ipynb file). From the Google Colab notebook, select File>Download .ipynb
- Upload file to canvas
- Referencing images and files can be slightly different in the Colab notebook (through direct html link or mounted drive) than in Jupyter Notebook (can include by referencing local directory).
- To the best of my knowledge the notebooks should be functionally identical with the exception of the mounting system
- I would like to ensure that the a compiled Google Colab Notebook is acceptable for submission before moving forward with it
Submitting a PDF File
I am currently unaware of a way to easily print a Google Colab Notebook using MiKTeX or other LaTeX typesetting system. Google Colab has a print function that is able to generate a PDF through html but I would assume that this would not be up to submission standards.
- Download the Google Colab Notebook as a Jupyter Notebook file (.ipynb file). From the Google Colab notebook, select File>Download .ipynb
- Open the downloaded file in a Jupyter Notebook Session
- Export the notebook as PDF. File>Download as> PDF via LaTeX (.pdf)
Adding Images to Google Colab Notebook
Directly Embedding Image using Copy & Paste
Google Colab has a very useful of feature that is able to add an image to a text cell by pasting an image from your clipboard. Once an image is pasted, Google Colab will insert the proper syntax for displaying an image embed the raw image data directly into the notebook.
Note: Although not required, it is reccomended to have a pasted image be in its own markdown cell. This is because the image data in text format is very large and can be difficult to move relative to other text in the same cell. This can be seen by editing the text cell for the image (double click image) in sections 8.1 and 8.2.
- Copy image from web, screenclipping, or other source
- Start a new text cell
- Paste the image
Adding Custom Images from Google Drive without Mounting
Full Tutorial on how to embed images
An alternative way to add a custom image to mounting your drive is to get a public link to an image saved on your drive. Once an image has been uploaded into your drive (does not matter where), follow the following steps:
-
Right click the image in the drive
-
Select “Get Link”
-
Change the sharing settings from “Restricted” to “Anyone with the link”
-
Paste this link into a Google Drive Direct Link Generator (link to generator) or follow the steps below to find the direct link manually a. Paste the link into any text editor
**EX:** https://drive.google.com/file/d/1jh9A3LRK8e3i982GhgzmRb4kYrvdTJti/view?usp=sharing
b. Copy the image ID (the long string of random alphanumeric characters)
**EX:** img_id = 1jh9A3LRK8e3i982GhgzmRb4kYrvdTJti
c. Append to the direct image link url: https://drive.google.com/uc?export=view&id="img_id"
**EX** https://drive.google.com/uc?export=view&id=1jh9A3LRK8e3i982GhgzmRb4kYrvdTJti
-
Use markdown to display this image
![Image Example](https://drive.google.com/uc?export=view&id=1jh9A3LRK8e3i982GhgzmRb4kYrvdTJti)