Book Image

Learning Jupyter 5 - Second Edition

Book Image

Learning Jupyter 5 - Second Edition

Overview of this book

The Jupyter Notebook allows you to create and share documents that contain live code, equations, visualizations, and explanatory text. The Jupyter Notebook system is extensively used in domains such as data cleaning and transformation, numerical simulation, statistical modeling, and machine learning. Learning Jupyter 5 will help you get to grips with interactive computing using real-world examples. The book starts with a detailed overview of the Jupyter Notebook system and its installation in different environments. Next, you will learn to integrate the Jupyter system with different programming languages such as R, Python, Java, JavaScript, and Julia, and explore various versions and packages that are compatible with the Notebook system. Moving ahead, you will master interactive widgets and namespaces and work with Jupyter in a multi-user mode. By the end of this book, you will have used Jupyter with a big dataset and be able to apply all the functionalities you’ve explored throughout the book. You will also have learned all about the Jupyter Notebook and be able to start performing data transformation, numerical simulation, and data visualization.
Table of Contents (18 chapters)
Title Page
Packt Upsell
Contributors
Preface
Index

Basic Notebook operations


In this section, we will describe the different operations that you can perform on your Jupyter Notebook. Most of the operations are menu functions that will change your display accordingly.

File operations

Let's walk through the basic file operations.

From the Files tab, we can see a list of files and folders in the current Notebook/disk folder. If we select (check) one of the files, we will see the top-left menu change:

We now have choices of Duplicate, Rename, and delete (the trash icon). Note that the number of files selected, 1, is displayed in the box as well.

Duplicate

If we hit the Duplicate button, we get a confirmation prompt with the name of the file that's been selected for duplication:

Cancelwill close the dialog.Duplicatewill create another copy of the file with an appended copy number, as shown in the following screenshot. The original filename has been used with the addition of-Copynin the filename, wherenis the copy number. Note the original file extension,.py, has been maintained in the new file:

 

Rename

Similarly, if we hit the Rename button, another dialog box will appear to prompt the new filename to apply. The main filename has been highlighted as it assumes you want to maintain the file extension as the file type has not changed:

Delete

We can also delete the file by clicking on the trashcan icon. This brings up a confirmation dialog box as follows. I like that they changed the background of Delete to red to make sure that you don't just happily click it:

At the top right of the screen, we have options for Upload and New.

 

Upload

The Upload button is more meaningful when the Notebook is stored on a web server. When running it on your desktop, it allows you to move files easily from one part of your Notebook to another. If you click this button, you are presented with a file selector dialog box. The following screenshot is specific to a Windows environment, but a similar display is presented on macOS. Once you select a file, it will be added to your Notebook space:

 

New text file

If we opt to createText File, we are presented with a new browser panel in the Jupyter text editor (I have shrunk down the size of the screen so that the display fits the boundaries of this book):

