Jupyter Notebooks: Interactive Computing in Your Browser

Before You Start

You should know: - How to create and activate a virtual environment and install libraries.

You will learn: - Why notebooks transformed geographic data science. - How to install, launch, and navigate Jupyter. - The difference between Code cells and Markdown cells. - Essential keyboard shortcuts that make notebooks fast to use. - The “restart and run all” discipline that keeps notebooks trustworthy. - What .ipynb files actually are.

Introduction

So far you have set up an environment and installed libraries. The question remains: where do you actually write the code?

The traditional approach is a plain .py script — a text file that runs from top to bottom. If an error occurs on line 400, everything stops and you re-run the entire file. For data exploration, this is painful.

Jupyter Notebooks solve this by letting you chop your code into small blocks called cells and run them one at a time. The output — a table, a map, a graph, or an error message — appears immediately beneath the cell. You write a cell, run it, see the result, refine it, and continue. The notebook becomes an executable document that records both your code and its output.

Jupyter is now the dominant tool for geographic data science, machine learning, and scientific research.

A Note on What “Browser” Means Here

When Jupyter launches, it opens in your web browser. This can be confusing: Jupyter looks like a website, but it is not connected to the internet. It is a local server running on your own computer, and your browser is simply the interface to it. If you disconnect from Wi-Fi, Jupyter continues to work perfectly.

Installing Jupyter

Jupyter is a Python package, installed into your active virtual environment like any other library.

Activate your environment first, then:

Using pip:

pip install jupyterlab

Using conda:

conda install -c conda-forge jupyterlab

JupyterLab is the modern, full-featured version. The older jupyter notebook interface is simpler but less capable. This book assumes JupyterLab.

Launching Jupyter

In your terminal, with your environment activated, navigate to your project folder and start the server:

jupyter lab

Your terminal will lock up and start printing server logs — this is normal. After a few seconds, your browser will open automatically to the JupyterLab interface. You will see a file browser on the left and a launcher tab on the right.

To create a new notebook, click Python 3 (ipykernel) in the Notebook section of the launcher.

Working With Cells

A notebook is a sequence of cells. Each cell is an independent block of code or text. There are two types.

Code Cells

Code cells contain Python. Type code and run it with Shift + Enter. The output appears directly below the cell, and a new empty cell appears after it.

import numpy as np

elevations = np.array([120, 340, 580, 720, 690, 440, 210])
print("Mean elevation:", elevations.mean(), "m")
print("Maximum elevation:", elevations.max(), "m")

The In [1]: label on the left shows the execution order. If you re-run a cell, the number updates. This matters — you can run cells out of order, which sometimes causes surprising bugs (more on this below).

Markdown Cells

Markdown cells contain formatted text. Change a cell type using the dropdown in the toolbar, or press Escape then M.

In a Markdown cell: - # Heading 1, ## Heading 2 create section headings - **bold** and *italic* format text - - item creates bullet lists - `code` formats inline code - $E = mc^2$ renders LaTeX mathematics inline

Press Shift + Enter on a Markdown cell to render it. Double-click it to edit again.

Use Markdown cells to document your analysis: explain your methodology, record decisions, note what the data source is, flag known issues. A notebook without explanation is as hard to read as a script without comments.

Keyboard Shortcuts

Learning these shortcuts makes notebooks far faster to use. Jupyter has two modes:

  • Edit mode (green border): you are typing inside a cell
  • Command mode (blue border): commands apply to the cell as a whole. Press Escape to enter command mode from edit mode.
Shortcut Mode Action
Shift + Enter Either Run cell and move to the next
Ctrl + Enter Either Run cell and stay in place
Escape Edit Switch to command mode
Enter Command Switch to edit mode
A Command Insert cell above
B Command Insert cell below
D D (press D twice) Command Delete the selected cell
M Command Change cell type to Markdown
Y Command Change cell type to Code
Z Command Undo last cell deletion

You will use A, B, and Shift+Enter constantly. Learn them in the first session and they become muscle memory.

The Kernel: What Actually Runs Your Code

When you run a cell, Jupyter sends the code to a kernel — a separate Python process running in the background. The kernel remembers every variable you have created so far in the session. If you define x = 5 in cell 1, the kernel still knows x when you run cell 10.

This memory is both useful and dangerous.

Restart and Run All

Here is a failure mode that trips up almost every new Jupyter user:

  1. You run cells in order 1, 2, 3, 4.
  2. You go back and delete a line in cell 2 that set a variable.
  3. You run cell 2 again, then cell 3. Cell 3 still works, because the kernel still has the deleted variable in memory from step 1.
  4. You close and reopen the notebook. Cell 3 now fails — the variable never gets set.

Your notebook only represents a complete, reproducible analysis if it can run top to bottom in a fresh kernel.

After making significant edits, always test this:

In JupyterLab: Kernel → Restart Kernel and Run All Cells

This clears all memory, re-runs every cell in order from the top, and shows you exactly what a fresh reader will see. If anything fails, fix it before saving.

Make this a discipline: if a notebook is going to be shared, re-run it completely before you commit it.

Shutting Down Jupyter

Closing the browser tab does not stop the server. The kernel keeps running, consuming memory. To properly shut down:

  1. Save your notebook: Ctrl + S (or Cmd + S on Mac).
  2. Switch to the terminal window that is running the Jupyter server.
  3. Press Ctrl + C twice.

The terminal returns to a normal prompt. The server has stopped.

What is an .ipynb File?

When you save a notebook, Jupyter writes a .ipynb file. Despite the notebook-like appearance, .ipynb files are plain text in JSON format. Open one in a text editor and you will see your code, markdown, and cell outputs stored as JSON objects.

This means: - .ipynb files can be committed to Git — they are plain text. - The stored outputs (tables, images, plots) are embedded as base64 strings and can make the file very large. It is often cleaner to clear outputs before committing: Edit → Clear All Outputs. - Tools like nbconvert can render .ipynb files to HTML, PDF, or .py scripts.

Verify Your Work

  1. Activate your virtual environment and install JupyterLab.
  2. Run jupyter lab and confirm the interface opens in your browser.
  3. Create a new notebook.
  4. In a Code cell, run import numpy as np; print(np.__version__). Confirm it prints a version number.
  5. Add a Markdown cell below it with a ## Heading and a sentence.
  6. Practice inserting a new cell above and below using keyboard shortcuts.
  7. Use Kernel → Restart Kernel and Run All Cells and confirm everything still runs.
  8. Shut down the server with Ctrl + C in the terminal.

Next Steps