web.py¶
clew/wiki/web.py
The repository web: every function in one zoomable, walkable view (round 4, R3).
_index_requirements
list[str] clew.wiki.web._index_requirements(list[dict] nodes, dict facts)
Give each implementing node indices into one sorted table of requirement ids.
Source · 16 lines
def _index_requirements(nodes: list[dict], facts: dict) -> list[str]:
"""COMPACT: one id table and small integers per node, rather than the id strings repeated on
every function that implements them (P5.1's weight budget; clew's own map has ~3,000 nodes).
@brief Requirement overlay data.
@return Ids.
"""
table = sorted({r for p in facts.values() for r in p.get("reqs") or ()})
at = {r: i for i, r in enumerate(table)}
for node in nodes:
reqs = (facts.get((node["name"], node["file"])) or {}).get("reqs")
if reqs:
node["r"] = [at[r] for r in reqs]
return table
Activity · 7 steps
Requirements
minimap_html
str clew.wiki.web.minimap_html(dict[str, Any] graph, list[str] order)
The sidebar's labelled block list: the web's blocks, in its order, each a link to it.
Source · 21 lines
def minimap_html(graph: dict[str, Any], order: list[str]) -> str:
"""ROUND 4, R2 (owner: the unlabelled sidebar boxes were "easier to simply click into map"):
one labelled row per block with its size, ordered as the web draws them, so a block's link
(`#b=<index>`) lands on that block. ONE STRING FOR EVERY PAGE: a page lights its own blocks
with a CSS rule on `[data-comp]`, so the markup never varies.
@brief Labelled minimap.
@return HTML.
"""
blocks = _ordered(graph["components"], order)
if not blocks:
return ""
top = max(b.get("functions", 0) for b in blocks) or 1
rows = "".join(
f'<a href="@MAP@#b={i}" data-comp="{escape(b["id"])}" title="{escape(b["id"])}">'
f'<span class="mm-name">{escape(b["id"])}</span>'
f'<span class="mm-size" style="--w:{max(4, round(100 * b.get("functions", 0) / top))}%">'
f'</span><span class="mm-n">{b.get("functions", 0)}</span></a>'
for i, b in enumerate(blocks)
)
return f'<nav class="minimap" aria-label="System blocks">{rows}</nav>'1 more lines, not shown
Activity · 6 steps
Requirements
web_data
dict[str, Any] clew.wiki.web.web_data(Path db, dict[str, Any] graph, list[str]|None order=None, int max_ring=MAX_RING, bool owners=True)
The web's data: blocks, functions with positions, edges, block links.
Called by _data, web_page
Tested by
Calls _block_row, _boundary, _edges, _file_links, _first_owners, _index_requirements, _ordered, _place, _rings, _wedges, connect
Source · 53 lines
def web_data(
db: Path,
graph: dict[str, Any],
order: list[str] | None = None,
max_ring: int = MAX_RING,
owners: bool = True,
) -> dict[str, Any]:
"""@brief Compute the web. @return Data."""
## THE OWNER'S RULING (round 5): vendor sits OUTSIDE the boundary. First-party blocks share the
## whole circle; each vendor area is one node beyond the outer ring, and calls into it end there.
every = _ordered(graph["components"], order or [])
blocks = [b for b in every if not b.get("vendor")]
vendors = [b for b in every if b.get("vendor")]
index = {b["id"]: i for i, b in enumerate(blocks)}
owner = graph.get("file_block") or {}
facts = {(p["name"], p["file"]): p for p in graph.get("symbols") or []}
with connect(db) as conn:
all_funcs = _functions(conn, owner)
all_edges = _edges(conn, all_funcs)
first_owner = _first_owners(conn) if owners else {}
funcs = {r: f for r, f in all_funcs.items() if owner[f[1]] in index}
edges = {(a, b) for a, b in all_edges if a in funcs and b in funcs}
ring = _rings(funcs, edges, max_ring)
_wedges(blocks, funcs, owner)
nodes, slot, files = _place(blocks, index, funcs, owner, ring, facts)
boundary, crossing = _boundary(vendors, all_funcs, all_edges, owner, nodes, slot, ring)
## P5 (2.0.0): THE OWNERS OVERLAY: each node carries its file's first owner, unless the
## audience withholds them (`exclude_owners`), in which case no node carries one.
for node in nodes:
if node["file"] in first_owner:
node["owner"] = first_owner[node["file"]]
req_ids = _index_requirements(nodes, facts)
node_edges = sorted({(slot[a], slot[b]) for a, b in edges if a in slot and b in slot})
return {
"req_ids": req_ids,
"blocks": [_block_row(b) for b in blocks],
"nodes": nodes,
"edges": node_edges,
## Owner (2026-11-22): files as slices, and the calls between them, for the zoomed-in view.
"files": files,
"file_links": _file_links(nodes, node_edges, files),
"links": [
{"from": link["from"], "to": link["to"], "calls": link["calls"]}
for link in graph["links"]
if link.get("calls")
],
"max_ring": max(ring.values(), default=0),
"geometry": {"r0": _R0, "band": _BAND},
"boundary": boundary,
"boundary_edges": crossing,
}
Activity · 22 steps
Requirements
web_html
str clew.wiki.web.web_html(dict[str, Any] data)
The web's HTML: toolbar, canvas, side panel, inline data, and the block table.
Source · 43 lines
def web_html(data: dict[str, Any]) -> str:
"""THE DATA IS INLINE (`application/json`, never executed) and the script is local: the page
works from file://, a Pages site or an ADO wiki with nothing fetched.
@brief Render.
@return HTML.
"""
payload = json.dumps(data, separators=(",", ":")).replace("</", "<\\/")
flows = "".join(
f'<option value="{i}">{escape(f["title"])}</option>'
for i, f in enumerate(data.get("flows") or [])
)
picker = (
f'<label>Play a flow <select data-web-flow><option value="">none</option>{flows}</select>'
"</label>"
if flows
else ""
)
return (
'<div class="web" data-web>'
'<div class="web-bar"><input type="search" data-web-search placeholder="Find a function" '
## C-14 (UX review, owner Q/A 2026-11-22): zoom is one grouped control.
'aria-label="Find a function"><span class="seg"><button type="button" data-web-zoom="1.6" '
'aria-label="Zoom in">+</button><button type="button" data-web-zoom="0.625" aria-label="Zoom out">'
'−</button><button type="button" data-web-fit>Whole system</button></span>'
'<label>Lens <select data-web-lens><option value="tier">Requirements</option>'
'<option value="block">Blocks</option>'
+ ('<option value="changed">Change</option>' if _has(data, "changed") else "")
+ ('<option value="owner">Owners</option>' if _has(data, "owner") else "")
+ f"</select></label>{picker}</div>"
## Owner map notes (2026-11-22): what each colour means, with its count, for the lens.
'<div class="web-legend" data-web-legend aria-live="polite"></div>'
'<div class="web-stage"><canvas class="web-canvas" aria-label="Repository web: blocks '
'as wedges, functions on rings by call depth"></canvas>'
'<aside class="web-panel" data-web-panel hidden></aside>'
'<div class="web-tip" data-web-tip hidden></div></div>'
f'<script type="application/json" id="web-data">{payload}</script>'
## web-files.js (the zoomed-in file layer) BEFORE web.js, which calls it.
"</div>" + _table(data) + '<script src="_static/web-files.js"></script>'
'<script src="_static/web.js"></script>'
)
Activity · 4 steps
Requirements
web_page
str clew.wiki.web.web_page(Path db, dict[str, Any] graph, list[str] order, list pages, dict[tuple[str, str], str] hrefs, Path source, int max_ring=MAX_RING, bool owners=True)
Write the map page: the web, with source links and playable flows.
Called by _build_site
Calls _briefs, _flow, _with_briefs, connect, web_data, web_html
Source · 45 lines
def web_page(
db: Path,
graph: dict[str, Any],
order: list[str],
pages: list,
hrefs: dict[tuple[str, str], str],
source: Path,
max_ring: int = MAX_RING,
owners: bool = True,
) -> str:
"""A FLOW IS PLAYED BY ITS FUNCTIONS IN CALL ORDER: the camera visits each step; a step whose
function is not drawn (test code, or outside the index) is skipped, never invented.
@brief Write map.md.
@return Page name.
"""
if not graph["components"]:
return ""
data = web_data(db, graph, order, max_ring, owners)
## X-4 (owner review 2, 2026-11-22): what each function does, at a glance in the pop-out.
from ..query._common import connect
from .entities import _briefs
with connect(db) as conn:
_with_briefs(data["nodes"], _briefs(conn))
slot = {(n["name"], n["file"]): i for i, n in enumerate(data["nodes"])}
for n in data["nodes"]:
href = hrefs.get((n["name"], n["file"]))
if href:
n["href"] = href
data["flows"] = [f for f in (_flow(p, slot) for p in pages) if len(f["steps"]) > 1]
lead = (
"Every function in the repository, in one view. Each wedge is a block of the system; rings "
"run outward by call depth from the entry points at the centre. Zoom in to see functions, "
"click one to see what it calls and what calls it, and walk from there; zoom out and the "
"functions fold back into their blocks. Pick a flow to watch it travel."
)
## THE MAP IS HOME (owner Q/A 2026-11-22): its block is written ONCE and included by both this
## page and the home page, so the two can never differ.
(Path(source) / MAP_BLOCK).write_text(web_html(data), encoding="utf-8")
text = ["# Map", "", lead, "", "```{raw} html", f":file: {MAP_BLOCK}", "```", ""]
(Path(source) / "map.md").write_text("\n".join(text), encoding="utf-8")
return "map"