Skip to content

Zero Rects, Zero Edges: The Groups Never Reached the SVG

Adityo Guni Waluyo

A generated diagram file opened fine but held zero rects and zero edges. The cause was an attach contract that nothing was checking.

TL;DR

While finishing an architecture report, the author found SVG files with groups but zero shapes. The drawsvg scripts created drawable objects without appending them to the drawing. Fixing each call and verifying with element counts and pixel probes caught missing shapes and thin arrows.

Zero Rects, Zero Edges: The Groups Never Reached the SVG

An SVG file with content and no shapes

I was in the final layout pass of the infrastructure architecture report for KotaPortal, a city public-service portal project. Seven A4 pages, 16 sections, and 2 diagrams produced by drawsvg scripts in the repo: a container topology inside the host frame, and a release flow of 6 steps with a two-layer rollback. I opened the topology file to check whether the labels still fit their boxes.

The file was not empty. It had a root element, <g> group tags, and a full page of XML.

The shape count, though, was zero. A grep for rect came back with nothing, and the same search for the edge paths came back with nothing either. Zero rects and zero edges in a file that the SVG generator had just written, with no errors on the way out.

The working assumption, and the one I started from, was simple: if the SVG file exists and contains groups, the diagram must render. The scripts looked correct as well. Each helper call sat on its own line and read exactly like a drawing command.

Creating a drawable object is not drawing it

drawsvg is a Python 3 library for programmatically generating SVG images and animations that can render and display your drawings in a Jupyter notebook or JupyterLab [5]. Its own example builds shapes first and then attaches them: r = draw.Rectangle(...), then d.append(r) [5]. Attaching is the step that draws.

Most of the shapes in these scripts were never attached.

The helpers return drawable objects. In the generators, those results went into local variables, and a local name holds an object, not drawing content. The arrow lines were worse: a bare call like arrow((380, 66), (380, 110), "HTTP(S)") evaluates, returns a drawable, and Python drops the value. The docs describe exactly this case: an expression statement evaluates the expression list [7], and such statements usually call procedures, functions that return no meaningful result and return None in Python [7]. A call that returns a drawable has the same shape from Python's side. The result is computed and thrown away.

The <g> groups that did survive into the file do not change this. MDN describes <g> as a container used to group other SVG elements [6]. A container with no children renders nothing.

# Before: only the top box was attached; the rest became locals or bare calls
dw = d.Drawing(W, 470)
dw.append(node(270, 20, 220, 46, "Peramban"))
fe = node(60, 210, 280, 62, "Frontend")
api = node(430, 210, 280, 62, "Backend API")
arrow((380, 66), (380, 110), "HTTP(S)")

# After: every helper call is wrapped in dw.append(...)
dw = d.Drawing(W, 470)
dw.append(node(270, 20, 220, 46, "Peramban"))
dw.append(node(60, 210, 280, 62, "Frontend"))
dw.append(node(430, 210, 280, 62, "Backend API"))
dw.append(arrow((380, 66), (380, 110), "HTTP(S)"))

So the before-state had exactly one append, the browser box at the top. The frontend and backend boxes ended as locals named fe and api, and every arrow was a bare statement. The fix was mechanical: wrap each call, in all 3 generators, because they shared one mechanism rather than three separate bugs. All SVGs were regenerated after that.

Checks for parts the eye cannot confirm

Regeneration on its own is not verification. All SVGs were rebuilt, reports 4-6 were re-printed with the new diagrams, and the check ran in two passes.

The first pass was a grep census paired with a vision pass: count rects, texts, and paths in each file, then compare against what the code appends. That caught the missing elements immediately and named the cause.

It could not confirm the edges.

Diagram arrows are thin hairlines: stroke-width 1.1, colour #6c757d on white. At that weight, the lines fall below what a vision pass can reliably confirm, so the check moved to the pixels. A probe on the 150 dpi raster counts grey pixels along the expected arrow path. If the count is zero, the edge is missing even though the code clearly says dw.append(arrow(...)).

Two levels of mechanical check, then. Element-level counts catch attachment mistakes before anything renders. Pixel-level probes catch what is too thin for anyone to see.

A code review would not flag this either, because a helper call that creates an object looks exactly like a command that draws it. Created is not used, and generated output deserves a check that counts the result.

Sources

Related articles