Ping Shiuan Chua
  • About
  • Blog
  • Projects
  • PSD Studio
  • Tools
    Keyword Cluster Keyword Grouping Broken Links Check Term-frequency Web Scraper
Python >> Colaboratory

Intro to Colaboratory and Linking it to Google Sheets

16 Apr 2018

Colaboratory is an amazing new tool built by Google that is a Jupyter notebook running on Python 2 or Python 3 ran entirely on the Cloud.

The main advantages I find to using Colaboratory are:

  1. Very easy to share your code: Just share it like any Google Drive files
  2. It's fast: I've tested it versus my laptop's Spyder IDE and Colaboratory is significantly faster as it can tap onto a GPU runtime
  3. No setup required: This makes it super easy to teach and share Python with other team members who might not have Python readily installed. They can just run it in the cloud!
  4. It's just a Jupyter notebook: Anyone who has used Jupyter will easily understand how to use this. The environment is also easy to learn compared to the native Python interpreter.

With that said, I'll be writing a few posts about using Colaboratory as I've found it extremely useful to educate more people about Python.

 

Opening Colaboratory and Quick Setup

Head over to the Colaboratory link (https://colab.research.google.com) and you'll be greeted with a very useful list of Colaboratory guides and code snippets ready for your perusal.

Code snippets for Colaboratory

Go ahead and click the "New Notebook" option at the bottom right and select either Python 2 or Python 3, depending on your use case and preference. Just select Python 3 if you have no preference.

You're now on Colaboratory! To make your runtime faster, head over to Runtime > Change runtime type.

Changing the runtime type for Colaboratory

You'll see a dialogue box for an option to change your Python runtime type and to enable hardware acceleration. Go ahead and select "GPU" under hardware accelerator.

GPU Hardware accelerator on Colaboratory

Next, I will run through linking your Colaboratory with a data source i.e. Google Sheets while running through some basics of Colaboratory.

 

Linking to Google Sheets

I find working with Google Sheets to be easier than working with a CSV file while on Python, be it on Colaboratory or elsewhere. That's my personal preference.

Google Sheets is easier to share, so other people can see your awesome work with Python and everyone can benefit from your amazing work.

Linking Google Sheets to Colaboratory is extremely easy. As with my earlier post on overpowering your Google Sheets with Python, I will use the pygsheets package again.

Firstly, we need to do a quick pip install to install the package on your notebook's environment. Type the following into a code cell and run it.

!pip install --upgrade -q pygsheets

The --upgrade flag ensures that the package will always be up-to-date every time you run the code, while the -q flag prevents the typical command line/terminal output that is not really important.

To run any cell on Colaboratory, either click on the Play button at the left-hand side of any code cell or click Ctrl/Cmd + Enter. If you want a new code cell to immediately appear while running this code cell, click Shift+Enter instead.

Actually, the right shortcut to adding a new cell after running a code is Alt+Enter. That will add regardless if you already have a next cell, while Shift-Enter will only add when your current cell is the last one.

The Colaboratory code is running

Try to segment your code into cells based on the stages of the code (e.g. installation, authorisation, initialisation, processing, output).

If you didn't click Shift+Enter, you will need to create a new code cell by clicking +Code at the top options bar or just hover to the bottom of your current cell and click +Code.

Adding a new code cell in Colaboratory

Next, we go through the authentication that enables the interaction between Colaboratory and a Google Sheets file in your Google Drive.

from google.colab import auth
auth.authenticate_user()

import pygsheets
from oauth2client.client import GoogleCredentials

gc = pygsheets.Client(GoogleCredentials.get_application_default())

If the above does not work for you, try this instead (Thanks Manuel from the comments section!)

import google.auth
from google.colab import auth

auth.authenticate_user()
import pygsheets
credentials, _ = google.auth.default()
gc = pygsheets.client.Client(credentials)

Upon running this cell, you will be prompted to click on a link. Follow the instructions and copy the authorisation code at the end of the process and paste it back in Colaboratory.

You're done! The gc object is the same one that was explained in my earlier post so please refer there to learn the inner workings of pygsheets!

 

Some Useful Tips

In the first screenshot of this post i.e. the lightbox dialogue box that greeted you on Colaboratory, there are three notebooks that I recommend you to read through to fully utilise the best of Colaboratory. These are

  1. Overview of Colaboratory Features: Explains all of the features of Colaboratory
  2. Markdown Guide: This is useful to make sure your code is readable and ensures proper documentation
  3. Forms: Enables a form input to make your notebook easier to use.

At any time, you can click on the left sidebar open button to view some useful code snippets that can be inserted into your notebook immediately.

Code snippets on Colaboratory notebook

Have fun with Colaboratory and please help to spread the word about this post too if you find it useful!

Share the love


Like the post? Consider donating to fund the maintenance of this website:

  • sponsor a green tea soy latte for a job well done
  • help to maintain this site for another month


Related Posts

Card image cap
Analysing Subreddit's Keyword Concentration with Python and PRAW
Card image cap
Make Your Colaboratory Code Easier to Use with Form Input
Card image cap
Importing Your Own Python Module or Python File in Colaboratory
blog comments powered by Disqus

© 2021 Copyright: pingshiuanchua.com

Terms of Use Privacy Policy