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:
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.
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.
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.
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.
Next, I will run through linking your Colaboratory with a data source i.e. Google Sheets while running through some basics of Colaboratory.
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.
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.
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.
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())
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!
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
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.
Have fun with Colaboratory and please help to spread the word about this post too if you find it useful!