Manipulating Notebook Cells for Python Programming

By John Paul Mueller

Cells are what make Notebook considerably different from using other IDEs. By using the functionality that cells provide, you can perform all sorts of application manipulations that would otherwise be difficult or error prone using other IDEs, such as moving related code around as a chunk, rather than line-by-line.

Adding various cell types

Notebook gives you access to several different cell types. Here’s a rundown of all the types that you can use with Notebook:

  • Code: Contains interpreted Python code that provides an input and an output area.
  • Markdown: Displays special documentation text using the GitHub markup technique. Markdown cells areoften used for headings, but you can include all sorts of information in cells of this type.
  • Raw NBConvert: Provides a method for including uninterpreted content within a notebook that affects certain kinds of downloaded output, such as LaTeX.
  • Heading (obsolete): This is an older method of creating headings that you shouldn’t use any longer.

Whenever you execute the content of a cell using the Run Cell button or by choosing Cell → Run Cells and Select Below, and the insertion point is on the last cell, Notebook automatically adds a new cell for you. However, this isn’t the only way to add a new cell. For example, you might want to add a new cell in the middle of the notebook. To perform this task, you choose Insert → Insert Cell Above or Insert → Insert Cell Below, depending on whether you want to insert a cell above or below the current cell.

Splitting and merging cells

Notebook treats cells as distinct entities. Whatever you do in a cell will affect the application as a whole. However, you can execute or manipulate that individual cell without changing any other cell. In addition, you can execute cells in any order and execute some cells more often than you do others. That’s why you need to focus on cell construction: You need to determine whether a cell is independent enough, yet complete enough, to perform a desired task. With this in mind, you may find it necessary to split and merge cells.

Splitting a cell means to create two cells from an existing cell. The split occurs at the current cursor location within the cell. To perform the split, you choose Edit → Split Cell.

Merging a cell means to create a single cell from two existing cells. The cells are merged in the order in which they appear in the notebook, so you must be sure that the cells are in the correct order before you merge them. To perform the merge, you choose Edit → Merge Cell Above or Edit → Merge Cell Below.

Moving cells around

Cells need not stay in the order in which you originally create them. You may find that you need to perform a particular task sooner or later in the process. The simplest way to move a cell is to select the cell and then choose Edit → Move Cell Up or Edit → Move Cell Down. However, these two commands simply move the cells. You might decide to do something completely different.

As do most editors, Notebook comes with a full selection of editing commands. All these commands appear on the Edit menu. Following are editing commands that Notebook provides in addition to the standard movement commands:

  • Cut Cells: Removes the selected cell but places it on the Clipboard for later reuse.
  • Copy Cells: Places a copy of the selected cell on the Clipboard without removing it.
  • Paste Cells Above: Inserts a copy of the cell that appears on the Clipboard above the selected cell.
  • Paste Cells Below: Inserts a copy of the cell that appears on the Clipboard below the selected cell.
  • Delete Cells: Removes the selected cell without creating any copy.
  • Undo Delete Cells: Adds a deleted cell back onto the notebook (there is only one level of undelete, so you must perform this task immediately after a deletion).

Running cells

To see the result of interpreting a cell, even those markdown cells, you need to run the cell. The most common way to perform this task is to click the Run Cell button on the toolbar (the one with the right pointing arrow). However, you might decide that you don’t want to run your cells in the default manner. In this case, you have a number of options to choose from on the Cell menu:

  • Run Cells: Runs the selected cell while maintaining the current selection.
  • Run Cells and Select Below (default): Runs the selected cell and then selects the cell below it. If this is the last cell, Notebook creates a new cell to select.
  • Run Cells and Insert Below: Runs the selected cell and then inserts a new cell below it. This is a good option to choose if you’re adding new cells in the middle of an application because you get a new cell regardless of whether this is the last cell.
  • Run All: Starts at the top and runs every cell in the notebook. When Notebook reaches the bottom, it selects the last cell, but doesn’t insert a new one.
  • Run All Above: Starts from the current cell and executes all the cells above it in reverse order. You won’t find this option in most other IDEs!
  • Run All Below: Starts from the current cell and executes all the cells below it in order. When Notebook reaches the bottom, it selects the last cell but doesn’t insert a new one.

Toggling outputs

Sometimes seeing the output from a cell is helpful, but in other cases, the output just gets in the way. In addition, situations that call for starting with a clean output can arise, so you may want to clear the old information. The Cell → Current Outputs menu has options that affect just the selected cell, and the Cell → All Output menu has options that affect all the cells in the notebook. Here are the options you have for controlling output:

  • Toggle: Turns the output on or off, based on a previous condition. The output is still present in its entirety.
  • Toggle Scrolling: Reduces the size of long outputs to just the default number of lines. In this way, you can see enough information to know how the cell worked, but not get every detail.
  • Clear: Removes the current output. You must rerun the cell in order to generate new output after using this option.