There are several points of interest on this screen:

  • We are in a new browser panel (the Notebook display is still present in the Other tab).
  • The name of the new file is untitled1.txt. Using the same convention as duplication, the new filename starts with untitled.txt and is incremented as needed.
  • Curiously, it mentions when the file was created.
  • In the top-right corner, we see Plain Text. So, we might expect to see some other description here for other file types.
  • We have a new menu, which includes File, Edit, View, and Language.
  • TheFilemenu has the following options:
    • New: Starts another new text window.
    • Save: Save or updates the current text file into the Notebook area.
    • Rename: Changes the name of the file (unlikely, as you would want to keep theuntitlednname that's provided).
    • Download: Again, an option that makes more sense if your Notebook is running on the web. As explained for upload, downloadon a desktop installation allows you to copy a file to another part of your machine.

 

  • The Edit menu has the following options:
    • Find: Searches for a string.
    • Find & Replace: Searches and replaces a string.
    • Separator: Below this line is adjusting the text editor in use.
    • Key Map: Set your own function mapping for your keyboard.
    • Default: Checked as it is the default choice. This means using the default text editor.
    • Sublime Text: If you would prefer to use the Sublime editor.
    • Vim: If you would prefer to use VIM.
    • emacs: If you would prefer to use emacs.
  • The View menu only has an option to Toggle Line Numbers. I imagine future revisions of the package will have additional features. Similarly, for other file types, the menu may change.
  • The Language menu allows you to specify whether this text file is a specific type of programming file. This allows for syntax highlighting, which is a major feature of source editors. The list is extensive:

New folder

The Folder option creates a new folder with the naming convention untitledfolder.

New Python 3

The new Python 3 option creates a new Python 3 Notebook. You are presented with a new browser panel with a similar naming convention, as shown in the following screenshot.

This is a very different presentation, where Python code is expected to be entered in the cells on the page with results displayed in each cell.

There is an extensive menu with File, Edit, View, Insert, Cell, Kernel, and Help options. We have a fairly complete IntegratedDevelopmentEnvironment (IDE) for creating Python coding:

The File menu has the following options:

  • New Notebook: Starts a new Notebook (another browser panel like this one)
  • Open...: Selects a file to open from the Notebook files view
  • Make a Copy...: Copies the current Notebook completely into another browser panel
  • Rename...: Renames the current Notebook
  • Save and Checkpoint: Saves the current Notebook and records a checkpoint

Note

A checkpoint is a point in time where all information about a Notebook is preserved. You can have many checkpoints and return the state of your Notebook to the previous checkpoint state at any time. This is an excellent way to give yourself the room to try out a new angle on your analysis without risking losing what you have done so far.

  • Revert to Checkpoint: Reverts your Notebook to a previous checkpoint
  • Print Preview: Presents a preview of the printed form of your Notebook
  • Download as: Downloads the Notebook in a variety of formats:
    • IPython Notebook (its current form)
    • IPython
    • HTML representation
    • Markdown a specialized display format
    • REST – Restructured Text, which is an easy to read, plain text markup
    • PDF
    • Presentation
  • Close andHalt: Closes the current Notebook and stops any running scripts

Note

Each of the rectangular work areas in your Notebook is a cell. The innermost text area is where you enter code. Below that (but within the surrounding rectangle), the results of each code stop will be displayed.

  • The Edit menu has the following options:
    • Copy Cells: Copies cells from the clipboard to the current cursor position.
    • Paste Cells Above: Pastes cells from the clipboard above the current cell.
    • Paste Cells Below: Pastes cells from the clipboard below the current cell.
    • Paste Cells & Replace: Pastes the cells from the clipboard on top of the current cell.
    • Delete Cells: Deletes the current cells.
    • Undo Delete Cells: Reverts the last delete cells invocation.
    • Split Cell: Splits up a cell from the current cursor position.
    • Merge Cell Above: Merges the current cell with the one above.
    • Merge Cell Below: Merges the current cell with the one below.
    • Edit Notebook Metadata: Every Notebook has underlying metadata which describes the characteristics of the Notebook. Advanced users can manipulate this data directly in order to adjust features more readily. For example, the current Notebook metadata looks like the following screenshot:
  • Find and Replace: Allows for find and replace among the selected cells. There is a standardized dialog box for this, as shown in the following screenshot:
  • As seen in the preceding screenshot, the parameters and their functions are as follows:
    • The Aa icon toggle determines whether a case-insensitive search is made
    • The * icon toggle determines whether a regex search is made
    • The stacked lines icon toggle determines whether a replace will be made
    • The Find text block presents the search criteria
    • The Replace text block is used for the replacement text
  • The View menu has the following options:
    • Toggle Header: Toggles the display of the Jupyter logo and filename
    • Toggle Toolbar: Toggles the display of the toolbar
    • Cell Toolbar: Toggles the display of the cell action icons
  • The Insert menu has the following options:
    • Insert Cell Above: Adds a new cell above the current one
    • Insert Cell Below: Adds a new cell below the current one
  • The Cell menu has the following options:
    • Run Cells: Runs the selected (or all) cells.
    • Run Cells and Select Below: Runs the current cells down and creates a new one below.
    • Run Cells and Insert Below: Runs the current cells and creates a new one above.
    • Run All: Runs all cells.
    • Run All Above: Runs all cells prior to the current cell.
    • Run All Below: Runs all cells below the current cell.
    • Cell Type: Changes the type of cell selected to Code, Markdown, or NBConvert. There is an automatic message that is displayed, noting that all cells are, by default, Code type.
    • Current Outputs and All Output have options to toggle their display.
  • The Kernel menu has the following options:
    • Interrupt: Send a keyboard interrupt, Ctrl + C, to the kernel. This is useful if your code is in an endless loop.
    • Restart: Restart the kernel.
    • Restart & Clear Output: Restart the kernel and clear all output anew.
    • Restart & Run All: Restart the kernel and run all cells.
    • Reconnect: Connect back to a remote Notebook.
    • Change kernel: Not useful as only Python 2 is available at this point.
  • The Help menu has the following options:
    • User Interface Tour: Walks the user through a UI tour
    • Keyboard Shortcuts: Presents a list of built-in keyboard shortcuts
    • Notebook Help: Presents help topics on the Notebook
    • Markdown: Description of the markdown available within a Notebook
    • Python Reference, IPython Reference, NumPy Reference, SciPy Reference, Matplotlib Reference, SymPy Reference, Pandas Reference: Help topics on the various languages and packages that can be used in Notebooks
    • About: A standard about box

There is an icon panel below the menu that has shortcut icons for the preceding functions:

  • Floppy disk icon: Save and Checkpoint.
  • Plus sign: Insert cell below.
  • Scissors: Cut selected cells.
  • Duplicate pages: Copy selected cells.
  • Up arrow: Move selected cells up.
  • Down arrow: Move selected cells down.
  • An icon that looks like a speaker: Run the current cell.
  • Black square: Interrupt the kernel.
  • Circular arrow: Restart the kernel (with dialog).
  • A drop-down menu for display characteristics:
    • Code
    • Markdown
    • RawNBConvert
    • Heading
  • Keyboard: Open the command palette.
  • Change the current toolbar in use. Clicking on the Cell Toolbar button auto-displays the Cell Toolbar choice from the View menu.