# Exercise: Food Truck Weekly Report

Write the six functions of a food truck's weekly report script, which find the newest week's sales file, load it, sum up its headline numbers, chart the items sold, write the report as a web page in a folder for that week, and run it all from one command with argparse options, and let pytest check the numbers and the files it writes.

Journeyman · Python · Updated November 19, 2026

By Gregor Dzierzon

The Greenway Food Truck gets a new file of sales every Monday, and Dana, who owns it, wants the same report each time: last week's sales, how many items sold, the best-selling item, the best place to park, and a chart. You'll write the script that builds that report as a web page, from whichever week is newest, with one command. Tests call each of your functions, run the whole script on their own copy of the data, and check the numbers and the files it writes. A notebook comes with the exercise for trying your functions as you go.

## Get the exercise code

Open this lesson's GitHub repository, [github.com/ask-codey/exercise-data-food-truck-weekly-report](https://github.com/ask-codey/exercise-data-food-truck-weekly-report), also linked from the **Exercise** box beside the lesson. Clone it into your `python-course` folder. In VS Code's terminal:

```bash
cd ~/python-course
git clone https://github.com/ask-codey/exercise-data-food-truck-weekly-report.git
```

Git makes a folder, `exercise-data-food-truck-weekly-report`, with the exercise code inside. Open that folder in VS Code (**File > Open Folder**), open its terminal with **Terminal > New Terminal**, and make the exercise's virtual environment. On Windows (PowerShell):

```powershell
python -m venv .venv
.venv\Scripts\Activate.ps1
pip install -r requirements.txt
```

On a Mac (zsh):

```bash
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
```

The prompt starts with `(.venv)` once the virtual environment is on. The exercise's README says what each command does, and what to do if PowerShell won't run the second one. Then make the data, once:

```bash
python seed.py
```

```text
Wrote data/incoming/week-2026-09-07.csv: 409 sales.
Wrote data/incoming/week-2026-09-14.csv: 422 sales.
Wrote data/incoming/week-2026-09-21.csv: 412 sales.
Wrote data/incoming/week-2026-09-28.csv: 409 sales.
Wrote data/incoming/week-2026-10-05.csv: 411 sales.
Wrote data/incoming/week-2026-10-12.csv: 403 sales.
Wrote data/incoming/week-2026-10-19.csv: 416 sales.
Wrote data/incoming/week-2026-10-26.csv: 390 sales.
```

The folder holds:

- `seed.py`: makes eight weeks of the truck's sales in `data/incoming/`, one file a week, named by the Monday it starts on, our own made-up data. Each row is one sale: `sold_at`, `location` (City Park, Office Row or Stadium), `item`, `quantity`, `price` (for one item) and `payment`. `python seed.py --next` adds the week after the newest, as next Monday's file arriving.
- `weekly_report.py`: the six functions, for you to finish.
- `explore.ipynb`: a notebook for looking at the weeks and trying your functions. Its first cell turns on autoreload, so each change you save in `weekly_report.py` is used the next time you run a cell. Choose the `.venv` kernel with **Select Kernel** (top right) before you run it. The tests don't read it.
- `tests/`: the tests, which call each function, run the script, and check what comes back and the files written, and `conftest.py`, which gives every test its own fresh copy of the eight weeks. You don't need to change them.
- `requirements.txt` and `pytest.ini`: the packages the exercise needs, at exact versions, and where pytest finds the tests and your code.

## What to do

Every function in `weekly_report.py` is already declared with its name and parameters, and for now it stops with a `NotImplementedError` that says *Not built yet.* The docstring under each one says what it should do, with examples:

```python
def newest_week_file(folder):
    """Return the Path of the newest week-*.csv file in folder (a str or a Path).

    The files are named by the Monday their week starts on, year first, so sorting the names
    puts them in date order and the last one is the newest:
    week-2026-09-07.csv, week-2026-09-14.csv, ... week-2026-10-26.csv -> week-2026-10-26.csv

    If folder holds no week-*.csv file, raise FileNotFoundError with a message that names the
    folder, for example: No week-*.csv file in data/incoming
    """
    raise NotImplementedError("Not built yet.")
```

In each function, replace the `raise NotImplementedError("Not built yet.")` line with your own code:

