Skip to course content
Free Python statistics course

Statistical Data Analytics with Python

Unit 12.01: Importable modules, inline expressions

The analysis lives in importable modules. The report calls them and cites what they return. No number in the prose is ever typed.

Importable modules, inline expressions

The structure that scales is a Quarto document whose setup chunk imports from a src/ package, and whose prose refers to the returned objects through inline expressions. Revise the data, re-render, and every figure in every sentence updates together.

Putting the analysis in modules rather than in the document has a second benefit: it can be imported by a test, a job or another report. A notebook cell cannot.

echo: false hides the code from readers who want findings while leaving it in the file for anyone checking the work. The report performs no analysis of its own.

This block writes a complete report skeleton and inspects it.

import pathlib, tempfile, re

qmd = """---
title: "Checkout variant B: 7-day retention"
date: 2026-07-29
format: html
execute:
  echo: false
  warning: false
---

25 lines with sections for Question, Result and Limitations, 1 code chunk that imports and calls, 4 inline expressions in the prose, and 0 numbers typed by hand. The analysis logic is entirely in src/clean.py and src/analysis.py, so the same code can be unit-tested.

The mistake this prevents

The mistake is rendering once and then editing the output. The next render overwrites the edit, and until then the source and the published document disagree.

Takeaway

Put the analysis in importable modules and reference their return values with inline expressions. Re-render from scratch before circulating, and never edit the rendered output.