Skip to content
VDAI with VD

Phase 00 · Setup & Tooling

Jupyter Notebooks

Notebooks are the lab bench of AI engineering. You prototype here, then move what works into production.

  • Build
  • Python
  • 30 minutes
  • Before this: Phase 0, Lesson 01

Learning Objectives

  • Install and launch JupyterLab, Jupyter Notebook, or VS Code with the Jupyter extension
  • Use magic commands (%timeit, %%time, %matplotlib inline) to benchmark and visualize inline
  • Distinguish when to use notebooks vs scripts and apply the "explore in notebooks, ship in scripts" workflow
  • Identify and avoid common notebook traps: out-of-order execution, hidden state, and memory leaks

The Problem

Every AI paper, tutorial, and Kaggle competition uses Jupyter notebooks. They let you run code in pieces, see outputs inline, mix code with explanations, and iterate fast. If you try to learn AI without notebooks, you're doing math homework without scratch paper.

But notebooks have real traps. People use them for everything, including things they're terrible at. Knowing when to use a notebook and when to use a script will save you from debugging nightmares later.

The Concept

A notebook is a list of cells. Each cell is either code or text.

graph TD
    A["**Markdown Cell**\n# My Experiment\nTesting learning rate 0.01"] --> B["**Code Cell** ► Run\nmodel.fit(X, y, lr=0.01)\n---\nOutput: loss = 0.342"]
    B --> C["**Code Cell** ► Run\nplt.plot(losses)\n---\nOutput: inline plot"]

The kernel is a Python process running in the background. When you run a cell, it sends the code to the kernel, which executes it and sends back the result. All cells share the same kernel, so variables persist between cells.

graph LR
    A[Notebook UI] <--> B[Kernel\nPython process]
    B --> C[Keeps variables in memory]
    B --> D[Runs cells in whatever order you click]
    B --> E[Dies when you restart it]

That "whatever order you click" part is both the superpower and the foot-gun.

Interactive figure coming later.

Build It

Step 1: Pick your interface

Three options, one format:

InterfaceInstallBest for
JupyterLabpip install jupyterlab then jupyter labFull IDE experience, multiple tabs, file browser, terminal
Jupyter Notebookpip install notebook then jupyter notebookSimple, lightweight, one notebook at a time
VS CodeInstall "Jupyter" extensionAlready in your editor, git integration, debugging

All three read and write the same .ipynb file. Pick whatever you like. JupyterLab is the most common in AI work.

pip install jupyterlab
jupyter lab

Step 2: Keyboard shortcuts that matter

You operate in two modes. Press Escape for command mode (blue bar on the left), Enter for edit mode (green bar).

Command mode (most used):

KeyAction
Shift+EnterRun cell, move to next
AInsert cell above
BInsert cell below
DDDelete cell
MConvert to markdown
YConvert to code
ZUndo cell operation
Ctrl+Shift+HShow all shortcuts

Edit mode:

KeyAction
TabAutocomplete
Shift+TabShow function signature
Ctrl+/Toggle comment

Shift+Enter is the one you'll use a thousand times a day. Learn it first.

Step 3: Cell types

Code cells run Python and show the output:

import numpy as np
data = np.random.randn(1000)
data.mean(), data.std()

Output: (0.0032, 0.9987)

Markdown cells render formatted text. Use them to document what you're doing and why. Supports headers, bold, italic, LaTeX math ($E = mc^2$), tables, and images.

Step 4: Magic commands

These aren't Python. They're Jupyter-specific commands that start with % (line magic) or %% (cell magic).

Time your code:

%timeit np.random.randn(10000)

Output: 45.2 us +/- 1.3 us per loop

%%time
model.fit(X_train, y_train, epochs=10)

Output: Wall time: 2.34 s

%timeit runs the code many times and averages. %%time runs it once. Use %timeit for microbenchmarks, %%time for training runs.

Enable inline plots:

%matplotlib inline

Every plt.plot() or plt.show() now renders directly in the notebook.

Install packages without leaving the notebook:

!pip install scikit-learn

The ! prefix runs any shell command.

Check environment variables:

%env CUDA_VISIBLE_DEVICES

Step 5: Display rich output inline

Notebooks auto-display the last expression in a cell. But you can control it:

import pandas as pd

df = pd.DataFrame({
    "model": ["Linear", "Random Forest", "Neural Net"],
    "accuracy": [0.72, 0.89, 0.94],
    "training_time": [0.1, 2.3, 45.6]
})
df

This renders a formatted HTML table, not a text dump. Same with plots:

import matplotlib.pyplot as plt

plt.figure(figsize=(8, 4))
plt.plot([1, 2, 3, 4], [1, 4, 2, 3])
plt.title("Inline Plot")
plt.show()

The plot appears right below the cell. This is why notebooks dominate AI work. You see the data, the plot, and the code together.

For images:

from IPython.display import Image, display
display(Image(filename="architecture.png"))

Step 6: Google Colab

Colab is a free Jupyter notebook in the cloud. It gives you a GPU, pre-installed libraries, and Google Drive integration. No setup required.

  1. Go to colab.research.google.com
  2. Upload any .ipynb file from this course
  3. Runtime > Change runtime type > T4 GPU (free)

Colab differences from local Jupyter:

  • Files don't persist between sessions (save to Drive or download)
  • Pre-installed: numpy, pandas, matplotlib, torch, tensorflow, sklearn
  • from google.colab import files to upload/download files
  • from google.colab import drive; drive.mount('/content/drive') for persistent storage
  • Sessions time out after 90 minutes of inactivity (free tier)

