How to load files & insert images in Google Colabatory

Quick tips to get you up and running in Google colab

ShengJun
Data Arena
4 min readFeb 21, 2022

--

Google colaboratory can be a productive tool for well, collaboration. The name is a wordplay on “collaboration” and “laboratory” (i.e. experimentation). A simple way to see google colab is that if Github is an open-source repository for sharing and collaboration on projects, colab is a pseudo-GitHub hosting its version of jupyter notebook. Users need not worry about the setting up of virtual environments — just import the necessary libraries. This characteristic can be a huge time-saver, especially for users who are grappling with setting up local machines for Deep Learning 😄

Photo by Kevin Ku on Unsplash

One would need to first create a free account to use Google colab. It’s pretty straightforward, so it’s doesn’t bear repeating here. If you’d like to follow along, first head over to google colab and set up an account. Then, save a notebook in your drive. It’s recommended to place the colab notebook and all subsequent materials (including subfolders e.g. “data”) in that folder. You don’t have to follow this directory setup strictly, just make a note of where you have uploaded your data file; it does help if it’s in the same folder as the colab notebook though.

Example folder directory | image by Author

Loading Files in Google Colab

There are several methods of loading files into google colab. Here, we’ll go through one of the relatively simple ones. Simple in the sense we don’t have to deal with setting read/ write permissions of folders of the local machine.

First, upload the data file into the data folder in your drive. For our example, it’s a .csv file. Loading of the data file is going to be similar to how one does in a jupyter notebook, so other file formats are fine too. Just remember to use the relevant methods accordingly on your end.

Example data file | image by Author

Once uploaded, right-click on the file icon, change the share permission to “Anyone with the link can edit”.

Head over to colab, connect the session by clicking on “connect” in the upper right. Then run the following:

Mounting drive | image by Author

You should have a pop-up message on the colab notebook requesting permission to access your drive. Grant the permission.

On the left-hand side of the user interface, access the file directories (last icon). You should see a folder named “drive” like so:

orange folder icon to access files directory | image by Author

Navigate to the relevant folder where the data file has been recently uploaded to. Right-click and copy the path. Please note that the data file is not placed in a dedicated data folder for this example.

data file | image by Author

Create a variable and assign the copied file path as a string.

single or double quotes are fine | image by Author

Read and load the data file.

Data file loading | image by Author

Inserting images in Google Colab

Next, images. Images are inserted using the markdown. In colab, this is the + Text icons. Here’s we’ll go through two methods and discuss them.

The first method, insert an image from the local machine: Click on the “Insert image”, and choose the image from your local machine.

Landscape icon; 6th icon from the left | image by Author

In this example, I used one of the images prior (colab file directory)

image as it would appear on the right | image by Author

The second method, insert an image from Google drive: upload your image to the same folder where your colab notebook is residing. Once done, head over to the drive folder set the permission to “Anyone with the link can view”. Copy the link.

remember to set the image access permission | image by Author

Now, the link anatomy should be something like this:

link | image by Author

We are interested in the file identifier string “1en…nJK”. Highlight and copy using “Ctrl + C”.

image identifier | image by Author

Now paste the file identifier after the id in the code string. If the code string looks familiar, it is. It’s similar to the code string of images inserted in Jupyter notebooks.

image code | image by Author

I’d prefer the second method of image insertion; less space used on the local machine and, consolidating all the materials in a common folder.

That’s it! Hopefully, it helped you out in a pinch. Happy experimenting in google colab!

--

--

ShengJun
Data Arena

Engineer | Analyst | Matcha & Coffee lover | Iaidoka