Algorithms For Dummies. John Paul Mueller
Чтение книги онлайн.
Читать онлайн книгу Algorithms For Dummies - John Paul Mueller страница 23
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
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 to your local drive. The following sections review the cloud-based options for saving notebooks.
Using Drive to save 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/
).
Colab tracks the versions of your project as you perform saves. However, as these revisions age, Colab removes them. To save a version that won’t age, you use the File ⇒ Save and Pin Revision command. To see the revisions for your project, choose File ⇒ Revision History.
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.
Using