DCubes
Python module

Lesson 16

Notebooks as files

Run Jupyter in a venv, understand that .ipynb is JSON, and keep scripts as the source of truth.

beginner35 minRun on your machine

What you will be able to do

  • Install Jupyter in the project venv and start the notebook server
  • Create a short notebook and see it on disk as JSON
  • State when to promote notebook code into a .py script

Why this matters for data work

Notebooks are excellent for exploration and terrible as the only production artifact. A .ipynb is a JSON file with cells, outputs, and metadata — messy in git, easy to drift from the job that actually runs. Use notebooks to learn; ship scripts.

Concepts

Jupyter — browser UI over a kernel that executes cells in order (unless you jump around — that is a footgun).

.ipynb — JSON on disk. Open it in an editor and you will see cells, source, outputs.

Kernel — should be this project’s venv, not a random global Python.

Promotion path — once a cell sequence works, move it to a .py with functions and logging.

Practice on your machine

cd ~/dcubes/python-lab
source .venv/bin/activate
python -m pip install jupyter
mkdir -p notebooks

Start Jupyter (binds locally; stop with Ctrl-C when done):

cd ~/dcubes/python-lab
jupyter notebook --notebook-dir=notebooks

In the browser:

  1. New → Notebook → Python 3 (ipykernel from the venv).
  2. Cell 1: print("notebook-ok") → Run.
  3. Cell 2: from pathlib import Path; print(Path.cwd()) → Run.
  4. Save as check.ipynb.

In another terminal (leave Jupyter running or stop it first):

cd ~/dcubes/python-lab
head -n 30 notebooks/check.ipynb

You should see JSON with "cells" and your source strings.

What you should see

  • Browser notebook UI connected to the venv kernel.
  • notebooks/check.ipynb as readable JSON.
  • Print output from the first cell.

When to leave the notebook

Stay in notebook Move to script
One-off profile of a sample Scheduled job
Plot to think Load to Postgres
Teaching yourself an API Shared team entrypoint

Common mistakes

  • Installing jupyter globally and losing project packages in the kernel list.
  • Hidden state — running cells out of order; Restart & Run All before trusting results.
  • Committing huge outputs — clear outputs before git when you get there.

Next

You are ready for the capstone lab: First Python data shift — generate random retail orders and load them into Postgres.