In this lesson, you will learn how to use
Jupyter Notebook to run
Python code and render
Markdown text in existing
Jupyter Notebook files (
After completing this hands-on activity, you will be able to:
- List the components and functionality of
Jupyter Notebookand explain how they support open reproducible science
- Navigate the
Jupyter Notebookdashboard and open existing
Jupyter Notebookfiles (
- Run Code and
Markdowncells within Jupyter Notebook to execute
Pythoncode and render
- Create new Code and
Markdowncells within Jupyter Notebook
What You Need
Be sure that you have followed the instructions on Setting up Git, Bash, and Anaconda on your computer to install the tools for your operating system (Windows, Mac, Linux).
This notebook is available in the ea-bootcamp-day-1 repository that you cloned to
earth-analytics-bootcamp under your home directory.
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.
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 Notebookinteractive 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
Jupyter Notebookfile has an
Kernels A kernel runs your code in a specific programming language.
Jupyter Notebooksupports over 40 different languages. In this class, you will use
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 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!
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).
Data Tip: Learn more about Markdown.
Hopefully, by now you understand:
- How the
Jupyter Notebookenvironment works
- How Jupyter supports open reproducible science through combining programming code, displaying output, and documentation with
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
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
Jupyter Notebook, you should always do the following:
- change the current directory to your desired working directory. In this case:
- run the command
jupyter notebookin 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.
$ cd ~/earth-analytics-bootcamp $ pwd /users/jpalomino/earth-analytics-bootcamp
Begin a Jupyter Notebook Session From the Terminal
Now, start a new Jupyter Notebook session by typing the command
jupyter notebook in the
$ 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.
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.
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 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.
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.
Understand the Structure of Jupyter Notebook Files
Jupyter Notebook has three main parts, which are highlighted in the image below:
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.
Jupyter Notebook consists of a set of cells, which can be specified to store text such as
Markdown or Code such as
You can check the cell type of any cell by clicking in the cell and looking at the Cell Type in Toolbar.
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
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
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
## 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.
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.
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.
|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.
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.
Shutdown the Jupyter Notebook Local Server
After all of your notebooks are closed and shut down, you can close the browser tab displaying the Jupyter Notebook dashboard.
You will notice that the
Terminal is still running commands for your
Jupyter Notebook session.
You can shutdown the session by clicking in the
Terminal window and clicking
Last, you will be asked to confirm that you want to
Shutdown this notebook server (y/[n])?. Type
y and hit
Enter to confirm.
Then, you can close the
Terminal by typing the command
Optional Challenge 1
Jupyter Notebook skills to:
- Add a Code cell and run the following
Pythoncode to determine which day had the most precipitation (i.e. the day of the greatest flooding) during the Fall 2013 flood in Boulder, CO, U.S.A.
# import required python packages import pandas as pd import matplotlib.pyplot as plt from matplotlib.dates import DateFormatter import matplotlib.dates as mdates # create variable with data boulder_precip = pd.DataFrame(columns=["date", "precip"], data=[ ["2013-09-09", 0.1], ["2013-09-10", 1.0], ["2013-09-11", 2.3], ["2013-09-12", 9.8], ["2013-09-13", 1.9], ["2013-09-14", 0.01], ["2013-09-15", 1.4], ["2013-09-16", 0.4]]) # convert text strings in date to datetime type boulder_precip.date = pd.to_datetime(boulder_precip.date) # format date to just contain month and date myFmt = DateFormatter("%m/%d") # create plot with title and custom date labels for axes plt.rcParams['figure.figsize'] = (12, 12) fig, ax = plt.subplots() ax.bar(boulder_precip['date'].values, boulder_precip['precip'].values, edgecolor='blue') plt.setp(ax.get_xticklabels(), rotation=45); ax.set(xlabel="Date", ylabel="Precipitation (Inches)",) ax.set(title="Daily Precipitation (inches)\nBoulder, Colorado 2013") ax.xaxis.set_major_formatter(myFmt) ax.xaxis.set_minor_locator(mdates.DayLocator())
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
Jupyter Notebook skills to:
Jupyter Notebookfrom the
Open the existing
Jupyter Notebookfile called
Double-click in a
Markdownto see the syntax.
Run the cell to see
Markdownreturn to the nice formatting.
- Add a
Markdowncell as the second cell of the notebook. Include:
##) with the name of the notebook (e.g.
Earth Analytics Bootcamp - Day 1 Lesson on Jupyter Notebook)
- a smaller
###) with your name (e.g.
- Add four Code cells to the bottom of the notebook and run the following
Pythoncode for calculations:
16 - 4
24 / 4
2 * 4
2 ** 4
What do you notice about the output of
24 / 4 compared to the others? What operation does