An analysis someone else can read.
Turn validated records into an annotated chart and a machine-readable summary.
01 The idea
A report is more useful when its visual and numeric outputs come from the same validated data. This build uses one source of truth for category values, a chart with explicit units, and a JSON summary that can be checked in tests. The chart is a communication layer; the meaning and quality of its input must be established first.
02 The experiment
Read it once. Predict what will happen. Then copy it into a .py file and run it in your local Python setup.
import json
from pathlib import Path
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
records = [{"name": "Build", "minutes": 40}, {"name": "Test", "minutes": 20}, {"name": "Review", "minutes": 15}]
for row in records:
if not isinstance(row["name"], str) or type(row["minutes"]) is not int or row["minutes"] < 0:
raise ValueError("Invalid report record")
summary = {"tasks": len(records), "minutes": sum(row["minutes"] for row in records)}
Path("summary.json").write_text(json.dumps(summary), encoding="utf-8")
fig, ax = plt.subplots(figsize=(6, 3.5), layout="constrained")
bars = ax.barh([r["name"] for r in records], [r["minutes"] for r in records], color="#ed5027")
ax.bar_label(bars, padding=3)
ax.set(title="Planned effort by task", xlabel="Minutes", xlim=(0, 48))
ax.invert_yaxis()
fig.savefig("analytics-report-project.png", dpi=140, bbox_inches="tight")
plt.close(fig)
print(summary){'tasks': 3, 'minutes': 75}
03 Under the hood
- 1
The validation precedes both outputs. A chart should not silently coerce a corrupt duration while the JSON report uses a different rule.
- 2
The numeric summary is easy to test independently of pixel rendering. Test the summary contract and inspect the actual exported chart.
- 3
Extend this project by accepting the validated JSON from the file pipeline and by adding a report title supplied through argparse. Keep input discovery, validation, analysis, and rendering as separate functions.
04 Your turn, builder
Extract a summary function and verify that empty input produces zero tasks and minutes.
Reveal one possible solution +
def summarize(records):
return {"tasks": len(records), "minutes": sum(row["minutes"] for row in records)}
assert summarize([]) == {"tasks": 0, "minutes": 0}
assert summarize([{"minutes": 4}]) == {"tasks": 1, "minutes": 4}
print("Summary checks passed")↗ Definition of done
- □Reject invalid duration values.
- □Produce summary.json with 3 tasks and 75 minutes.
- □Export a readable chart with units and zero baseline.
- □Test empty-input summary behaviour.
- □Accept a real validated input file instead of editing source.
05 Check your signal
What should the chart and numeric summary share?
Experiment. Logged.
When you can explain the idea and make the example your own, mark it complete. Your log stays in this browser.
Keep exploring
Go deeper with the official Python guide ↗.