Lesson 4. Get to Know the Jupyter Notebook Interface


This tutorial walks you through the Jupyter Notebook interface.

Learning Objectives

At the end of this activity, you will be able to:

  • Open Jupyter Notebook from the terminal.
  • Write and run Python code and Markdown in Jupyter Notebook.
  • Be able to add and run Markdown text in a Jupyter Notebook file.

What You Need

Information below is adapted from materials developed by Project Jupyter, Datacamp, and Dataquest.

About Jupyter Notebook

Jupyter Notebook supports a reproducible workflow. It allows you to:

  • Write and run inline 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 code and Markdown text.

Advantages of Jupyter Notebook

Jupyter Notebook also makes coding more efficient.

  • Shortcuts: Jupyter Notebook has different shortcuts for running cells, adding new cells and formatting the notebook to help save you time
  • Autocomplete : Autocomplete is available to quickly find function names, arguments for those functions, and variables that are stored in your environment. This makes typing your code easier and less error-prone. Hitting tab while typing will prompt Jupyter Notebook to help you complete the name of a function or variable that you want to call.
  • Markdown Integration: You can combine text and code in the same document. This allows you to document and explain the code you have written and make your workflow more reproducible.

Get to Know Jupyter Notebook

In this lesson, you will explore Jupyter Notebook, the Integrated Development Environment (IDE) that you will use to write code, navigate files on our computer, inspect variables and more.

An IDE is different from a text editor. IDEs allow you to write, test, and debug code.

Other commonly used Python IDEs are Spyder (which comes with the Python Anaconda distribution) and PyCharm. There are many tools to help you code, but in this class, you will use Jupyter Notebook.

Jupyter Notebook is an open source product and free under the BSD 3-clause “New” or “Revised” License.

How to Start Jupyter Notebook

  • To launch Jupyter Notebook via the terminal, first make sure you are in the earth-analytics directory and that the earth-analytics-python environment has been activated. Then, you can launch Jupyter Notebook by typing jupyter notebook.
$ source activate earth-analytics-python 
$ cd earth-analytics 
$ jupyter notebook 

If you are on Windows, use activate earth-analytics-python to active the conda environment:

$ activate earth-analytics-python 
$ cd earth-analytics 
$ jupyter notebook 

Jupyter Notebooks combine three components:

  1. The Jupyter Notebook: Jupyter Notebook is a web application that launches in a web browser like Firefox or Safari. The notebook is where you write and run your code.

  2. Kernels: A kernel is the program that runs your code. For instance, if you want to run Python code, then you will run a Python kernel in Jupyter Notebook. The kernel runs your code in the given language and returns output back to the Jupyter Notebook web application. Jupyter Notebook supports over 40 different kernel languages. In this class, you will use Python. Specifically, you will use the earth-analytics-python kernel which contains all of the libraries that you need for this course.

  3. Notebook documents The notebook document is the file that you use to store your code and Markdown. Each Jupyter Notebook file has an .ipynb extension.

Sample Jupyter Notebook from Jupyter documentation indicating the location of the toolbar, menu bar, cell in command mode, and kernel indicator.
Sample Jupyter Notebook from Jupyter documentation indicating the location of the toolbar, menu bar, cell in command mode, and kernel indicator.

When you first start the Jupyter Notebook server, your browser will open to the notebook dashboard.

Jupyter notebook dashboard with one notebook called week1 and a data folder.
Jupyter notebook dashboard with one notebook called week1 and a data folder.

The dashboard serves as a homepage for Jupyter Notebook. Its main purpose is to display the notebooks and files in the current directory. For this class, you will always work in the earth-analytics directory.

Explore the Jupyter Notebook Interface

When you open or create a new Jupyter Notebook file, you will see three main parts:

  • Menu
  • Toolbar
  • Notebook Area and Cells

Work With Markdown and Code Cells in Jupyter Notebooks

A Jupyter Notebook file consists of a set of cells that can store text or code. You will focus on code and Markdown cells for this lesson.

  • Text Cells: Text cells allow you to write Markdown. This is where you will describe your workflow.
  • Code Cells: Allow you to write code (e.g. using Python).
You can change the type of any cell using the drop down. Cell type options include code, markdown, Raw NBConvert, and heading.
You can change the type of any cell using the drop down. Cell type options include code, markdown, Raw NBConvert, and heading. There are also shortcuts to enter the command mode. To use shortcuts, hit the esc key. After that, you can change a cell to Markdown by hitting the “m” key, or you can change a cell to code by hitting the “y” key.

For a full list of keyboard shortcuts, click the help button, then the keyboard shortcuts button.

Help menu of Jupyter Notebook.
Help menu of Jupyter Notebook.

Markdown Cells in Jupyter

A Jupyter Notebook file can contain text written using the Markdown syntax, in a cell that is specified for markdown.

Jupyter Notebook with a non-rendered Markdown cell.
Jupyter Notebook with a non-rendered Markdown cell.
Jupyter Notebook with a rendered Markdown cell.
Jupyter Notebook with a rendered Markdown cell.

Code Cells in Jupyter Notebooks

You can add code to the code cells in Jupyter Notebook. Code is written in code chunks. When you run the code in a cell, the code output displayed below. A cell can be run by hitting the run button or using a keyboard shortcut, as discussed in the next section.

Jupyter Notebook with a code cell that contains 4 + 5. When you run the code, the output is 9 and is displayed below the code cell.
Jupyter Notebook with a code cell that contains 4 + 5. When you run the code, the output is 9 and is displayed below the code cell.

For more information on code in Jupyter Notebook, see the next lesson on Install and Import Python Packages.

Useful Jupyter Shortcuts For Running Code and Adding Cells

  • You can manipulate your Jupyter Notebook using the drop down tools from the menu, with keyboard shortcuts, or using both.
  • Keyboard shortcuts allow your workflow to be faster and more efficient.
  • The most important keyboard shortcuts are Enter, which enters edit mode, and Esc, which enters command mode.
    • Enter Mode is indicated by green around cells and is for editing the contents of a cell
    • Command Mode is indicated by a grey cell border with blue on the left edge. This mode allows you to edit the notebook as a whole.

The table below shows common tasks in Jupyter Notebook and how to do them using keyboard shortcuts or the menu tool.

FunctionKeyboard ShortcutMenu Tools
Save notebookEsc + sFile → Save and Checkpoint
Create new cellEsc + a (above), Esc + b (below)Insert→ cell above Insert → cell below
Run CellCtrl + enterCell → Run Cell
Copy CellcCopy Key
Paste CellvPaste Key
Interrupt KernelEsc + i iKernel → Interrupt
Restart KernelEsc + 0 0Kernel → Restart
Find and replace on your code but not the outputsEsc + fN/A
merge multiple cellsShift + MN/A
When placed before a function Information about a function from its documentation?N/A

Data Tip: Inline magic are commands built into the Python kernel and always start with a %. Magic commands are useful shortcuts. Magic commands always start with a % because this symbol isn’t valid in Python. Magic commands also only work on the ipython kernel. Magic commands provide shortcuts to common tasks that would normally take several lines of code to accomplish in Python.

MagicWhat it does
%writefileSaves the contents of a cell to an external file
%timeitShows how long it takes a cell of code to run
%whoList all variables of a global scope
%storePass variables between notebooks
%loadInsert code from an external script

Leave a Comment