Recipes¶
Experimental
The built-in recipes, karni new --recipe, the recipe key of
.karni/build.json and karni_cli.recipes.apply are
experimental. They may change in a
minor release, and each change is in the CHANGELOG. The error codes keep
their meaning.
A recipe is a good starting spec for a kind of app many people ask for, plus
the little domain code a spec cannot express. The spec is a
karni.yaml, so Karni turns it into code without a
language model, and the domain code is a few methods, actions, seeders and
tests in the files the project owns. Each recipe also switches on the
modules and features that suit it, so a recipe doubles as a profile.
| Recipe | For | Entities | Modules and features | Domain code |
|---|---|---|---|---|
shop |
an online shop | Category, Product, Cart, CartItem, Order, OrderItem | payments, email code login, terms | 84 lines: the order total, checkout |
booking |
booking a slot of a resource | Resource, Slot, Booking | payments, email code login | 66 lines: a slot never takes more bookings than its capacity |
crm |
a sales team's deals | Company, Contact, Deal, Activity | two-factor sign-in | 15 lines: the pipeline per stage |
lms |
online courses | Course, Lesson, Enrolment, LessonProgress | email code login | 52 lines: a student's completion of a course |
marketplace |
sellers and buyers | Seller, Listing, Order, Payout | payments, two-factor sign-in, terms | 58 lines: the payout after the fee |
data-portal |
datasets and indicators by region and sector | Region, Sector, Dataset, Indicator | saved charts, per-user grants, response caching | 139 lines: the trees, datasets, indicators and the saved chart of the demo data |
The domain code counts the lines beyond what karni build writes in the
same files anyway. Each recipe also ships tests of its own rules.
karni recipes list --match "<the request>" picks the recipe for a one-line
request (an online shop, book a class, track sales deals, online courses, a
marketplace for sellers, a data portal), and karni recipes show <name> prints its spec,
its files and what people usually change. Where they come from, the
recipe.toml format and recipes of your own are in the app
spec.
What karni new --recipe does¶
- It renders the project as
karni newdoes. The kind is the recipe's (app), or--kind site+appfor a public site next to the app. - It writes the recipe's
karni.yamlwith the project's name asapp.name, the kind asapp.kindand the recipe asapp.recipe. - It names the recipe in
.karni/build.json("recipe": "shop"): the firstkarni buildthat writes puts the recipe's own files in. - It runs
karni buildin the new project. The build adds the modules, writes Karni's code and its stubs for the project's files, then the recipe's own files over those stubs, then runs the migrations, seeds the demo data with the recipe's seeders, runs the project's tests (the recipe's among them) andkarni check.
Without an interpreter that has Django and karni for the project (its
.venv, the active virtualenv, or the one running karni-cli), or without
a database it can open, it stops after step 3 and says the one command
left: karni build. That build does step 4 the same way.
The output for the shop, from an empty folder:
Created ./corner_shop: kind app (recorded under [tool.karni] in pyproject.toml), ...
Recipe shop (Online shop): wrote karni.yaml (app.recipe: shop) and named the recipe in .karni/build.json. The first karni build writes its own files over its stubs: shop/models.py, shop/seeds.py, shop/tests/test_checkout.py, shop/tests/test_seeds.py, shop/viewsets.py.
$ karni build
...
40 changes, 0 risky.
modules karni add payments
files 30 written: 21 Karni's, 9 the project's (pyproject.toml, shop/__init__.py, ...)
migrations shop/migrations/0001_initial.py
migrate applied 81 on default
seed Cart 10, Category 5, Order 30, Product 40, CartItem 30, OrderItem 80 (demo login demo@example.com / karni-password)
types frontend/src/api/types.ts and hooks.ts
test 15 passed in 8.84s, tests/openapi.snapshot.json updated (the API changed: commit it)
check ok
Built: the project matches karni.yaml.
karni new "<Name>" --recipe blank writes the blank start (the version and
the app's name) and builds nothing. An unknown recipe is refused with
C420, which names the closest recipes, and --design,
--kind site or --no-staff-app with a recipe with
C440. Nothing is written then.
How fast is it¶
Karni times what a person waits for in CI, so the speed cannot slip unnoticed.
scripts/benchmark/speed.py runs on every push. These are the times it
recorded in scripts/benchmark/speed-baseline.json
on a developer laptop (a Mac with Apple silicon, the 0.5.0 release candidate,
the median of 3 runs, SQLite, no frontend install). Other work was running on
the laptop, so read them as a slightly busy desktop, not a clean lab:
| Step | Seconds |
|---|---|
karni new --recipe marketplace, from scratch to a built project |
13.2 |
karni new --recipe booking |
13.8 |
karni new --recipe lms |
15.2 |
karni new --recipe data-portal |
16.9 |
karni new --recipe crm |
21.9 |
karni new --recipe shop (0.6.0, see below) |
53.8 |
the /new-app flow with a scripted agent, request to green karni check |
15.4 |
karni build again, no spec change |
1.08 |
karni check, cold (no cache) |
2.88 |
karni check, cached |
1.45 |
karni spec validate |
0.29 |
karni plan |
1.05 |
karni plan --questions |
0.29 |
Inside a recipe build the time goes to the project's own tests (3.5 to 6.6
seconds), the migrations (1.9 to 2.8 seconds), the demo data (1.6 to 2.5
seconds), the types (1.5 to 2.0 seconds) and karni check (2.3 to 3.6
seconds). karni plan and karni check take as long as they do because each
starts the project's Django once.
The shop row is from 0.6.0, where the shop recipe became a store on
karni-commerce. Its build took 71.1 seconds on the same laptop under more load
(the median of 3 builds), stored as 53.8 at the baseline's calibration, the way
the comparison scales a run. It spends 28.5 seconds in its tests (77 of them,
three of which seed the whole demo shop), 6.9 in its 99 migrations (commerce,
payments, storage and the family parts), 5.3 in the demo data, 5.6 in the types
and 4.1 in karni check.
Each time has a budget: the baseline (scripts/benchmark/speed-baseline.json)
times how much slower or faster the machine is than the one that recorded it
(the same calibration as the large-project benchmark), times 1.5, plus a few
seconds of slack. A build with no spec change has its own ceiling of 5 seconds
on any machine. The speed job fails when a step goes over, and keeps the times
as an artifact. Run it yourself with python scripts/benchmark/speed.py, and
record a new baseline on a quiet machine with --record when a growth is
expected.
The calibration is pure Python, and the builds are not: relative to it, the
builds and the karni commands ran 1.1 to 2.1 times slower on GitHub's Linux
runner than on that laptop. So the CI job compares with
scripts/benchmark/speed-baseline-ci.json, the times of one CI run (the job's
speed artifact), and the laptop's baseline stays for runs on a laptop. A
change that slows a step on purpose records both: --record on the laptop, and
the CI job's speed artifact copied to speed-baseline-ci.json.
The stub rule¶
karni build creates the project's own files (<app>/models.py,
viewsets.py, seeds.py) as stubs: a docstring, an import and a class per
entity that extends Karni's. A recipe's file replaces such a file only
while it is still exactly that, and creates a file the project does not
have (its own tests). A file with code of the project's in it stops the
build with C441, naming each file, and nothing is
written. A recipe's file never goes where Karni writes its own code
(<app>/karni/, <app>/tests/karni/, the karni:spec blocks): such a
recipe cannot be used (C421).
When karni.yaml has an entity the recipe does not (added before the first build), build's stub class for it stays, after the recipe's code.
After the first build¶
The recipe's files are the project's from then on. karni build never
writes them again and only adds a class for a new entity, as it does for
any project file. Change the app in karni.yaml (each recipe's page says
how for what people usually change), then run karni build. The recipe's
domain code is written for its entities and fields: after a rename, move
the methods that name the old one.