Skip to content

Diagrams and images

Diagrams are inline SVG generated by a small Python script that uses scripts/diagrams.py. No diagram library runs in the browser, the drawings follow the deck's light and dark themes, and they stay sharp at any size.

Which diagram for which message

The message is... Draw Helper
who uses the system and what it depends on context diagram (C4 level 1) Dg + node(tag=...)
what runs where, what talks to what container diagram (C4 level 2) Dg + boundary, cyl shapes for stores
the main modules inside one part component diagram (C4 level 3) Dg, one boundary for the container, stores outside it
stages happen in this order flow / pipeline flow()
who calls whom, in what order, for one request sequence diagram Seq
a thing moves between named states state machine Dg + node(shape="pill") + link
the records and how they refer to each other data model Dg + entity()
the classes behind one decision class view (C4 level 4, use sparingly) Dg + entity(stereo=...)
the same pipeline differs per mode variant toggle variant_toggle()
levels zoom into each other panels Dg.panel(zoom_from=...) + mini()

One diagram answers one question. If you need the word "and" to say what a diagram shows, split it. Structure (boxes that exist) and behaviour (things that happen in order) go in different diagrams.

scripts/example_diagrams.py has a working example of every row; copy the closest one and change it.

Writing the figures script

Create src/figures.py next to the deck source, with diagrams.py copied beside it:

import sys
sys.dont_write_bytecode = True   # before importing diagrams: keeps __pycache__ out of the project
from diagrams import Dg, Seq, flow, row, save_figs

def context():
    g = Dg("ctx", 1200, 440, "Sentence describing the diagram for screen readers.")
    g.node("user", 0, 60, 220, 104, "Analyst", ["uploads files", "reads reports"], tag="PERSON", kind="person")
    g.node("sys", 400, 46, 400, 132, "Report service", "files in → reports out", tag="SOFTWARE SYSTEM", kind="system")
    g.link("user", "sys", "POST /reports", kind="accent")
    return g.render(maxh=540)

if __name__ == "__main__":
    save_figs({"context": context()}, sys.argv[1] if len(sys.argv) > 1 else "figs.json")

Each key fills the FIG:key marker of the same name in the deck source. figs.json is a generated file.

Layout warnings: what they cover and what they do not

save_figs prints layout warning: lines. Fix every one, but treat a clean run as necessary, not sufficient.

Checked:

  • a node title, sub-line or tag wider than its box, or more text lines than the box height holds (node, entity rows, mini labels, variant_toggle stages);
  • a node, boundary, panel, edge or edge label outside the canvas;
  • an edge or link that runs through a box it neither starts nor ends at (boxes made with node, entity, or mini(id=...));
  • sequence lanes closer together than their headers, lane titles too long for the header, and a message label wider than the gap between its two lanes;
  • unknown kind names, and frame() used without render(maxh=...).

Not checked, so look at the rendered slide for these:

  • two boxes overlapping each other;
  • an edge label (pill) or free text() sitting on a box, on an arrow or on another label;
  • arrows crossing each other, or two arrows drawn on top of each other;
  • an arrow that passes through its own start or end box on the way out;
  • anything involving mini() boxes without an id, Seq.self_ labels versus neighbouring lanes, notes, bands, and variant_toggle routing;
  • whether the text is large enough once the figure is scaled to the slide.

Widths are estimates from character counts, so a label within a few pixels of the limit can still touch its box.

API summary

These are the actual signatures in scripts/diagrams.py.

Dg(pid, W=1200, H=None, aria="") - a free-form canvas. pid is a short id unique per figure in the deck; H=None fits the height to the content; aria is the screen-reader sentence.

