This notebook is available in the ea-bootcamp-day-1 repository that you cloned to earth-analytics-bootcamp under your home directory.

Jupyter Notebook

In the lesson on Open Reproducible Science, you learned that Jupyter Notebook is a web-based interactive development environment (IDE) that allows you to:

Write and run code interactively

Share your work with colleagues so they can see both your code and the code outputs.

Document all aspects of your workflow using a combination of well written programming code (e.g. Python) and Markdown text.

This functionality supports open reproducible science by facilitating and supporting collaboration and documentation.

The components of Jupyter Notebook include:

The Jupyter Notebook IDE The Jupyter Notebook interactive development environment (IDE) is the web application that launches in a web browser like Firefox or Safari and is the environment where you write and run your code.

Notebook documents (.ipynb files) The notebook document is a file type that you can use to store your Python code and Markdown text. A Jupyter Notebook file has an .ipynb extension (e.g. jpalomino-homework-1.ipynb)

Kernels A kernel runs your code in a specific programming language. Jupyter Notebook supports over 40 different languages. In this class, you will use Python.

Jupyter Notebook also provides functionality for making the coding process more efficient such as keyboard short-cuts as well as auto-complete options using the Tab button. You will review this functionality in the hands-on activity.

Markdown

Markdown is a human readable syntax for formatting text documents and can be used to produce nicely formatted documents including .pdf files and web pages (e.g. .html files). When you format text using Markdown in a document, it is similar to using the formatting tools (bold, heading 1, heading 2, etc) in a word processing tool like Microsoft Word or Google Docs.

However, instead of using buttons to apply formatting, you use syntax such as **this syntax bolds text in Markdown** or # Here is a heading in Markdown. Markdown allows you to format text - such as making headings, italics, bold, and bulleted lists, - add hyperlinks to websites (e.g. URLs), make tables, and more.

In fact, this webpage that you are looking at right now (and all of the hands-on exercises in this course) use Markdown for formatting!

Using Markdown within Jupyter Notebook provides an easy and visually-appealing way to document your code’s workflow, provide details on your data, and describe your output and results within a single Jupyter Notebook file (.ipynb).

How Jupyter supports open reproducible science through combining programming code, displaying output, and documentation with Markdown.

Launch Jupyter Notebook From the Terminal

In this lesson, you will review how to use Jupyter Notebook and the functionality that it provides for documenting open reproducible science. First, you need to know how to open Jupyter Notebook, which is done in the Terminal.

Use Bash to Change to Your Desired Working Directory

It is ideal (but not required) to launch Jupyter Notebook from the working directory where all of the notebook files that you wish to use live. In this case, these files are in the earth-analytics-bootcamp directory.

To launch Jupyter Notebook, you should always do the following:

change the current directory to your desired working directory. In this case: ~/earth-analytics-bootcamp

run the command jupyter notebook in the Terminal to launch Jupyter Notebook.

IMPORTANT: the jupyter notebook command requires that you have Jupyter Notebook installed on your laptop! This was installed as part of the Anaconda installation that you followed as a part of the setup for this course.

First, change the current working directory to earth-analytics-bootcamp under your home directory, and then check that the current working directory has been updated.

Begin a Jupyter Notebook Session From the Terminal

Now, start a new Jupyter Notebook session by typing the command jupyter notebook in the Terminal.

$ jupyter notebook

You can use Shell (Terminal) to open Jupyter Notebook with the command, Jupyter Notebook.

When you type this command into the terminal, it will launch a local web server on your computer. This server runs the Jupyter Notebook interface. If everything works as planned, your default web browser will open with a new tab that displays your Jupyter Notebook environment.

This is what the Jupyter Notebook dashboard will look like when you launch it.

You will also notice that the Terminal is running commands to start your Jupyter Notebook session. Be sure to leave the Terminal open while you use Jupyter Notebook. It is running a local server for Jupyter Notebook so that you can interact with it in your web browser.

This is what the Terminal looks like with a Jupyter Notebook session running.

Data Tip: While Jupyter Notebook looks like an online interface, when you launch if from the terminal, like you did in this lesson, it is actually running locally on our computer. You do not need an internet connection to run Jupyter Notebook locally.

Navigate the Jupyter Notebook Dashboard

To navigate in the dashboard, you can simply click on the name of a directory (e.g. ea-bootcamp-day-1), and the dashboard will update to show you the contents of the directory.

You can click on the name of directory in the Jupyter Notebook Dashboard to navigate into that directory and see the contents.

You can return to the parent directory of your Jupyter Notebook session (i.e. the directory from which you launched Jupyter Notebook; in this example, earth-analytics-bootcamp) by clicking on the folder icon on the top menu bar.

You can use the Jupyter Notebook dashboard to return to the parent directory of any subdirectory.

Open Existing Jupyter Notebook Files

You can open existing Jupyter Notebook files (.ipynb) in the Jupyter Notebook dashboard.

Note: if you don’t see the Jupyter Notebook file (.ipynb) or directory that you are looking for, you may need to navigate to another directory in the dashboard (see above). You may also need to launch the Jupyter Notebook from a different directory.

Opening an existing Jupyter Notebook file (.ipynb) is as easy as clicking on the name of the file in the dashboard (e.g. filename.ipynb).

