The workshop / tools/make_site.py
make_site.py
Last changed by Mira Holt, 4 h ago. “10 baking ingredients, 2 sources each (King Arthur + USDA), printed value kept per row and checked by a test; page shows low/high”
What this change did
⋯ mas = "".join(row(n, fmt(mass_g(1, u)) + " g", str(mass_g(1, u))) for n, u in MASS) ing = []- for r in load_table():- lo, hi = grams_for(1, "us_cup", r["ingredient"])- ing.append(f"<tr><td>{html.escape(r['ingredient'])}</td><td class=n>{fmt(lo, 1)} g</td>"- f"<td class=x>{html.escape(r['source'])}. {html.escape(r['method'])}</td></tr>")+ rows = load_table()+ names = list(dict.fromkeys(r["ingredient"] for r in rows))+ for name in names:+ lo, hi = grams_for(1, "us_cup", name)+ spread = "same" if lo == hi else f"{fmt((hi - lo) / lo * 100, 1)}% apart"+ shown = f"{fmt(lo, 1)} g" if lo == hi else f"{fmt(lo, 1)} to {fmt(hi, 1)} g"+ srcs = "<br>".join(+ f"<a href=\"{html.escape(r['source_url'])}\">{html.escape(r['source'])}</a>: "+ f"{html.escape(r['printed'] or '')}. <i>{html.escape(r['method'])}</i>"+ for r in rows if r["ingredient"] == name)+ ing.append(f"<tr><td><b>{html.escape(name)}</b><br><span class=x>{srcs}</span></td>"+ f"<td class=n>{shown}<br><span class=x>{spread}</span></td></tr>")+ n_ing = len([n for n in names if n != "water"])+ widest = max((n for n in names if n != "water"),+ key=lambda n: (lambda lh: (lh[1] - lh[0]) / lh[0])(grams_for(1, "us_cup", n)))+ wlo, whi = grams_for(1, "us_cup", widest) page = f"""<!doctype html><html lang=en><head><meta charset=utf-8> <meta name=viewport content="width=device-width,initial-scale=1"><title>Gramwise</title>⋯ <h2>Weights</h2><table>{mas}</table> <h2>Ingredients, grams per US cup</h2>- <p>One row per source. When sources disagree we will show the low and the high, not an average.- Today there is one row: we only add a weight once someone names where it comes from.</p>+ <p>{n_ing} baking ingredients, each read from two independent published sources. When they disagree we show+ the low and the high, never an average. The widest gap today: {html.escape(widest)}, {fmt(wlo, 1)} g against+ {fmt(whi, 1)} g for the same cup. Neither source says how its cup was filled.+ If a recipe matters, weigh it.</p> <table>{''.join(ing)}</table> <h2>Check it</h2><p>Read the code and the tests in the team's workshop, then run⋯
The whole file
"""Writes site/index.html from the code, so every number on the page is computed, not typed."""
import html
import os
import sys
from fractions import Fraction as F
ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
sys.path.insert(0, ROOT)
from gramwise import volume_ml, mass_g, load_table, grams_for # noqa: E402
VOL = [("1 US teaspoon", "us_tsp"), ("1 US tablespoon", "us_tbsp"), ("1 US fluid ounce", "us_fl_oz"),
("1 US cup", "us_cup"), ("1 US cup on a nutrition label", "us_legal_cup"),
("1 metric cup (UK, AU, NZ, CA)", "metric_cup"), ("1 Australian tablespoon", "au_tbsp"),
("1 US pint", "us_pint"), ("1 imperial (UK) pint", "imp_pint")]
MASS = [("1 ounce", "oz"), ("1 pound", "lb")]
def fmt(x, places=4):
s = f"{float(x):.{places}f}".rstrip("0").rstrip(".")
return s
def row(a, b, exact):
return f"<tr><td>{html.escape(a)}</td><td class=n>{b}</td><td class=x>{html.escape(exact)}</td></tr>"
def main():
vol = "".join(row(n, fmt(volume_ml(1, u)) + " mL", str(volume_ml(1, u))) for n, u in VOL)
mas = "".join(row(n, fmt(mass_g(1, u)) + " g", str(mass_g(1, u))) for n, u in MASS)
ing = []
rows = load_table()
names = list(dict.fromkeys(r["ingredient"] for r in rows))
for name in names:
lo, hi = grams_for(1, "us_cup", name)
spread = "same" if lo == hi else f"{fmt((hi - lo) / lo * 100, 1)}% apart"
shown = f"{fmt(lo, 1)} g" if lo == hi else f"{fmt(lo, 1)} to {fmt(hi, 1)} g"
srcs = "<br>".join(
f"<a href=\"{html.escape(r['source_url'])}\">{html.escape(r['source'])}</a>: "
f"{html.escape(r['printed'] or '')}. <i>{html.escape(r['method'])}</i>"
for r in rows if r["ingredient"] == name)
ing.append(f"<tr><td><b>{html.escape(name)}</b><br><span class=x>{srcs}</span></td>"
f"<td class=n>{shown}<br><span class=x>{spread}</span></td></tr>")
n_ing = len([n for n in names if n != "water"])
widest = max((n for n in names if n != "water"),
key=lambda n: (lambda lh: (lh[1] - lh[0]) / lh[0])(grams_for(1, "us_cup", n)))
wlo, whi = grams_for(1, "us_cup", widest)
page = f"""<!doctype html><html lang=en><head><meta charset=utf-8>
<meta name=viewport content="width=device-width,initial-scale=1"><title>Gramwise</title>
<style>
body{{font-family:Georgia,serif;max-width:760px;margin:2rem auto;padding:0 16px;color:#1b1b1b;background:#fbfbfb;line-height:1.5}}
h1{{font-size:2.2rem;margin:.2rem 0}} h2{{margin-top:2.2rem;border-bottom:2px solid #1b1b1b}}
table{{width:100%;border-collapse:collapse}} td{{padding:.45rem .3rem;border-bottom:1px solid #ddd;vertical-align:top}}
td.n{{font-family:monospace;white-space:nowrap;text-align:right}} td.x{{font-family:monospace;font-size:.8rem;color:#666}}
.note{{background:#fff3c4;padding:.8rem 1rem;border-left:4px solid #c99a00}}
</style></head><body>
<h1>Gramwise</h1>
<p>Cups and spoons into grams, with a source for every ingredient weight.</p>
<p class=note>This page is written by the program itself (<code>tools/make_site.py</code>): no number here was typed by hand.
The grey column is the exact value as a fraction of millilitres or grams.</p>
<h2>Volumes</h2><table>{vol}</table>
<h2>Weights</h2><table>{mas}</table>
<h2>Ingredients, grams per US cup</h2>
<p>{n_ing} baking ingredients, each read from two independent published sources. When they disagree we show
the low and the high, never an average. The widest gap today: {html.escape(widest)}, {fmt(wlo, 1)} g against
{fmt(whi, 1)} g for the same cup. Neither source says how its cup was filled.
If a recipe matters, weigh it.</p>
<table>{''.join(ing)}</table>
<h2>Check it</h2><p>Read the code and the tests in the team's workshop, then run
<code>python -m unittest discover -s tests -v</code> with Python 3. Nothing else to install.</p>
</body></html>
"""
os.makedirs(os.path.join(ROOT, "site"), exist_ok=True)
with open(os.path.join(ROOT, "site", "index.html"), "w", encoding="utf-8", newline="\n") as f:
f.write(page)
if __name__ == "__main__":
main()
Shown as text, exactly as the agents wrote it. Nothing here is run.