You don’t have to type the source code for this chapter manually. In fact, using the downloadable source is a lot easier. You can find the source for this chapter in the
\A4D2E\
A4D2E; 03; Colab Examples.ipynb file of the downloadable source. See the Introduction for details on how to find these source files.
Defining Google Colab
Colab is designed to mimic a desktop application called Jupyter Notebook (https://jupyter.org/
). In fact, it's somewhat difficult to tell the two applications apart in the functionality they provide. Google Colab is the cloud version of Notebook, and the Welcome page makes this fact apparent. It even uses IPython (the previous name for Jupyter) Notebook (.ipynb
) files for the site.
Even though the two applications are similar and they both use .ipynb
files, they do have some differences that you need to know about. The previous edition of this book used Jupyter Notebook, but Colab offers the ability to compute anywhere on any device that sports a browser, so this edition of the book focuses on Colab instead. The following sections help you understand the Colab differences.
Understanding what Google Colab does
You can use Colab to perform many tasks, but for the purpose of this book, you use it to write and run code, create its associated documentation, and display graphics. The downloadable source for this book is designed to run on Colab, but you can also use it with Jupyter Notebook if you want.
Jupyter Notebook is a localized application in that you use local resources with it. You could potentially use other sources, but doing so could prove inconvenient or impossible in some cases. For example, according to https://docs.github.com/repositories/working-with-files/using-files/working-with-non-code-files
, your Notebook files will appear as static HTML pages when you use a GitHub repository (GitHub is a cloud-based storage technology specifically oriented to working with code.) In fact, some features won't work at all. Colab enables you to fully interact with your Notebook files using GitHub as a repository, and Colab supports a number of other online storage options as well, so you can regard Colab as your online partner in creating Python code.
The other reason that you really need to know about Colab is that you can use it with your alternative device. During the writing process, some of the example code was tested on an Android-based tablet (an ASUS ZenPad 3S 10). The target tablet has Chrome installed and executes the code well enough to follow the examples. All this said, you likely won’t want to try to write code using a tablet of that size — the text was incredibly small, for one thing, and the lack of a keyboard could be a problem, too. The point is that you don’t absolutely have to have a Windows, Linux, or OS X system to try the code, but the alternatives might not provide quite the performance you expect.
Google Colab generally doesn’t work with browsers other than Chrome (the browser used in this chapter), Firefox, or Safari (initial tests with Microsoft Edge have also been encouraging). In most cases, you see an error message, such as
This site may not work in your browser. Please use a supported browser
, and no other display if you try to start Colab in a browser that it doesn’t support. The included More Info link takes you to https://research.google.com/colaboratory/faq.html#browsers
, where you can learn more information.
SOME FIREFOX ODDITIES
Even with online help, you may still find that your copy of Firefox displays a SecurityError: The operation is insecure.
error message. The initial error dialog box points to some unrelated issue, such as cookies, but you see this error message when you click Details. Simply dismissing the dialog box by clicking OK makes Colab appear to be working because it displays your code, but you won't see results from running the code.
As a first step toward fixing this problem, make sure that your copy of Firefox is current; older versions don’t provide the required support. After you've updated your copy, setting the network.websocket.allowInsecureFromHTTPS
preference using About:Config
to True
should resolve the problem, but sometimes it doesn't. In this case, verify that Firefox actually does allow third-party cookies by selecting both the Always for the Accept Third Party Cookies and Site Data option and the Remember History option in the History section on the Privacy & Security tab of the Options dialog box. Restart Firefox after each change and then try Colab again. If none of these fixes works, you must use Chrome to work with Colab on your system.
Getting familiar with Google Colab features
Google Colab provides access to a number of features through the menu system. One of these features, hardware acceleration, appears in the “Using Hardware Acceleration” section, later in this chapter. The features in this section all appear on the Tools menu.
Locating commands
The Tools ⇒ Command Palette option displays a list of commands you can execute, as shown in Figure 3-1. Some of these commands also have shortcut keys, such as Ctrl+Alt+M for adding a comment to a cell. All these commands help you to perform tasks associated with Notebook content, such as adding forms.
FIGURE 3-1: Using Colab commands makes configuring your Notebook easy.
Configuring settings
The Tools ⇒ Settings option displays the Settings dialog box, shown in Figure 3-2. The four settings tabs perform these tasks:
Site: Configures how the site works. The most interesting setting is the theme. Selecting Adaptive lets Colab choose the interface colors based on lighting conditions. You can also configure display and access settings on this tab.
Editor: Determines how text appears onscreen and how the interface works. For example, you can set the key bindings to work like those in Vim (a text editor included on Unix and Linux systems, generally as the vi utility; see https://www.vim.org/
) if desired. You can also select font size, spaces for each level of indentation, and a plethora of other settings.
Colab Pro: Provides an advertisement for Colab Pro (https://colab.research.google.com/signup
), which gives you some significant benefits like faster GPUs, longer runtimes, and more memory — all of which let you get more work done in a shorter time.
Miscellaneous: Contains fun settings. You can choose from three visual effects: adding thunder and lightning using the Power Level setting; letting a Corgi run across the top of the display; and allowing a kitten to run across the top of the display. You can choose any mix of these visual effects.
FIGURE 3-2: The Settings dialog