Method Draws
node(id, x, y, w, h, title, sub=(), tag=None, kind="code", shape="rect", dash=False) labelled box; sub is a string or list of lines; shapes rect, cyl, hex, pill; dash=True for optional parts
entity(id, x, y, w, name, rows, stereo=None, caption=None, kind="code") class / table box with left-aligned rows; height is derived from the rows and returned
mini(x, y, w, h, label="", kind="code", hl=False, shape="rect", id=None) small unshadowed box for thumbnails; hl=True thick outline; give an id to use it with link / p and to include it in the arrow check
link(a, b, label=None, kind="ink", dash=False, lp=None, both=False, shift=0) arrow between two boxes, routed automatically; shift moves a straight arrow sideways (parallel request / reply)
edge(pts, label=None, kind="ink", dash=False, lp=None, both=False) arrow through explicit (x, y) points; lp=(x, y) places the label; both=True adds a second arrowhead
p(id, side, off=0) anchor point on a box side (l, r, t, b), slid along the side by off, for use in edge
boundary(x, y, w, h, label) dashed group outline with a caps label in its top-left corner
panel(x, y, w, h, head=None, title=None, caption=None, zoom_from=None) framed sub-canvas; zoom_from=(x, y, w, h) draws a zoom wedge from that rectangle
pill(at, label, kind="ink", fill="bg") mono label on a background pill centred at at; fill is bg, c1, c2, c3 or none
text(x, y, s, cls="lab", anchor="middle") free text; classes lab (+ lab-c1 / lab-c2 / lab-c3 / lab-accent), tg, t, s, note-t
step(index=None) context manager: what is drawn inside appears as the next step (see animations.md)
gopen(cls="fragment", attrs="") / gclose() the low-level group behind step()
frame(did, x, y, w, h, role, pad=4) zoom frame for auto-animate; role is "depart" or "arrive"; pad grows the frame around the rectangle (use pad=0 for a whole-canvas arrive frame)
render(svg_cls="", svg_attrs="", fig_cls="", maxh=None) returns the figure HTML; fig_cls="r-stretch" fills the slide's free height; maxh=540 caps the height in px (required with frame)

flow(pid, steps, aria, W=1200, per_row=None, box_h=72, gap=24, y0=24, row_gap=64, H=None) - a left-to-right pipeline. steps is a list of (id, title[, sub[, kind]]). Boxes share the row width equally (gap between them), start at y0, are box_h high, and wrap after per_row with row_gap between rows. Returns the Dg, not yet rendered, so you can add stores or steps below and then call .render(). Pass H=300 for a flowstack slide.

Seq(pid, lanes, aria, W=1200, gutter=150, steps=True) - a sequence diagram. lanes is a list of (key, title[, sub]); gutter is the left margin kept for band tags; steps=False makes it static.

Method Draws
m(a, b, label, ret=False, kind="ink") message from lane a to lane b; ret=True dashed reply; label a string or a list of two lines
self_(a, label, kind="ink") lane a does something on its own
note(a, b, label) italic remark spanning lanes a to b
band(kind, tag) open a region: kind="box" dashed outline (alt / opt / loop), or "c1", "c2", "c3", "accent" for a tinted background; tag a string or list of lines
els(tag) divider inside a box band: the alternative branch
end() close the innermost band
render(fig_cls="r-stretch") returns the figure HTML; stretches to the slide's free height by default

variant_toggle(pid, stages, variants, aria, all_head="", legend="", cols=6, W=1200) - returns the figure HTML directly. stages is a list of (title, sub, states); variants up to three (name, headline) pairs; states are on, key, off, opt (see animations.md).

Helpers: row(n, w, gap=None, x0=0, W=1200) and centred(n, w, gap, W=1200) return x positions for a row of boxes; text_w(s, per_char=6.95) estimates a mono label's width; save_figs(figs, path, strict=False) writes the JSON and prints the warnings (strict=True exits with status 1 if there are any).

