Resources

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

Initial Setup

Installing Google Colabatory

  1. In google drive, go to New>More>Connect more apps
  2. Search for “Colaboratory”
  3. Install the drive add on

Starting new Google Colab Notebook

  1. In google drive, go to New>More>Google Colaboratory
  2. This opens up a fresh Google Colab Notebook in the drive directory you were currently in
  3. 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

  1. In google drive, go to New>File Upload
  2. Double click the .ipynb file
  3. If it does not open the file with collabatory by default; right click>Open With>Google Collaboratory

Installing Dependencies

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

  1. 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
    
  2. 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

  1. Selecting the files button (folder icon)
  2. Selecting the Mount Drive button (dark folder with google drive icon)
  3. 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:

  1. In colab go to “File>Save Copy in Github”

  2. Sign into your github account

  3. Select the repository where you want your notebook to be saved by using the dropdown box

  4. 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"
    
  5. Select “ok” and the github repository will be updated with the new notebook

Submitting Google Colab Notebook to Canvas

Submitting a Compiled .ipynb File

  1. Download the Google Colab Notebook as a Jupyter Notebook file (.ipynb file). From the Google Colab notebook, select File>Download .ipynb
  2. Upload file to canvas

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.

  1. Download the Google Colab Notebook as a Jupyter Notebook file (.ipynb file). From the Google Colab notebook, select File>Download .ipynb
  2. Open the downloaded file in a Jupyter Notebook Session
  3. 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.

  1. Copy image from web, screenclipping, or other source
  2. Start a new text cell
  3. Paste the image

image.png

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:

  1. Right click the image in the drive

  2. Select “Get Link”

  3. Change the sharing settings from “Restricted” to “Anyone with the link”

  4. 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
    
  5. Use markdown to display this image

    ![Image Example](https://drive.google.com/uc?export=view&id=1jh9A3LRK8e3i982GhgzmRb4kYrvdTJti)