← Master Index
Vol. 03 Module 3.3 Lecture

Jupyter

AI & Data Libraries

How This Lesson Fits the Module

You have met the libraries—NumPy, Pandas, PyTorch, and others. Jupyter is where most AI engineers actually write, run, and share that code: an interactive notebook combining prose, equations, visualizations, and executable cells.

Jupyter is not a replacement for production Python modules—it is the exploration and communication layer. Master it before the module capstone in Google Colab.

Learning Objectives

By the end of this lesson, students should be able to:

  • Install and launch JupyterLab or the classic Notebook interface.
  • Work with code, markdown, and raw cells effectively.
  • Manage kernels, restarts, and reproducible execution order.
  • Organize notebooks for EDA, experimentation, and stakeholder reports.
  • Know when notebooks are appropriate versus when to use .py scripts.
  • Export notebooks and transition experiments to production code.

What Jupyter Is—and When to Use It

Jupyter is an open-source interactive computing environment. The notebook document (`.ipynb`) stores cells executed by a kernel—typically IPython for Python. Output (text, tables, plots) appears inline beneath each cell.

Use Jupyter when…Use plain Python scripts when…
Exploring data and iterating on model ideasBuilding production services and scheduled jobs
Teaching, documenting, or presenting analysisYou need version-control-friendly diffs (notebooks are noisy)
Visualizing intermediate results cell by cellRunning automated CI/CD test suites
Prototyping before refactoring to modulesLong-running training with checkpoint/resume requirements

Getting Started

# In your project virtual environment pip install jupyterlab ipykernel # Register the venv as a selectable kernel python -m ipykernel install --user --name=ai-curriculum --display-name "Python (ai-curriculum)" # Launch jupyter lab
Engineering Rule

Always activate the correct virtual environment before launching Jupyter. The most common notebook bug is running cells against the system Python while believing you are in a project venv.

Cell Types and Keyboard Workflow

# Cell 1 — imports (run once per session) import numpy as np import pandas as pd import matplotlib.pyplot as plt %matplotlib inline # inline plots in the notebook # Cell 2 — load data df = pd.read_csv("data/churn.csv") df.head()

Magic Commands

IPython magics are notebook conveniences prefixed with % (line) or %% (cell).

MagicPurpose
%timeitMicro-benchmark a line of code
%pip install packageInstall into the active kernel’s environment
%load_ext autoreload
%autoreload 2
Reload edited .py modules without kernel restart
%%timeTime an entire cell
%who / %whosList variables in namespace (debugging)

Kernel State and Reproducibility

Notebooks remember state. Running cells out of order can leave variables defined that mislead you about what code actually does.

Critical Mistake — Hidden State

You tweak cell 5, never re-run cells 1–4, and get perfect accuracy. You share the notebook; your teammate runs “Restart Kernel & Run All” and gets garbage. Always verify with a full restart before declaring an experiment successful.

Good Notebook Hygiene

  • One logical step per cell
  • Clear markdown section headers
  • Pin package versions at the top
  • “Restart & Run All” before sharing
  • Extract reusable logic to .py modules

Signs It Should Leave the Notebook

  • Same code copied across three experiments
  • Training loops longer than a coffee break
  • Need for unit tests and linting in CI
  • Secrets/API keys appearing in cells
  • Multi-engineer collaboration on core logic

From Notebook to Production

The professional workflow: explore in Jupyter, refactor stable functions into a Python package, test with pytest, deploy as a script or service.

# features.py (extracted from notebook) def build_features(df: pd.DataFrame) -> pd.DataFrame: out = df.copy() out["tenure_months"] = out["tenure_days"] / 30.44 return out # train.py (script entry point) from features import build_features # ... sklearn Pipeline, argparse, logging

Knowledge Check

  1. Short Answer: What is a kernel? Answer: The process that executes code cells and holds variable state.
  2. True/False: Running cells out of order can cause reproducibility bugs. Answer: True.
  3. Short Answer: Why %matplotlib inline? Answer: Renders plots directly in notebook output.
  4. Multiple Choice: Best pre-share verification: (a) run last cell only, (b) restart kernel and run all, (c) save without running. Answer: (b).
  5. Short Answer: What keyboard shortcut runs a cell and advances? Answer: Shift+Enter.
  6. True/False: You should activate the project virtual environment before launching Jupyter. Answer: True—otherwise cells may run against system Python.
  7. Short Answer: What does %timeit do? Answer: Micro-benchmarks a line of code.
  8. Multiple Choice: Production scheduled jobs belong in: (a) notebooks only, (b) plain Python scripts/services, (c) markdown cells, (d) raw cells. Answer: (b).
  9. True/False: Secrets and API keys should appear in notebook cells. Answer: False—extract to env/config and keep them out of shared notebooks.
  10. Short Answer: What is the professional notebook-to-production workflow? Answer: Explore in Jupyter, refactor stable functions into a package, test, then deploy as a script or service.

Key Takeaways

  • Jupyter is the standard interactive environment for AI exploration and communication.
  • Manage kernels and virtual environments deliberately.
  • Treat “Restart & Run All” as your reproducibility gate.
  • Refactor proven notebook code into tested Python modules for production.
  • Next: Google Colab—cloud notebooks with free GPU access.
Trainer’s Guide

Exercise: Deliberately break a notebook by running cells out of order. Students diagnose the failure, then fix it with proper structure and a clean “Run All” pass.

Recap: Jupyter is the interactive workspace for exploration; continue to Google Colab for hosted notebooks with free GPU access.