How I Built a 280-Page Kitchen Converter Site With a Python Static Generator and Cloudflare Pages
I recently built GramsInCup, a free grams-to-cups converter for home bakers. It looks like a simple calculator, but getting the numbers right and keeping ~280 pages fast and cheap to host turned into a fun little enginee
I recently built GramsInCup, a free grams-to-cups converter for home bakers. It looks like a simple calculator, but getting the numbers right and keeping ~280 pages fast and cheap to host turned into a fun little engineering project. Here is how it works.
The problem: one number doesn't fit all ingredients
Most quick converters treat a cup as a fixed weight. That breaks baking, because a cup is a volume:
- 1 cup of all-purpose flour β 125 g
- 1 cup of granulated sugar β 200 g
- 1 cup of honey β 340 g
So the core data is a list of ingredients, each with its own grams-per-cup value (based on the US cup, 236.588 ml):
INGREDIENTS = [
# key, name, category, grams per US cup
("all-purpose-flour", "All-purpose flour", "Flours & starches", 125),
("granulated-sugar", "Granulated sugar", "Sugars & sweeteners", 200),
("butter", "Butter", "Fats & oils", 226.8),
("honey", "Honey", "Sugars & sweeteners", 340),
# ... 48 ingredients in total
]
Everything else (tablespoons, teaspoons, ml, ounces) is derived from that one value.
No framework, just a Python build script
The whole site is generated by a few plain Python files:
-
data.py: ingredient densities and unit definitions -
build.py: renders the main tool pages, the shared header and footer, the sitemap and robots.txt -
programmatic.py: generates one page per ingredient (for example "flour grams to cups") -
blog.py: turns Markdown posts into HTML
There is no React, no bundler and no database. Each page is a string of HTML written to a dist folder. A full build takes a couple of seconds, and every page ships one small CSS file and one small JS file.
The converter itself is a short vanilla JS file. The build script injects the ingredient list into it as JSON, so the browser and the server-rendered tables always use the same data.
The rounding bug I didn't expect
The server-rendered tables (Python) and the live converter (JavaScript) disagreed on some values. The cause: Python's round() uses banker's rounding, so round(62.5) gives 62, while my JS used normal half-up rounding and showed 63.
The fix was a tiny helper so both sides round the same way:
import math
def rhu(x, nd=0):
"""round half up, like JavaScript Math.round"""
m = 10 ** nd
return math.floor(x * m + 0.5) / m
I also wrote a small Playwright script that loads every URL in the sitemap and checks that the numbers in the HTML match what the JS calculates. It catches this kind of drift before it goes live.
Showing cups the way people measure
Nobody measures "0.8 cups". The converter snaps results to kitchen fractions (β , ΒΌ, β , β , Β½, β , β , ΒΎ, β ) and shows the exact decimal next to it, for example: 100 g of flour = ΒΎ cup (0.8 exactly).
Hosting: Cloudflare Pages, deployed from GitHub
The code lives in a GitHub repo. Cloudflare Pages runs the Python build on every push and serves the static output from its CDN. For a static site the free plan is plenty: unlimited bandwidth and 500 builds a month.
Blog posts are Markdown files edited through a Git-based CMS. Posts with a future date are skipped at build time, and a small GitHub Actions cron job makes an empty commit on the publish date, which triggers a rebuild and makes the post go live. That gives scheduled posts on a static site with no server.
An embeddable widget
I also made a tiny standalone converter page that other sites can embed with an iframe. It is a single HTML file with inline CSS and JS and no external requests, so it loads fast inside someone else's page.
What I'd do differently
- Write the data-consistency test on day one instead of after the rounding bug.
- Keep every number in one data file from the start. It made adding new ingredient pages trivial later.
If you're building a small tool site, a plain Python generator plus a free static host goes a surprisingly long way. Happy to answer questions about any part of the setup in the comments.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.