Hosting Python Notebooks on Google Colab and Binder

July 15, 2020 ☼ PythonGuidesProgramming

Python notebooks (e.g., Jupyter Notebooks) are a staple of scientific computing and are increasingly an important pedagogical tool. The key reason why Python notebooks are a powerful education medium is that they combine running and editing raw Python code alongside full-textual comments, enabling educators to include pedagogical comments within a fully functional Python environment. However, while Python notebooks are a great medium, there is a small activation barrier before students could traditionally get started: downloading and installing Jupyter Notebooks (often through the Anaconda distribution). While this is usually not an obstacle for students versed in programming, it may present an unnecessary hurdle to get students started or for small illustrative examples. Instead, you can host and serve these notebooks using online services to facilitate easy access to the fully interactive environment. Let’s get started!

Table of Contents

  1. Introducing the Online Approach
  2. Serving with Binder
  3. Serving with Google Colab
  4. Closing

Introducing the Online Approach

Instead of distributing Python notebooks to students and asking them to install Jupyter Notebooks locally, you can host and serve them online. This workflow facilitates a one-click access to an interactive copy that they can edit (temporary changes that don’t affect the source file) and download the files for further modifications, reducing the friction for students to start leveraging the interactive nature of the notebook environment. There are two primary methods for hosting and serving your notebooks online: Google Colab and Binder. In this guide, I’ll introduce a workflow to utilize these services for your educational content. For both services, I recommend hosting the files online in a git repository, such as through GitHub or GitLab. If you are unfamiliar with GitHub, you should walk through one of the many great online tutorials (such as this one) before working through the rest of this guide.

Serving With Binder

Binder is an online service that transforms a Git repository into a cloud-hosted JuypterHub (server-based Jupyter Notebooks application) instance. Using this service provides a native Jupyer Notebooks interface with interactive content accessible through a single click through a URL.

Workflow

  1. Upload your notebooks to an online Git repository.
  2. Setup a file to specify the dependencies.
    • This can either be a requirements.txt (equivalent to a pip install setup, see documentation) or an environment.yml (equivalent to an Anaconda setup, see documentation).

    Example of an environment.yml from the repository for this blog.Example of an environment.yml from the repository for this blog.

  3. Fill in the repository, branch, and path to the notebook (optional) on mybinder.org.

    Example of completed Binder setup.Example of completed Binder setup.

  4. Launch the Binder instance to generate the initial cache of your repository.
  5. Access the instance using the URL provided on the page. The landing page (which is what students will first see) will show the current status until it is ready.

    Example of Binder landing page.Example of Binder landing page.

  6. Launch each notebook to cache their setup to reduce launch time for students
  7. Link to specific notebooks by modifying the file path to point to the notebook
    • https://mybinder.org/v2/gh/{username}/{repository}/{branch}/{path-to-notebook}

Advantages and Disadvantages

Using Binder comes with a set of advantages and disadvantages that you should be aware of:

Advantages

Disadvantages

Serving with Google Colab

Google Colab is a browser-based Python notebook programming environment provided by Google. While the functionality is similar to Binder and the traditional Jupyter Notebook, Google Colab utilizes a unique user interface. This service integrates with Google Drive and can also serve Python notebooks hosted elsewhere. I’ll introduce two ways to use Google Colab: 1) Hosting files directly in Google Drive, 2) Hosting files on GitHub.

Installing Google Colab

Prior to utilizing Google Colab, students will need to install the plugin for their Google Drive account. Installation is an easy process:

Workflow (Google Drive Hosting)

Hosting your Python notebooks on Google Drive to use with Google Colab is similar to managing shared Google Docs, Sheets, or Slides. While this method seems less complicated than other methods, there are additional steps surrounding sharing and editing settings that may lead to problems.

  1. Upload the Python notebook into your Google Drive (ideally in an organized folder, but not required)
  2. Distribute notebooks by sharing individual links or adding students to the specific folder. Make sure not to give students edit access, or the source will be modified.

Workflow (GitHub Hosting)

To get around potential sharing permission problems inherent in hosting notebooks directly on Google Drive, you can host the notebooks on an external source, such as GitHub, and serve them through the Google Colab interface. The original integration documentation can be found here. Unlike hosting the files directly in Google Drive, there are no risks of editing the source.

  1. Upload notebooks to a GitHub repository (this can be the same as the Binder repository I introduced earlier)
  2. Generate the Open in Colab” URL
    • Use the Open in Colab Extension when the notebook is opened in GitHub
    • Modify the general URL pattern:
      • https://colab.research.google.com/github/{GitHub Account}/{repository}/blob/{branch}/{pathname}

    Example of generating the Colab URL using the Chrome ExtensionExample of generating the Colab URL using the Chrome Extension

  3. Share the final link with students to give them access

Advantages and Disadvantages

Just like the Binder workflow, utilizing Google Colab comes with advantages and disadvantages.

Advantages

Disadvantages

Closing

Python notebooks are useful pedagogical tools for teaching programming or providing students interactive environments to explore mathematical models. However, depending on the nature of the content and the experience level of your students, requiring them to install the necessary components to work with these files locally on their computer may not be the best option. Using Binder or Google Colab lets you host and serve Python notebooks entirely online without requiring a local installation. While setting up this workflow may require more work than distributing the raw files directly, I believe this dramatically improves the accessibility for non-programmers or quick, interactive demonstrations. I hope you have found this guide helpful for setting up your online workflow!