Writing the Report: Charts and Tables on a Web Page
A report someone else reads is a web page, so the script saves each chart as a PNG with savefig and closes it, turns tables into HTML with to_html, builds the page with an f-string, and writes it into a folder of its own for each week, ready to open in any browser.
Journeyman · Python · Updated
After this lesson you can
- Save a chart as a PNG with savefig and close it, and turn a table into HTML with to_html
- Build a report page with an f-string, write it into a folder for each week, and open it in a browser
Key takeaways
- fig.savefig(path) writes the chart to a PNG file, and plt.close(fig) frees it, so a script that makes many charts doesn't fill its memory.
- df.to_html(index=False) gives back a table as HTML text, ready to drop into a page.
- An f-string builds the page, with the numbers, the image and the table dropped into their places, and write_text saves it as report.html.
- Each week's report goes in its own folder, such as reports/2026-10-26/, so last week's report is still there when this week's is built.
- The report is a file any browser opens. A scheduler (Task Scheduler on Windows, cron on a Mac) can run the script every Monday.
Numbers printed in a terminal are gone when the window closes, and Dana, who owns the Greenway Food Truck, is never going to open a terminal. A report someone else reads needs to be a file they can open: a web page, with the numbers, a chart and a table. The script can write that page itself. It's how a weekly summary lands in a shared folder, a lab's results get posted for a class, or a club's numbers reach its members.
The example is the food truck's report.py from A Report Script That Finds the Newest Data: it finds the newest week in data/incoming/ (week-2026-10-26.csv for now), loads it with analysis.load_week, which adds a total column (quantity times price), and stacks every week into one table, weeks, with a week column.
Charts become PNG files
Figures and Axes: How matplotlib Draws draws charts under a notebook cell. A script has no cell to draw under, so it saves each chart as a file with fig.savefig, then closes it with plt.close(fig). Each open chart takes memory, and matplotlib warns once a script has more than 20 open; a script that runs every week for a year should close what it opens:
import matplotlib.pyplot as plt
from pathlib import Path
import analysis
week = analysis.load_week("data/incoming/week-2026-10-26.csv")
counts = week.groupby("item")["quantity"].sum().sort_values(ascending=False)
fig, ax = plt.subplots(figsize=(8, 4.5), layout="constrained")
ax.bar(counts.index, counts.values)
ax.set_title("Items sold this week")
ax.set_ylabel("Number sold")
out_dir = Path("reports/2026-10-26")
out_dir.mkdir(parents=True, exist_ok=True)
fig.savefig(out_dir / "items.png")
plt.close(fig)
print([path.name for path in out_dir.iterdir()])
['items.png']
The chart is now a PNG image in reports/2026-10-26/: Street Tacos' bar is the tallest, at 173, then Burrito Bowl at 118. mkdir(parents=True, exist_ok=True) makes the folder, and any folders above it, and does nothing if it's already there, so the script can run twice in one week.
Tables become HTML with to_html
A web page is written in HTML: text with tags such as <h1> for a heading and <table> for a table. pandas writes a table's HTML for you. to_html gives it back as text, and index=False leaves out the row numbers:
by_location = week.groupby("location", as_index=False)["total"].sum()
print(by_location.to_html(index=False))
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th>location</th>
<th>total</th>
</tr>
</thead>
<tbody>
<tr>
<td>City Park</td>
<td>1018.5</td>
</tr>
<tr>
<td>Office Row</td>
<td>1706.5</td>
</tr>
<tr>
<td>Stadium</td>
<td>1730.0</td>
</tr>
</tbody>
</table>
<th> holds a column name and <td> a value; you never type them yourself.
A page from an f-string
The page itself is one long f-string: HTML with the week's numbers, the image and the table dropped into place. Inside the braces, :,.2f formats a number as money, with a comma between thousands and two decimal places:
sales = 4455.0
print(f"Sales: ${sales:,.2f}")
print(f"Sales: ${16543.5:,.2f}")
Sales: $4,455.00
Sales: $16,543.50
Here's the function that writes the whole report into report.py. save_items_chart(week, path) holds the chart code from above:
def write_report(week, weeks, out_dir):
out_dir.mkdir(parents=True, exist_ok=True)
save_items_chart(week, out_dir / "items.png")
numbers = analysis.summarize(week)
by_week = weeks.groupby("week", as_index=False)["total"].sum()
table = by_week.rename(columns={"total": "sales"}).to_html(index=False)
page = f"""<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>Weekly report</title></head>
<body>
<h1>Greenway Food Truck: week of {out_dir.name}</h1>
<p>Sales: ${numbers['sales']:,.2f} from {numbers['items_sold']} items.
Best seller: {numbers['best_item']}.</p>
<img src="items.png" alt="Bar chart of the number of each item sold this week">
<h2>Sales by week</h2>
{table}
</body>
</html>
"""
report = out_dir / "report.html"
report.write_text(page, encoding="utf-8")
return report
Three quotes (f""") let the string run over many lines. <img src="items.png"> finds the chart because the page and the image sit in the same folder, and its alt text tells a reader who can't see the image what it shows.
A folder for each week
main gains a second option, --out, the folder the reports go in, and gives each week a folder of its own, named by the week's date:
parser.add_argument("--out", default="reports", help="the folder for the reports")
newest = newest_week(args.data)
week_date = newest.stem.removeprefix("week-")
report = write_report(
analysis.load_week(newest),
load_all_weeks(args.data),
Path(args.out) / week_date,
)
print("wrote", report.as_posix())
python report.py
wrote reports/2026-10-26/report.html
Double-click report.html (or right-click it in VS Code's Explorer and choose Reveal in File Explorer or Reveal in Finder) and it opens in your browser: the heading, the sales, the chart and the table of every week.
When next week's file arrives, the same command writes reports/2026-11-02/report.html, and last week's folder stays beside it. To send Dana the report, send the folder: the page needs its image beside it. To have it built without you, a scheduler runs the command every Monday morning: Task Scheduler on Windows, cron on a Mac.
Check yourself
2 questions to try on the lesson page.
So what
A script that writes its charts, tables and numbers into a web page turns an analysis into a report anyone can open, rebuilt the same way every week with one command.