Use It

Notebooks vs Scripts: When to use which

Use notebooks forUse scripts for
Exploring a datasetTraining pipelines
Prototyping a modelReusable utilities
Visualizing resultsAnything with if __name__
Explaining your workCode that runs on a schedule
Quick experimentsProduction code
Course exercisesPackages and libraries

The rule: explore in notebooks, ship in scripts.

A common workflow in AI:

  1. Explore data in a notebook
  2. Prototype your model in the notebook
  3. Once it works, move the code to .py files
  4. Import those .py files back into the notebook for further experiments

Common traps

Out-of-order execution. You run cell 5, then cell 2, then cell 7. The notebook works on your machine but breaks when someone runs it top to bottom. Fix: Kernel > Restart & Run All before sharing.

Hidden state. You delete a cell but the variable it created is still in memory. The notebook looks clean but depends on a ghost cell. Fix: Restart the kernel regularly.

Memory leaks. Loading a 4GB dataset, training a model, loading another dataset. Nothing gets freed. Fix: del variable_name and gc.collect(), or restart the kernel.

Ship It

This lesson produces:

  • outputs/prompt-notebook-helper.md for debugging notebook issues

Exercises

  1. Open JupyterLab, create a notebook, and use %timeit to compare list comprehension vs numpy for creating an array of 100,000 random numbers
  2. Create a notebook with both markdown and code cells that loads a CSV, displays a dataframe, and plots a chart. Then run Kernel > Restart & Run All to verify it works top to bottom
  3. Take the code from code/notebook_tips.py, paste it into a Colab notebook, and run it with a free GPU

Key Terms

TermWhat people sayWhat it actually means
Kernel"The thing running my code"A separate Python process that executes cells and keeps variables in memory
Cell"A code block"An independently runnable unit in a notebook, either code or markdown
Magic command"Jupyter tricks"Special commands prefixed with % or %% that control the notebook environment
.ipynb"Notebook file"A JSON file containing cells, outputs, and metadata. Stands for IPython Notebook

Further Reading

Code

  • notebook_tips.py

    python phases/00-setup-and-tooling/05-jupyter-notebooks/code/notebook_tips.pyOpen on GitHub ↗

Take-away files

prompt-notebook-helper.md

---
name: prompt-notebook-helper
description: Debug Jupyter notebook issues including kernel crashes, memory problems, and display failures
phase: 0
lesson: 5
---

You diagnose Jupyter notebook problems. When someone describes an issue, identify the cause and give the fix.

Common issues and fixes:

**Kernel crashes:**
- Out of memory: The dataset or model is too large. Fix: reduce batch size, load data in chunks with `pd.read_csv(path, chunksize=10000)`, use `del variable` then `gc.collect()`, or switch to a machine with more RAM.
- Segfault from native library: Usually a version mismatch between numpy/torch/tensorflow and the system libraries. Fix: create a fresh virtual environment and reinstall.
- Kernel dies silently: Check the terminal where Jupyter is running for the actual error message. The notebook UI often hides it.

**Display problems:**
- Plots not showing: Add `%matplotlib inline` at the top of the notebook. If using JupyterLab, try `%matplotlib widget` for interactive plots (requires `ipympl`).
- DataFrame shows as text instead of HTML table: Make sure the dataframe is the last expression in the cell, not inside a `print()` call. `print(df)` gives text, just `df` gives the rich table.
- Images not rendering: Use `from IPython.display import Image, display` then `display(Image(filename="path.png"))`.
- LaTeX not rendering in markdown: Check for missing dollar signs. Inline: `$x^2$`. Block: `$$\sum_{i=0}^n x_i$$`.

**Memory issues:**
- Notebook uses too much RAM: Variables persist across all cells. Run `%who` to see all variables. Delete large ones with `del var_name` and run `import gc; gc.collect()`.
- Memory keeps growing: You are probably reassigning large variables without freeing the old ones. Restart the kernel (Kernel > Restart) to clear everything.
- Loading multiple large datasets: Use generators or chunked reading. `pd.read_csv(path, chunksize=N)` returns an iterator instead of loading everything at once.

**Execution issues:**
- Notebook works for me but not others: Cells were run out of order. Fix: Kernel > Restart & Run All. If it fails, you have a hidden dependency on a deleted or reordered cell.
- Cell runs forever (hanging): The code might be waiting for input (`input()`), stuck in an infinite loop, or blocked on a network request. Interrupt with Kernel > Interrupt (or press `I` twice in command mode).
- Import errors after pip install: The package installed in a different Python than the kernel is using. Fix: run `!pip install package` inside the notebook, or check `!which python` matches your environment.

**Colab-specific:**
- Session disconnected: Free Colab times out after 90 minutes of inactivity. Save work to Google Drive or download files.
- GPU not available: Runtime > Change runtime type > select GPU. If all GPUs are busy, try again later or use Colab Pro.
- Files disappeared: Colab wipes the filesystem between sessions. Mount Google Drive for persistent storage: `from google.colab import drive; drive.mount('/content/drive')`.

Diagnostic steps:
1. What is the exact error message? (Check both the notebook and the terminal)
2. Does the issue happen after restarting the kernel and running all cells top to bottom?
3. How much data are you loading? (`df.info()` for dataframes, `tensor.shape` and `tensor.dtype` for tensors)
4. What environment are you using? (Local JupyterLab, VS Code, Colab)
5. Were packages installed in the same environment as the kernel? (`!which python` and `import sys; sys.executable`)