Data Science Shortcuts

Quickly Open a Github Repository Notebook in Google Colab

When cloning isn’t convenient

Jamel Dargan
4 min readOct 2, 2020

I regularly read articles on Python projects coded in Jupyter notebooks where the author includes a link to the project’s Github repository. Sometimes, I want to see the code in action for myself. For example: If the code includes a snippet, practice, or visualization that I am interested in trying out in my own work, I may want to validate that the code is up to date with currently available libraries.

One way to test the code is to create a fork of the repository on my own profile, which has the advantage of copying any data and supporting files included in the original repo. But sometimes, if the dataset is small or if I just want to render a notebook visualization that does not show on Github, I would rather just open the notebook online without having to clone it locally.

Fortunately, the task can be accomplished easily, using Google Colaboratory (Colab).

Screen capture detail of the Google Colab landing page with a modal dialog open for selecting a notebook.
Screen capture detail of the Google Colab landing page with a modal dialog open for selecting a notebook.

For the uninitiated, Google’s Colab is a Jupyter notebook that runs in the cloud. The cross-platform service provides users with a free environment in which to perform shareable machine learning. For more detailed information, of course, you can Google it.

If you are familiar with Colab, you may already appreciate that there are many reasons you to view an existing Jupyter (.ipynb extension) file online and on-the-fly:

  • You are sitting in a cafe with a smartphone and an internet connection, and you want to test code or view cell output.
  • You have an idea for improvement that you want to test-out rapidly before you bother to fork your own copy of the repository.
  • You do not want to fork, because you don’t want to give the false impression that you intend to contribute to the original repo — or perhaps just not any time soon.
  • You are looking for a potential project partner, you have found a repo by someone who seems to fit, and you want to check out their code.

So, how can we do it?

Daytime image, closeup on part of a spiderweb with multiple rows draped in front of a blurred, verdant background.
Photo by Thomas Bishop on Unsplash

You could clone the entire repository

Before writing this article, I performed a quick search to avoid reinventing the wheel. I found a decent example of how you can clone a repository to Colab in Steve Tan’s article, here on Medium. If you do not want to clone an entire repository, read-on to see our embarrassingly easy trick.

As mentioned in Steve Tan’s article, you will need to be logged into Google to use Colab. I will presume that you can manage that without much trouble, whether you already have an account or need to sign-up for one. In addition, the notebook you wish to preview/run needs to be from a public repository (or at least one that you have permission to access).

Quickly jump into a notebook from Github

First copy this text snippet, the URL for Google Colab, to your clipboard:

colab.research.google.com/github

Then browse to the repository on Github that contains the notebook you would like to test. For example, we can try it out with ArkoMukherjee25’s “Data-Visualization” repo, https://github.com/ArkoMukherjee25/Data-Visualization—made available under the Apache License 2.0.

Screen capture of our example repo, on Github.
Screen capture of our example repo, on Github.

Now locate the hyperlinked name of the “.ipynb” notebook file and activate the link. A static, markdown preview of the file should begin to load in your browser.

Screen capture of the markdown preview of our example notebook, on Github, with text to be replaced highlighted.
Screen capture of the markdown preview of our example notebook, on Github, with text to be replaced highlighted.

In the browser’s address bar, delete all characters of the web page URL through “github.com” and replace them with the text snippet you copied above. The new URL should read as follows:

Screen capture of the browser’s address bar, showing the modified URL.
Screen capture of the browser’s address bar, showing the modified URL.

Submit/follow the created link to open the notebook inside of Google Colab.

Screen capture of the data visualization notebook, open in Colab.
Screen capture of the data visualization notebook, open in Colab.

That’s it. You are viewing the notebook, and you can run code cells to test cell output.

…But, what about the data?

In the data visualization repository we used for this example, only one of the notebook’s fifteen visualizations requires an imported dataset — a small “weather.csv” file read-into a Pandas dataframe. Most of the data is generated from Numpy or from Pandas lists.

If you are working with a notebook that requires a data file from the repo, you can read the data directly from Github. It isn’t difficult, but it may take a few more steps than you would want to carry out on a smartphone.

Rather than moving the goal post, let’s end our exercise here and save the rest for ‘latte’ — er, save it for the next article.

--

--