Chapter 4
IN THIS CHAPTER
Understanding Google Colab
Accessing Google and Colab
Performing essential Colab tasks
Obtaining additional information
Colaboratory (https://colab.research.google.com/notebooks/welcome.ipynb
), or Colab for short, is a Google cloud-based service that replicates Jupyter Notebook in the cloud. You don’t have to install anything on your system to use it. In most respects, you use Colab as you would a desktop installation of Jupyter Notebook (also called Notebook throughout the book). This book includes this chapter mainly for those readers who use something other than a standard desktop setup to work through the examples.
To use Colab, you must have a Google account and then access Colab using your account. Otherwise, most of the Colab features won’t work. The next section of the chapter gets you started with Google and helps you access Colab.
As with Notebook, you can use Colab to perform specific tasks in a cell-oriented paradigm. The next sections of the chapter go through a range of task-related topics that start with the use of notebooks. If you’ve used Notebook in previous chapters, you notice a strong resemblance between Notebook and Colab. Of course, you also want to perform other sorts of tasks, such as creating various cell types and using them to create notebooks that look like those you create with Notebook.
Finally, this chapter can’t address every aspect of Colab, so the final section of the chapter serves as a handy resource for locating the most reliable information about Colab.
Google Colab is the cloud version of Notebook. In fact, the Welcome page makes this fact apparent. It even uses IPython (the previous name for Jupyter) Notebook (.ipynb
) files for the site. That’s right, you’re viewing a Notebook right there in your browser. 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 following sections help you understand the Colab differences.
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, just as you do with Notebook. The techniques you use are similar, in fact, to using Notebook, but later in the chapter, you find out the small differences between the two. Even so, the downloadable source for this book should run without much effort on your part.
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://help.github.com/articles/working-with-jupyter-notebook-files-on-github/
, your Notebook files will appear as static HTML pages when you use a GitHub repository. In fact, some features won’t work at all. Colab enables you to fully interact with your Notebook files using GitHub as a repository. In fact, Colab supports a number of online storage options, 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.
For the most part, you use Colab just as you would Notebook. However, some features work differently. For example, to execute the code within a cell, you select that cell and click the run button (right-facing arrow) for that cell. The current cell remains selected, which means that you must actually initiate the selection of the next cell as a separate action. A block next to the output lets you clear just that output without affecting any other cell. Hovering the mouse over the block tells you when someone executed the content. On the right side of the cell, you see a vertical ellipsis that you can click to see a menu of options for that cell. The result is the same as when using Notebook, but the process for achieving the result is different.
If you use Chrome when working with Colab and choose to sync your copy of Chrome among various devices, all your code becomes available on any device you choose to work with. Syncing transfers your choices to all your devices as long as those devices are also set to synchronize their settings. Consequently, you can write code on your desktop, test it on your tablet, and then review it on your smart phone. It’s all the same code, all the same repository, and the same Chrome setup, just a different device.
What you may find, however, is that all this flexibility comes at the price of speed and ergonomics. In reviewing the various options, a local copy of Notebook executes the code in this book faster than a copy of Colab using any of the available configurations (even when working with a local copy of the .ipynb
file). So, you trade speed for flexibility when working with Colab. In addition, viewing the source code on a tablet is hard; viewing it on a smart phone is nearly impossible. If you make the text large enough to see, you can’t see enough of the code to make any sort of reasonable editing possible. At best, you could review the code one line at a time to determine how it works.
One thing to consider when using Notebook and Colab is that the two products use most of the same terminology and many of the same features, but they’re not completely the same. The methods used to perform tasks differ, and some of the terminology does as well. For example, a Markdown cell in Notebook is a Text cell in Colab. The “Performing Common Tasks” section of this chapter tells you about other differences you need to consider.
The only time you really need local runtime support is when you want to work within a team environment and you need the speed or resource access advantage offered by a local runtime. Using a local runtime normally produces better speed than you obtain when relying on the cloud. In addition, a local runtime enables you to access files on your machine. A local runtime also gives you control over the version of Notebook used to execute code. You can read more about local runtime support at https://research.google.com/colaboratory/local-runtimes.html
.
The most important consideration when using a local runtime, however, is that your machine is now open to possible infection from Notebook code. You need to trust the party supplying the code. The local runtime option doesn’t open your machine to others that you share code with, however; they must either use their own local runtimes or rely on the cloud to execute code.
Before you can perform any significant tasks using Colab, you must have a Google account. You can use the same account for all sorts of purposes, not just development. For example, a Google account gives you access to Google Docs (https://www.google.com/docs/about/
), an online document management system similar to Office 365.
To create a Google account, simply navigate to https://account.google.com/
and click the Create Your Google Account link. This page also contains a wealth of information about what a Google account can provide. When you click Create Your Google Account, you see the page shown in Figure 4-1. The account creation process consumes several pages. You just provide the required information in each page and click Next.
After you create and verify your account, you can sign into it. Before you can use Colab effectively, you must sign into your account. That’s because Colab relies on your Google Drive for certain tasks. You can also store notebooks in other places, such as GitHub, but having your Google account ensures that everything works as planned. To sign into your account, navigate to https://accounts.google.com/
, provide your e-mail address and password, and then click Next. You see the sign-in page shown in Figure 4-2.
As with Jupyter Notebook, the notebook forms the basis of interactions with Colab. In fact, Colab is built on notebooks, as previously mentioned. When you place the mouse on certain parts of the Welcome page at https://colab.research.google.com/notebooks/welcome.ipynb
, you see opportunities for interacting with the page by adding either code or text entries (which you can use for notes as needed). These entries are active, so you can interact with them. You can also move cells around and copy the resulting material to your Google Drive. Of course, while interacting with the Welcome page is both unexpected and fun, the real purpose of this chapter is to demonstrate how to interact with Colab notebooks. The following sections describe how to perform basic notebook-related tasks with Colab.
To create a new notebook, choose File ⇒ New Python 3 Notebook. You see a new Python 3 notebook like the one shown in Figure 4-3. The new notebook looks similar to, but not precisely the same as, those found in Notebook. However, all the same functionality exists. You can also create a new Python 2 notebook if desired, but this book doesn’t rely on Python 2.
The notebook shown in Figure 4-3 lets you change the filename by clicking on it, just as you do when working in Notebook. Some features work differently but provide the same results. For example, to run the code in a particular cell, you click the right-pointing arrow on the left side of that cell. In contrast to Notebook, the cell focus doesn’t change to the next cell, so you must choose the next cell directly or by clicking the Next Cell or Previous Cell buttons on the toolbar.
You can open existing notebooks found in local storage, on Google Drive, or on GitHub. You can also open any of the Colab examples or upload files from sources that you can access, such as a network drive on your system. In all cases, you begin by choosing File ⇒ Open Notebook. You see the dialog box shown in Figure 4-4.
The default view shows all the files you opened recently, regardless of location. The files appear in alphabetical order. You can filter the number of items displayed by typing a string into the Filter Notebooks field. Across the top are other options for opening notebooks.
Google Drive is the default location for many operations in Colab, and you can always choose it as a destination. When working with Drive, you see a list of files similar to those shown in Figure 4-4. To open a particular file, you click its link in the dialog box. The file opens in the current tab of your browser.
When working with GitHub, you initially need to provide the location of the source code online, as shown in Figure 4-5. The location must point to a public project; you can’t use Colab to access private projects.
After you make the connection to GitHub, you see two lists: repositories, which are containers for code related to a particular project; and branches, a particular implementation of the code. Selecting a repository and branch displays a list of notebook files that you can load into Colab. Simply click the required link and it loads as if you were using a Google Drive.
If you want to use the downloadable source for this book, or any local source for that matter, you select the Upload tab of the dialog box. In the center is a single button, Choose File. Clicking this button opens the File Open dialog box for your browser. You locate the file you want to upload, just as you normally would for opening any file.
Colab provides a significant number of options for saving your notebook. However, none of these options works with your local drive. After you upload content from your local drive to Google Drive or GitHub, Colab manages the content in the cloud and not on your local drive. To save updates to your local drive, you must download the file using the techniques found in the “Downloading notebooks” section, later in this chapter. The following sections review the cloud-based options for saving notebooks.
The default location for storing your data is Google Drive. When you choose File ⇒ Save, the content you create goes to the root directory of your Google Drive. If you want to save the content to a different folder, you need to select that folder in Google Drive (https://drive.google.com/
).
You can also save a copy of your project by choosing File ⇒ Save a Copy In Drive. The copy receives the word Copy as part of its name. Of course, you can rename it later. Colab stores the copy in the current Google Drive folder.
GitHub provides an alternative to Google Drive for saving content. It offers an organized method of sharing code for the purpose of discussion, review, and distribution. You can find GitHub at https://github.com/
.
Notice that this account doesn’t currently have a repository. You must either create a new repository or choose an existing repository in which to store your data. After you save the file, it will appear in your GitHub repository of your choice. The repository will include a link to open the data in Colab by default, unless you choose not to include this feature.
You use GitHub Gists as a means of sharing single files or other resources with other people. Some people use them for full projects as well, but the idea is that you have a concept that you want to share — something that isn’t quite fully formed and doesn’t represent a usable application. You can read more about Gists at https://help.github.com/articles/about-gists/
.
As with GitHub, Gists come in both public and secret form. You can access both public and secret Gists from Colab, but Colab automatically keeps your files secret. To save your current project as a Gist, you choose File ⇒ Save a Copy as a GitHub Gist. Unlike GitHub, you don’t need to create a repository or do anything fancy in this case. The file saves as a Gist without any extra effort. The resulting entry always contains a View in Colaboratory link, as shown in Figure 4-8.
Colab supports two methods for downloading notebooks to your local drive: .ipynb
files (using File ⇒ Download .ipynb) and .py
files (using File ⇒ Download .py). In both cases, the file appears in the default download directory for your browser; Colab doesn’t offer a method for downloading the file to a specific directory.
Most tasks in Colab work similar to their Notebook counterparts. For example, you can create code cells just as you do in Notebook. Markdown cells come in three forms: text, heading, and table of contents. They work somewhat differently from the Markdown cells found in Notebook, but the idea is the same. You can also edit and move cells, just as you do with Notebook. One important difference is that you can’t change a cell type. A cell that you create as a header can’t suddenly transform into a code cell. The following sections provide a brief overview of the various features.
The first cell that Colab creates for you is a code cell. The code you create in Colab uses all the same features that you find in Notebook. However, off to the side of the cell, you see a menu of extras that you can use with Colab that aren’t present in Notebook, as shown in Figure 4-9.
You use the options shown in Figure 4-9 to augment your Colab code experience. The following list provides a short description of these features:
https://colab.research.google.com/notebooks/forms.ipynb
.Code cells also tell you about the code and its execution. The little icon next to the output displays information about the execution when you hover your mouse over it, as shown in Figure 4-10. Clicking the icon clears the output. You must run the code again to regenerate the output.
Text cells work much like Markup cells in Notebook. However, Figure 4-11 shows that you receive additional help in formatting the text using a graphical interface. The markup is the same, but you have the option of allowing the GUI to help you create the markup. For example, in this case, to create the # sign for a heading, you click the double T icon that appears first in the list. Clicking the double T icon again would increase the header level. To the right, you see how the text will appear in the notebook.
Notice the menu to the right of the text cell. This menu contains many of the same options that a code cell does. For example, you can create a list of links to help people access specific parts of your notebook through an index. Unlike Notebook, you can’t execute text cells to resolve the markup they contain.
The special cells that Colab provides are variations of the text cell. These special cells, which you access using the Insert menu option, make creating the required cells faster. The following sections describe each of these special cell types.
When you choose Insert ⇒ Section Header Cell, you see a new cell created below the currently selected cell that has the appropriate header level 1 entry in it. You can increase the heading level by clicking the double T icon. The GUI looks the same as the one in Figure 4-11, so you have all the standard formatting features for your text.
An interesting addition to Colab is the automatic generation of a table of contents for your notebook. To use this feature, choose Insert ⇒ Table of Contents Cell. Figure 4-12 shows the output for this particular example.
The Table of Contents cell contains a heading in the actual cell that you can modify in the same way as any other heading in your notebook. The table of contents appears in the cell output. To update the table of contents, click Refresh to the right of the cell output. You also have access to all the usual text cell features when working with a Table of Contents cell.
Both Colab and Notebook have Edit menus that contain the options you except, such as the ability to cut, copy, and paste cells. The two products have some interesting differences. For example, Notebook allows you to split and merge cells. Colab contains an option to show or hide the code as a toggle. These differences give each product a slightly different flavor but don’t really change your ability to use it to create and modify Python code.
The same technique you use for moving cells in Notebook also works with Colab. The only difference is that Colab relies exclusively on toolbar buttons, while Notebook also has cell movement options on the Edit menu.
Your Colab code executes on a Google server. All your computing device does is host a browser that displays the code and its results. Consequently, any special hardware on your computing device is ignored unless you choose to execute code locally.
The Notebook Settings dialog box also lets you choose whether to include cell output when saving the notebook. Given that you store your notebook in the cloud in most cases and that loading large files into your browser can be time consuming, this feature enables you to restart a session more quickly. Of course, the trade-off is that you must now regenerate all the outputs you need.
For your to be useful, you need to run it at some point. Previous sections have mentioned the right-pointing arrow that appears in the current cell. Clicking it runs just the current cell. Of course, you have other options than clicking the right-pointing arrow, and all these options appear on the Runtime menu. The following list summarizes these options:
A notebook has a right-pointing arrow in its left margin. Clicking this icon displays a pane containing tabs that show various kinds of information about your notebook. You can also choose specific pieces of information to see from the View menu. To close this pane, click the X in the upper-right corner of the pane. The following sections describe each of these pieces of information.
Choose View ⇒ Table of Contents to see a table of contents for your notebook, as shown in Figure 4-14. Clicking any of the entries takes you to that section of the notebook.
At the bottom of the pane is a + Section button. Click this button to create a new header cell below the currently selected cell.
When you choose View ⇒ Notebook Information, you see a pane open at the bottom of the browser, as shown in Figure 4-15. This pane contains the notebook size, settings, and owner. Notice that the display also tells you the maximum notebook size.
The Notebook Info tab also includes a Modify link (two of them). Both links display the Notebook Settings dialog box, in which you can choose the runtime type and whether the notebook relies on hardware acceleration, as described in the “Using Hardware Acceleration” section, earlier in this chapter.
Colab keeps track of your code as you execute it. Choose View ⇒ Executed Code History to display the Executed Code tab in the pane at the bottom of the window, as shown in Figure 4-16. Note that the number associated with the entries in the Executed Code tab may not match the numbers associated with the associated cells. In addition, each unique execution of code receives a separate number.
You can share your Colab notebooks in a number of ways. For example, you can save it to GitHub or GitHub Gists. However, the two most direct methods are the following:
In both cases, you click the Share button in the upper right of the Colab window. The Share with Others dialog box opens (see Figure 4-17).
When you enter one or more names in the People field, an additional field opens in which to add a sharing message. You can type a message and click Send to send the link immediately. If you click Advanced instead, you see another dialog box, where you can define how to share the notebook.
In the upper-right corner of the Share with Others dialog box, you see the Get Shareable Link button. Click this button to display a dialog box containing the link for your notebook. Clicking Copy Link places the URL on the Clipboard for your device, and you can paste it into messages or other forms of communication with others.
The most obvious place to obtain help with Colab is from the Colab Help menu. This menu contains all the usual entries for accessing frequently asked questions (FAQs) pages. The menu doesn’t have a link to general help, but you can find general help at https://colab.research.google.com/notebooks/welcome.ipynb
(which requires you to log into the Colab site). The menu also provides options for submitting a bug and sending feedback.
One of the more intriguing Help menu entries is Search Code Snippets. This option opens the pane shown in Figure 4-18, in which you can search for example code that could meet your needs with a little modification. Clicking the Insert button inserts the code at the current cursor location in the cell that has focus. Each of the entries also shows an example of the code.
Colab enjoys strong community support. Choosing Help ⇒ Ask a Question on Stack Overflow displays a new browser tab, where you can ask questions from other users. You see a login screen if you haven’t already logged into Stack Overflow.
3.147.47.59