Node kinds: code (neutral), system (the thing in scope, highlighted), container, person, ext (external), store, sibling (dashed, someone else's system), c1 / c2 / c3 (category colours). Edge kinds: ink, accent, c1, c2, c3.

Layout rules

Canvas. Draw on a 1200-unit-wide canvas. On the 1280 x 760 stage one unit is close to one pixel. Height budget:

Slide Canvas height How it is sized
diagram alone under the headline up to 540 render(maxh=540), or Seq (stretches by itself)
diagram + one row of text or path labels up to 440 to 480 render(maxh=540) or plain render()
diagram above a bullet list (.flowstack) 300 plain render(); capped at 300px by the template
two diagrams side by side (.cols) canvas 600 wide, up to 330 high plain render()

A canvas much taller than the budget is shrunk to fit, and its text shrinks with it. If text looks small, the canvas is too big: reduce the content, do not shrink the font.

Boxes. Typical sizes: 170 to 270 wide, 70 to 112 high. A title holds about width / 8 characters and a sub-line about width / 7; heed the layout warnings. One or two sub-lines per box, three at most. Keep a gap of at least 24 units between boxes, 60 or more where an arrow carries a label.

Grid. Align boxes in rows and columns (row(), centred()); the eye reads misalignment as meaning. Put the main flow on one horizontal line, left to right; put what it depends on underneath.

Arrows. Use link first. Use edge with explicit points when two arrows would overlap, when an arrow must go around a box, or for a parallel request / reply pair. Arrows should not cross boxes and should cross each other as little as possible; rearranging boxes usually removes a crossing. Solid = a call or a dependency, dashed = a reply or something optional; state this once in a notation slide.

Labels. Every arrow that is not obvious gets a verb label (reads, enqueue, POST /jobs), three words at most, or two short lines as a list. If the automatic position collides with something, set lp=(x, y). Every box says what kind of thing it is: with a tag (PERSON, CONTAINER, DATA STORE, EXTERNAL) in architecture diagrams, or by a consistent shape and colour.

Sequence diagrams. Three to six lanes, at most about 16 messages. Lane titles of at most 18 characters.

A message label must fit between its two lanes, or it runs over the sender's number badge. With the default width and gutter, the characters available per line are:

Lanes Adjacent lanes Two lanes apart Three lanes apart
3 56 115 -
4 36 76 115
5 26 56 85
6 20 44 68

(In general: lane gap = (W - gutter - 190) / (lanes - 1) px; characters = (gap x lanes spanned - 26) / 7.2.) For a longer label pass a list of two lines, shorten it to a verb and an object, or move detail to the speaker notes. self_ labels have about 50 characters before they reach the next lane's text.

Group conditional parts with band("box", "CACHED") ... els("NOT CACHED") ... end(), and parts belonging to a category with band("c2", "ASYNC"). Split a longer conversation into two slides at a natural hand-off.

Density. About 12 boxes is the limit for a full-slide diagram, 6 for a flowstack diagram. More than that: split by zoom level, or use steps so the audience meets the boxes a few at a time.

Colour and contrast

  • Never write a colour value in a diagram. Pick a kind; the CSS tokens give it a fill, outline and text colour in both themes. A hard-coded colour breaks dark mode.
  • The three category colours carry one meaning per deck (three modes, three subsystems, sync / async / batch). Decide it in the outline, show it in a legend, and use the same mapping in cards, tags, tables and diagrams. If the project has no three-way split, use code, system, ext and store only; a mostly neutral diagram with one highlighted box reads better than a rainbow.
  • Do not rely on colour alone: pair it with a label, a tag, a dashed outline or a shape, so the diagram still works in greyscale and for colour-blind readers.
  • system (accent fill, thicker outline) marks the single thing the slide is about. Use it once or twice per diagram.
  • Write the aria sentence as the diagram's message in words; it is what a screen reader announces.

Screenshots and other raster images

Use a real screenshot when the message is "this is what it looks like": a UI, a CLI session, a dashboard, a report. Do not redraw a UI as boxes, and do not use a screenshot of someone else's architecture diagram where a drawn diagram would be clearer and themeable.

<figure class="shot">
  <img src="img/report-page.png" alt="The report page: a summary table above three charts.">
  <figcaption>The report page. The summary table is what most users read.</figcaption>
</figure>
  • Where to put the file: under src/ (for example src/img/). The path in src is relative to the deck source file.
  • Embedding: by default build.py converts every local <img src> into a base64 data: URI, so index.html stays a single self-contained file that can be mailed or published as it is. This is the right choice in nearly every case.
  • Relative paths instead: build with --no-inline-images when images are many or large (the deck would grow past a few MB). The paths are then left untouched and must resolve from the built file's location, so the image folder has to travel with index.html. The wrapper-less artifact.html generally needs embedded images.
  • Size and format: capture at 1600 to 2000 px wide and keep each file under about 400 KB (PNG for UI and text, JPEG for photos, SVG when available). A full-slide image is shown at most 520px high; in a column, 420px.
  • Cropping: crop to the region that matters and say in the caption what to look at. Remove personal data, tokens and internal hostnames before capturing.
  • Alt text is required: one sentence describing what is shown.
  • class="shot plain" drops the border and shadow, for images that have their own background or are transparent.
  • Screenshots do not follow the dark theme. Prefer light-mode captures; the border keeps them readable on a dark slide.

Remote images (https://...) are left as links and need a network connection when the deck is shown; prefer a local copy.


Source

This page is skills/project-deck/references/diagrams.md, the guide the skill reads while it works. Edit it there; the documentation includes it as is.