Understand the Structure of Jupyter Notebook Files

A Jupyter Notebook has three main parts, which are highlighted in the image below:

Menu

Toolbar

Cells

The components of a Jupyter Notebook file include the Menu, the Toolbar, and Cells just like the ones shown here.

You will review the menu and toolbar throughout this exercise. It is also important to understand how Jupyter Notebook files use cells to store and execute your code and text.

A Jupyter Notebook consists of a set of cells, which can be specified to store text such as Markdown or Code such as Python.

You can check the cell type of any cell by clicking in the cell and looking at the Cell Type in Toolbar.

You can check the cell type of any cell in Jupyter Notebook using the Toolbar. The default cell type is Code.

Work With Code and Markdown Cells

Run Existing Cells

You can run any cell in Jupyter Notebook (regardless of whether it contains Code or Markdown) using the Menu tools (Click on Cell -> Run Cell) or Keyboard Shortcuts (Ctrl, then Enter).

Function

Keyboard Shortcut

Menu Tools

Run Cell

Ctrl + enter

Cell → Run Cell

For example, you can run the Python code in the cell below using the Menu tools (Click on Cell -> Run Cell) or Keyboard Shortcuts (Ctrl, then Enter). Your result, or output, will be displayed below the Code cell that you run.

3+4

7

You can run Markdown cells in the same way as the Code cells using the Menu tools or Keyboard Shortcuts.

The difference between running a Code cell and a Markdown cell is that running a Markdown cell will not display results under the Markdown cell.

Rather, when you run Markdown cell, you will see that the Markdown syntax has been converted to nice formatting.

For example, the Markdown below is rendered from syntax for creating headings and titles using Markdown syntax: ## Text Follows the Hashmarks.

You can double-click in any Markdown cell to see the Markdown syntax. To return to the formatted Markdown (i.e. rendered text), simply run the cell again.

This is a subtitle in Markdown

This is a smaller subtitle

This is an even smaller subtitle

Create New Cells

You can also use the Menu tools and Keyboard Shortcuts to create new cells.

Function

Keyboard Shortcut

Menu Tools

Create new cell

Esc + a (above), Esc + b (below)

Insert→ Insert Cell Above OR Insert → Insert Cell Below

Copy Cell

c

Copy Key

Paste Cell

v

Paste Key

The default cell type is Code. You can change the cell type of any existing cell by clicking in the cell and selecting a new cell type (e.g. Markdown) in the cell type menu in the toolbar.

You can change the cell type of any cell in Jupyter Notebook using the Toolbar. The default cell type is Code.

Move Cells Within Jupyter Notebook

You can change the order of cells within Jupyter Notebook using the up arrow and down arrow buttons on the menu bar. Simply click inside the cell that you want to move and then press the desired arrow as many times as you need to move the Cell to the desired location.

You can use the menu bar in Jupyter Notebook to move cells within the Jupyter Notebook file.

Run All Cells in Jupyter Notebook

In addition to running individual cells within a Jupyter Notebook, you can also run all of the cells in consecutive order using the Menu.

Function

Menu Tools

Run all cells

Cell → Run all

Clear Results in Jupyter Notebook

Sometimes, you may want to clear any output results that have been produced.

You can do this using the Menu by clicking Cell -> Current Outputs -> Clear. This will clear the current cell that you are working in, which you can activate by clicking in a cell.

You can also clear all of the output in a file by clicking Cell -> All Output -> Clear.

Save Jupyter Notebook Files

You can save Jupyter Notebook files (.ipynb) using the Menu or Keyboard Shortcuts.

Function

Keyboard Shortcut

Menu Tools

Save notebook

Esc + s

File → Save and Checkpoint

Download Jupyter Notebook Files

When using the Jupyter Cloud environment, you can download Jupyter Notebook files (.ipynb) to our computers using the Menu by clicking File -> Download as -> Notebook (.ipynb).

Close Your Jupyter Notebook Session

Close and Shutdown Jupyter Notebook Files

After saving your Jupyter Notebook files (.ipynb), you can close the browser tab displaying the notebook, but you still need Shutdown the notebook from the dashboard.

To Shutdown a Jupyter Notebook file (.ipynb), click in the checkbox to left of the filename. You will see an orange button named Shutdown appear in the top left of the dashboard menu; click on it to Shutdown any file that is checked in the list.

You have now experienced the benefits of using Jupyter Notebook for open reproducible science!

Without writing your own code, you were able to easily replicate this analysis because this code block can be shared with and run by anyone using Python. By the end of this course, you will be able to write your own code to conduct analysis and produce plots like these.

Optional Challenge 2

Test your Jupyter Notebook skills to:

Launch Jupyter Notebook from the ea-bootcamp-day-1 directory.

Open the existing Jupyter Notebook file called jupyter-notebook-interface.ipynb.

Double-click in a Markdown to see the syntax.

Run the cell to see Markdown return to the nice formatting.

Add a Markdown cell as the second cell of the notebook. Include:

a Markdown subtitle (##) with the name of the notebook (e.g. Earth Analytics Bootcamp - Day 1 Lesson on Jupyter Notebook)