Customizing keyboard shortcuts
If you don’t like the default keyboard shortcuts, you can customize them to match your needs. To do so, choose Tools ⇒ Keyboard Shortcuts, and you see the Keyboard Preferences dialog box, shown in Figure 3-3. If you see Set Shortcut, it means that the command doesn’t currently have a shortcut, so you can add one if desired. Here’s how you work with shortcuts:
To add or change a shortcut, place the cursor in the box next to the command and press the shortcut key you want to use for that command.
To remove a shortcut, press Delete.
FIGURE 3-3: Customize shortcut keys for speed of access to commands.
Comparing files
Sometimes you need to compare two files to see how they differ. When you select Tools ⇒ Diff Notebooks, Colab opens a new browser tab and shows you two notebooks side by side, as shown in Figure 3-4. These are files selected by random from your Google Drive. To select the files you actually want to work with, click the down arrow next to the file path in each pane. The differences appear onscreen.
FIGURE 3-4: Colab lets you compare two files to see how they differ.
Working with Notebooks
The notebook forms the basis for 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, although 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.
Creating a new notebook
To create a new notebook, choose File ⇒ New Notebook. You see a new Python 3 notebook like the one shown in Figure 3-5. (The latest version of Colab doesn’t provide support for Python 2, so you can’t use Python 2 with it.)
FIGURE 3-5: Create a new Python 3 Notebook.
The notebook shown in Figure 3-5 lets you change the filename by clicking it. To run the code in a particular cell, you click the right-pointing arrow on the left side of that cell. After you run the code, you must choose the next cell directly.
Opening existing notebooks
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 3-6.
FIGURE 3-6: Use this dialog box to open existing notebooks.
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.
Even if you’re not logged in, you can still access the Colab example projects. These projects help you understand Colab but won’t allow you to do anything with your own projects. Even so, you can still experiment with Colab without logging into Google first. The following sections tell you more details about these options.
Using Google Drive for existing notebooks
Google Drive is the default location for many operations in Colab, and you can always choose it as a destination. When working with Google Drive, you see a list of files similar to those shown in Figure 3-6. To open a particular file, you click its link in the dialog box. The file opens in the current tab of your browser.
Using GitHub for existing notebooks
When working with GitHub, you initially need to provide the location of the source code online, as shown in Figure 3-7. The location must point to a public project; you can’t use Colab to access private projects.
FIGURE 3-7: When using GitHub, you must provide the location of the source code.
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 (https://drive.google.com/
), which is another type of online storage.
Using local storage for existing notebooks
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.
Selecting a file and clicking Open uploads the file to Google Drive. If you make changes to the file, those changes appear on Google Drive, not on your local drive. Depending on your browser, you usually see a new window open with the code loaded. However, you could also simply see a success message, in which case you must now open the file using the same technique as you would when using Google Drive. In some cases, your browser asks whether you want to leave the current page. You should tell the browser to do so. The File ⇒ Upload Notebook command also uploads a file to Google Drive. In fact, uploading a notebook works like uploading any other kind of file, and you see the same dialog box. If you want to upload other kinds of files, using the File ⇒ Upload Notebook command is likely faster.
Saving notebooks