Two of the central building blocks of the EarthAI Notebook environment are Jupyter Notebooks and the Python programming language. In this article we cover the basics of working with Jupyter Notebooks.
Notebooks and JupyterLab Overview
Jupyter Notebooks provide a web-based coding environment that allows you to not only execute code, but also to communicate the narrative around the code development and execution process as well as more clearly visualize the results of the executed code. Notebooks were developed with the spirit of traditional laboratory notebooks in mind where the researcher (or programmer) documents their experimental design, results, and conclusions in a way that can be communicated to others.
EarthAI Notebook is built on top of the widely-used JupyterLab environment. The general layout of the EarthAI environment is that the left-hand side contains a file browser, list of running kernels, notebook tools, and a Git interface. The main center workspace is where the active notebooks are displayed.
Notebooks have the file extension ".ipynb". If multiple notebooks are open they can be selected by tabs at the top of the main work area. Just below the tabs is a menu bar with buttons to save the notebook, along with various actions for the notebook cells.
Top Menu Bar
There are a handful of useful notebook tools in the top menu bar.
Save the notebook contents and create checkpoint:
Insert a cell below:
Cut the selected cells:
Copy the selected cells:
Paste cells from the clipboard:
Run the selected cells and advance:
Interrupt the kernel:
Restart the kernel:
Restart the kernel, then re-run the whole notebook:
Select the cell type:
Notebook Cell Types
Notebooks cells are how you interact with and carry out work within a notebook. There are three types of cells: code, markdown, and raw.
Code cells are the primary type of notebook cells, and are selected by default when you add a new cell to a notebook. As the name suggests this is where Python code can be written and executed. For example, the cell below evaluates the expression
1+1. Executing a cell is carried out by Shift+Enter or by clicking the Run arrow on the menu bar. When executed, the result of the evaluation
2 is printed just below the cell:
Code cells differ from markdown and raw cells in that they are numbered. Code cells are numbered by the order in which they are executed, not by their location in the notebook. Indeed, Jupyter Notebooks are designed to be fluid with modifications as the exploration and analysis proceeds. The cell numbers allow you to keep track of the order in which different pieces of code were executed, regardless of where the cell is placed within the notebook.
Code cells can be easily edited after they have been run by clicking within the cell, changing the expression, and executing again. While a code cell is still running, you will see a
* in the indicator brackets.
If you need to stop a code cell from executing there is an Interrupt the kernel button on the top menu bar.
Markdown cells allow you to generate rich text via the Markdown language. Markdown allows you to add rich features to your text, such as bold, italics, code, and section headers using a lightweight markup language. If you are new to Markdown, there are a number of free and open-source reference guides online to help you get started.
You can create a Markdown cell by selecting Markdown from the drop-down menu on the menu bar above the notebook work area. After you execute a Markdown cell, the rich text features (bold, italics, etc.) show up as formatted text. To edit, simply double-click on the text to return to the markup view of the cell.
For example, the following Markdown language:
# Heading 1
## Sub-heading 1
* Bullet 1
* *Bullet 2*
* **Bullet 3**
## Sub-heading 2
1. Numbered item 1
1. ~Numbered item 2~
1. `Numbered item 3`
gets rendered in a notebook as:
- Bullet 1
- Bullet 2
- Bullet 3
- Numbered item 1
- ~Numbered item 2~
Numbered item 3
The final type of cell is a raw cell. Text or code in this type of cell is not evaluated by the notebook kernel. It can be used to render code formats in HTML or LaTex. The contents of a raw cell are displayed in plain text when the cell is executed:
This is an example of plain text output from a raw cell in notebook.