NEON EDUCATION bio photo

NEON EDUCATION

Devoted to open data and open source in science and education.

View All Tutorials

This tutorial is a part of a series!

Click below to view all lessons in the series!

Tags

R programming (52)
Hierarchical Data Formats (HDF5) (15)
Spatial Data & GIS (22)
LiDAR (10)
Raster Data (14)
Remote Sensing (24)
Data Visualization (4)
Hyperspectral Remote Sensing (17)
Time Series (15)
Phenology (7)
Vector Data (6)
Metadata (1)
Git & GitHub (7)
(1) (1) (13)

Tutorial by R Package

dplyr (7)
ggplot2 (16)
h5py (2)
lubridate (time series) (6)
maps (1)
maptools (1)
plyr (2)
raster (26)
rasterVis (raster time series) (3)
rgdal (GIS) (24)
rgeos (2)
rhdf5 (11)
sp (5)
scales (4)
gridExtra (4)
ggtheme (0)
grid (2)
reshape2 (3)
plotly (5)

View ALL Tutorial Series




Twitter Youtube Github


Blog.Roll

R Bloggers

Setting up Jupyter Notebooks

You can set up your notebook in several ways. Here we present the Anaconda Python distribution method so as to follow the Data Institute set up instructions.

Browser

First, make sure you have an updated browser on which to run the app. Both Mozilla Firefox and Google Chrome work well.

Installation

Data Institute participants should have already installed Jupyter Notebooks through the Anaconda installation during the Data Institute set up instructions.

If you install Python using pip you can install the Jupyter package with the following code.

 
# Python2
pip install jupyter
# Python 3
pip3 install jupyter

Using Jupyter Notebooks

Launching the Application

To launch the application either launch it from the Anaconda Navigator or by typing jupyter notebook into your terminal or command window.

 
# Launch Jupyter
jupyter notebook

More information can be found in the Read the Docs Running the Jupyter Notebook.

The following information is adapted from Griffin Chure’s Tutorial 0b: Using Jupyter Notebooks

If everything launched correctly, you should be able to see a screen which looks something like this. Note that the home directory will be whatever directory you have navigated to in your terminal before launching Jupyter Notebooks.

Upon opening the application, you should see a screen similar to this one. Source: Griffin Chure's <a href="http://bi1.caltech.edu/code/t0b_jupyter_notebooks.html" Tutorial 0b: Using Jupyter Notebooks</a>

To start a new Python notebook, click on the right-hand side of the application window and select New (the expanded menu is shown in the screen shot above). This will give you several options for new notebook kernels depending on what is installed on your computer. In the above screenshot, there are two available Python kernels and one Matlab kernel. When starting a notebook, you should choose Python 3 if it are available or conda(root) .

Once you start a new notebook, you will be brought to the following screen.

Upon opening a new Python notebook, you should see a screen similar to this one. Source: Griffin Chure's Tutorial 0b: Using Jupyter Notebooks

Welcome to your first look at a Jupyter notebook!

There are many available buttons for you to click. The three most important components of the notebook are highlighted in colored boxes.

  • In blue is the name of the notebook. By clicking this, you can rename the notebook.
  • In red is the cell formatting assignment. By default, it is registered as code, but it can also be set to markdown as described later.
  • In purple, is the code cell. In this cell, you can type an execute Python code as well as text that will be formatted in a nicely readable format.

Writing & running code

The following information is adapted from Griffin Chure’s Tutorial 0b: Using Jupyter Notebooks

All code you write in the notebook will be in the code cell. You can write single lines, to entire loops, to complete functions. As an example, we can write and evaluate a print statement in a code cell, as is shown below.

To exectue the code, we can simply hit Shift + Enter while our cursor is in the code cell.

 # This is a comment and is not read by Python
 print('Hello! This is the print function. Python will print this line below')

 Hello! This is the print function. Python will print this line below

We can also write a for loop as an example of executing multiple lines of code at once.

 # Write a basic for loop.
 for i in range(5):
# Multiply the value of i by two and assign it to a variable. 
temp_variable = 2 * i

# Print the value of the temp variable.
print(temp_variable)

 0
 2
 4
 6
 8

There are two other useful keyboard shortcuts for running code:

  • Alt + Enter runs the current cell and inserts a new one below
  • Ctrl + Enter run the current cell and enters command mode.

For more keyboard shortcuts, check out weidadeyue’s Shortcut cheatsheet.

Data Tip: Code cells can be executed in any order. This means that you can overwrite your current variables by running things out of order. When coding in notebooks, be cautions of the order in which you run cells.

If you would like more details on running code in Jupyter Notebooks, please go through the following short tutorial by Running Code by contributors to the Jupyter project. This tutorial touches on start and stopping the kernel and using multiple kernels (e.g., Python and R) in one notebook.

Writing Text

The following information is adapted from Griffin Chure’s Tutorial 0b: Using Jupyter Notebooks

Arguably the most useful component of the Jupyter notebook is the ability to interweave code and explanatory text into a single, coherent document. Through out the Data Institute (and one’s everyday workflow), we encourage all code and plots should be accompanied with explanatory text.

Each cell in a notebook can exist either as a code cell or as text-formatting cell called a markdown cell. Markdown is a mark-up language that very easily converts to other typesetting formats such as HTML and PDF.

Whenever you make a new cell, it’s default assignment will be a code cell. This means when you want to write text, you will need to specifically change it to a markdown cell. You can do this by clicking on the drop-down menu that reads code’ (highlighted in red in the second figure of this page) and selecting ‘Markdown’. You can then type in the code cell and all Python syntax highlighting will be removed.

Resources for Learning Markdown

Saving & Quitting

The following information is adapted from Griffin Chure’s Tutorial 0b: Using Jupyter Notebooks

Jupyter notebooks are set up to autosave your work every 15 or so minutes. However, you should not rely on the autosave feature! Save your work frequently by clicking on the floppy disk icon located in the upper left-hand corner of the toolbar.

To navigate back to the root of your Jupyter notebook server, you can click on the Jupyter logo at any time.

To quit your Jupyter notebook, you can simply close the browser window and the Jupyter notebook server running in your terminal.

Converting to HTML and PDF

In addition to sharing notebooks in the.ipynb format, it may useful to convert these notebooks to highly-portable formats such as HTML and PDF.

To convert, you can either use the dropdown menu option

File -> download as -> …

or via the command line by using the following lines:

 jupyter nbconvert --to pdf notebook_name.ipynb 

Where “notebook_name.ipynb” matches the name of the notebook you want to convert. Prior to converting the notebook you must be in the same working directory as your notebooks.

If you prefer to convert to a different format, like html, you simply change the file type. Read more on what formats you can convert to and more about the nbconvert package .

Additional Resources

Using Jupyter Notebooks

Using Python