Scrollycoding on a page without a table of contents
This page shows scrollycoding on a page that has no table of contents. The text stays in Starlight’s content column. The block below has lines too long for that column, so it spreads over the free space on each side. Then it shows the steps beside the code. Make the window narrower to see each step get its own copy of the block.
load_orders takes the path of a CSV file and returns one dict for each row.Code from this step on:
import csv
from collections import Counter
from pathlib import Path
def load_orders(path: Path) -> list[dict[str, str]]:
with path.open(newline="", encoding="utf-8") as fh:
return [row for row in csv.DictReader(fh) if row["status"]]
with path.open(newline="", encoding="utf-8") as fh:newline="" lets the CSV reader handle the line ends.
with path.open(newline="", encoding="utf-8") as fh:Rows with no status are left out.
with path.open(newline="", encoding="utf-8") as fh:count_by_status counts the orders for each status.Code from this step on:
import csv
from collections import Counter
from pathlib import Path
def load_orders(path: Path) -> list[dict[str, str]]:
with path.open(newline="", encoding="utf-8") as fh:
return [row for row in csv.DictReader(fh) if row["status"]]
def count_by_status(orders: list[dict[str, str]]) -> Counter[str]:
return Counter(order["status"] for order in orders)
with path.open(newline="", encoding="utf-8") as fh:
Counter comes from the standard library.
with path.open(newline="", encoding="utf-8") as fh:
with path.open(newline="", encoding="utf-8") as fh:
with path.open(newline="", encoding="utf-8") as fh:
The text after the block is in the content column again.