1. **`newest_week_file(folder)`** returns the `Path` of the last `week-*.csv` file by name: `week-2026-10-26.csv` of the eight seeded weeks. A file such as `notes.csv` doesn't count. In a folder with no week file, it raises `FileNotFoundError` with a message that names the folder.
2. **`load_week(path)`** reads one week's file with `sold_at` as dates and times, and adds a `total` column, quantity times price: a sale of 2 at 9.00 has a total of 18.0. The seeded `week-2026-09-07.csv` has 409 sales adding up to 4653.5.
3. **`summarize(df)`** returns a dictionary with four keys: `sales` (the sum of `total`, rounded to 2 places), `items_sold` (the sum of `quantity`), `best_item` (the item with the most sold) and `best_location` (the location with the most sales). A tie goes to the name first in alphabetical order. Worked through for three sales, Elote ×1 at City Park ($4.50), Street Tacos ×2 at the Stadium ($18.00) and Elote ×2 at City Park ($9.00): the sales are 4.50 + 18.00 + 9.00 = 31.5; the items sold are 1 + 2 + 2 = 5; Elote sold 3 and Street Tacos 2, so the best item is Elote; City Park took $13.50 and the Stadium $18.00, so the best location is Stadium. The answer is `{"sales": 31.5, "items_sold": 5, "best_item": "Elote", "best_location": "Stadium"}`.
4. **`items_chart(df)`** returns `(fig, ax)`: a bar chart with one bar per item, its height the number sold, largest first, titled *Items sold this week*, with the x-axis label *Item* and the y-axis label *Number sold*.
5. **`write_report(df, out_dir)`** makes `out_dir` if it isn't there, saves the chart there as `items.png` and closes it, and writes `report.html`, then returns that file's `Path`. The page shows the four numbers in the exact lines the docstring gives (for the three sales above, `Sales: $31.50`, `Items sold: 5`, `Best-selling item: Elote`, `Best location: Stadium`), the image tag the docstring gives, and the table `to_html(index=False)` makes from each location's sales, largest first.
6. **`main(argv)`** reads two options from `argv` with argparse, `--data` (default `data/incoming`) and `--out` (default `reports`), builds the newest week's report in a folder named by its date (`reports/2026-10-26/`), prints six lines in the form `N) name: value`, and returns the report's `Path`.

## Check your work

Run the tests from the exercise folder, with the virtual environment on:

```bash
python -m pytest
```

At first all 19 tests fail. That's expected: each function you finish turns its tests green. A failing test's message says what it called, what it expected and what came back, for example *newest_week_file(folder).name should give 'week-2026-10-26.csv', but it gave 'week-2026-09-07.csv'.*

## Hints

- **Try it in the notebook first.** `explore.ipynb` already loads a week and imports your module with autoreload on, as *Using Your Module in a Notebook: autoreload* shows. When a line works there, move it into the function.
- **The newest file is the last one by name.** `sorted(Path(folder).glob("week-*.csv"))`, then `[-1]`, as *A Report Script That Finds the Newest Data* does. Check for an empty list before you take `[-1]`.
- **Group, sum, then pick the largest.** `df.groupby("item")["quantity"].sum().idxmax()` gives the item with the most sold.
- **Save, then close.** `fig.savefig(path)` and `plt.close(fig)`, as *Writing the Report: Charts and Tables on a Web Page* shows, with `f"{sales:,.2f}"` for the money and `to_html(index=False)` for the table.
- **main reads the list it's given.** `parser.parse_args(argv)`, never `sys.argv`, so the tests can hand it their own folders. `Path(args.out) / week_date` is the week's folder.

## When you're done

Every test is green: pytest's last line says `19 passed`. And `python weekly_report.py` prints:

```text
1) week: 2026-10-26
2) sales: 4455.0
3) items sold: 537
4) best-selling item: Street Tacos
5) best location: Stadium
6) report: reports/2026-10-26/report.html
```

Open `reports/2026-10-26/report.html` in your browser. Then let next Monday's file arrive and build again:

```bash
python seed.py --next
python weekly_report.py
```

The script finds `week-2026-11-02.csv` by itself and writes `reports/2026-11-02/report.html`, beside last week's report. Look at the two pages side by side: what changed from one week to the next, and what would Dana need on the page to see it without opening both?

## Submit your exercise

The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here. *Hand In Your Work with Git* walks through each step.

1. On GitHub, click **+**, then **New repository**. Name it (for example `exercise-data-food-truck-weekly-report`), choose **Public**, and click **Create repository**. Leave out the README: your folder already has one.
2. In VS Code's terminal, in your exercise folder, send your work to your new repo. Point your copy at your own repo, then commit and push:

   ```bash
   git remote set-url origin https://github.com/your-name/exercise-data-food-truck-weekly-report.git
   git add .
   git commit -m "My solution"
   git push -u origin main
   ```

3. Paste your repo's address, such as `https://github.com/your-name/exercise-data-food-truck-weekly-report`, into **Submit your exercise** on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.

Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.

## So what

A report that one command rebuilds from the newest file is a report you can trust every week: the same steps, in the same order, whoever runs it and whenever the data arrives.

## Key terms

- [glob()](https://www.ask-codey.com/glossary/glob-python)
- [argparse](https://www.ask-codey.com/glossary/argparse)
- [savefig()](https://www.ask-codey.com/glossary/savefig)
- [to_html()](https://www.ask-codey.com/glossary/to-html)
- [f-string](https://www.ask-codey.com/glossary/f-string)
- [pytest](https://www.ask-codey.com/glossary/pytest)

## Related

- [From Notebook Cells to Functions](https://www.ask-codey.com/lessons/data-cells-to-functions)
- [Using Your Module in a Notebook: autoreload](https://www.ask-codey.com/lessons/data-module-in-a-notebook)
- [A Report Script That Finds the Newest Data](https://www.ask-codey.com/lessons/data-report-script)
- [Writing the Report: Charts and Tables on a Web Page](https://www.ask-codey.com/lessons/data-writing-the-report)

Source: https://www.ask-codey.com/lessons/exercise-data-food-truck-weekly-report
