--- name: apex-python-diagrams user-invocable: true disable-model-invocation: false argument-hint: "diagram or chart type, project and output path" description: "**UTILITY SKILL** — Python diagram generation for Azure architectures, WAF/cost/compliance charts, ERDs, swimlanes, timelines, and wireframes. WHEN: 'architecture diagram', 'WAF bar chart', 'cost chart', 'ERD', 'swimlane', 'timeline', 'wireframe'. DO NOT USE FOR: inline Mermaid diagrams (apex-mermaid)." compatibility: Works with VS Code Copilot, Claude Code, and any tool capable of running Python scripts. license: MIT metadata: author: apex version: "2.0" --- # Python Diagrams & Charts (`dlib` SVG/PNG + `matplotlib`) Skill for authoring faithful, brand-consistent Azure architecture diagrams as editable `1920×1080` SVG masters plus `3840×2160` (4K) PNG renders using [`scripts/dlib.py`](scripts/dlib.py) and the bundled official Microsoft Azure Public Service Icons v23 (`assets/`), alongside `matplotlib` for quantitative WAF/cost/compliance charts via [`scripts/diagram_io.py`](scripts/diagram_io.py). ## Prerequisites ```bash pip install cairosvg matplotlib pillow diagrams graphviz && apt-get install -y graphviz ``` ## Routing Guide Every diagram generator emits **both `.svg` and `.png` siblings** — the `.svg` is the editable vector master (selectable text, embedded official Azure SVG `` icons) and the `.png` is the `3840×2160` raster render. | Diagram type | Engine | Helper / API | Output | | --- | --- | --- | --- | | Azure architecture, hub-spoke routing, private DNS | `dlib` (`scripts/dlib.py`) | `from dlib import *; reset(); add(...); finish(...)` | `.py` + `.svg` + `.png` (4K) | | Identity, RBAC, Key Vault & telemetry flows | `dlib` (`scripts/dlib.py`) | `from dlib import *; reset(); add(...); finish(...)` | `.py` + `.svg` + `.png` (4K) | | Subscription vending, cutover & pipeline swimlanes | `dlib` (`scripts/dlib.py`) | `from dlib import *; reset(); add(...); finish(...)` | `.py` + `.svg` + `.png` (4K) | | WAF bar charts | `matplotlib` | `save_figure` from `scripts/diagram_io.py` | `.py` + `.png` + `.svg` | | Cost donut / projection charts | `matplotlib` | `save_figure` from `scripts/diagram_io.py` | `.py` + `.png` + `.svg` | | Compliance gap & Gantt timeline charts | `matplotlib` | `save_figure` from `scripts/diagram_io.py` | `.py` + `.png` + `.svg` | | Entity-relationship diagrams (ERD) | `dlib` or `graphviz` | `dlib.finish` or `render_graphviz` (`diagram_io.py`) | `.py` + `.png` + `.svg` | | UI wireframes | SVG / `graphviz` | `create_wireframe_svg` (`multi_diagram_generator.py`) | `.py` + `.svg` (+ `.png` via CairoSVG) | ## Required Outputs (Workflow Integration) For the standalone assessment profile, `assets/03-des-diagram.{py,png,svg}` is mandatory before final bundle acceptance. Diagram generation uses authorized evidence and validated analysis, not an intermediate Architecture approval. Render all observed resource families inside their Subscription/RG/VNet containers, encode observed versus declared/unverified edges visually via the compact legend (capping caveat boxes at `<= 10%` of canvas height), and never portray a recommendation or unverified control as deployed. For `assessment-bundle-v3` (`visual_charts: true`), `03-Architect` also runs `python3 tools/scripts/render-assessment-charts.py --project-dir agent-output/{project}` during `analysis` to produce `assets/02-waf-posture.{png,svg}`, `assets/03-cost-trends.{png,svg}`, and `assets/03-cost-drivers.{png,svg}` from `_meta/waf-scores.json` and `contracts/03-cost-management.json`, embedding the SVGs inline in `02-architecture-assessment.md` and `03-cost-management-report.md`. | Step | Python diagram & chart files | | --- | --- | | 2 | `02-waf-scores.py/.png/.svg` (`matplotlib` via `save_figure`) | | 3 | `03-des-diagram.py/.png/.svg` (`dlib`), `03-des-cost-distribution.py/.png/.svg`, `03-des-cost-projection.py/.png/.svg` | | 4 | `04-dependency-diagram.py/.png/.svg` (`dlib`), `04-runtime-diagram.py/.png/.svg` (`dlib`) | | 7 | `07-ab-diagram.py/.png/.svg` (`dlib`), `07-ab-cost-*.py/.png/.svg`, `07-ab-compliance-gaps.py/.png/.svg` | Suffix rules: `-des` for design (Step 3), `-ab` for as-built (Step 7). ## Architecture Diagram Workflow (`dlib.py`) Draw the approved specification faithfully; never redesign or invent unapproved services. ### Choosing the Right Style: AAC Technical Architecture vs. Executive Overview Slide * **AAC Technical Architecture (default for `03-des-diagram`, `04-runtime-diagram`, `07-ab-diagram`)**: Use bare official Azure icons (`tile()`), neutral containment boundaries (`zone()` for `region`, `sub`, `rg`, `vnet`, `subnet`, `onprem`), thin dark orthogonal connectors with numbered flow badges (`flow()`), and a matching `dataflow_key()`. This aligns with Microsoft Azure Architecture Center (AAC) and Well-Architected Framework diagramming standards. * **Executive / Bid Overview Slide (for RFI/RFP briefings and capability overviews)**: Use horizontal `card()` tiles, semantic tinted `sub()` / `rg()` frames, `arr()` connectors, and a bottom 5-stage `chevron()` summary bar (`gen02` pattern). 1. **Inspect scope & choose pattern**: Read the architecture/plan specification for exact titles, resource names, CIDRs, boundaries, and connectors. Consult [`references/common-patterns.md`](references/common-patterns.md) for the closest template (AAC hub-spoke + observability with numbered dataflows, executive overview, private DNS, identities/telemetry, or swimlanes). 2. **Author `.py` using AAC primitives (or semantic topology spec)**: ```python import sys from pathlib import Path sys.path.insert(0, str(Path(".github/skills/apex-python-diagrams/scripts").resolve())) from dlib import * # noqa: F403 reset() add(zone(40, 80, 1840, 820, "Azure Region: Sweden Central", kind="region", zone_id="reg-swc")) add(zone(70, 130, 520, 740, "Hub VNet", kind="vnet", subtitle="10.0.0.0/16", zone_id="vnet-hub", parent_id="reg-swc")) add(tile(200, 260, "fw", "Azure Firewall", node_id="fw-hub", parent_id="vnet-hub")) add(tile(440, 260, "dnsresolver", "DNS Private Resolver", node_id="dns-hub", parent_id="vnet-hub")) add(flow([(260, 260), (380, 260)], step=1, style="private", source_id="fw-hub", target_id="dns-hub")) add(dataflow_key(40, 920, 1840, [(1, "Hub firewall proxies private DNS resolution.")])) finish("agent-output/{project}/04-runtime-diagram", "Runtime Architecture & Private Connectivity", style="aac") ``` 3. **Run & check overflow warnings**: Execute `python3 agent-output/{project}/.py`. `dlib.t()` prints `WARN overflow:` lines whenever estimated text width exceeds `mw`. Fix every warning by wrapping text or widening the container — never shrink text below `12px`. `tile()` and `zone()` enforce `strict=True` exact icon matching so invalid icon keys fail immediately with suggestions. 4. **Inspect the rendered PNG & SVG**: View the rendered `.png` to verify zero overlapping labels, uncrossed arrows, and clean visual hierarchy. Note: if the `view` tool caches by path, copy the PNG to a unique preview filename, inspect it, and remove the temporary preview afterward. ## Canvas, Palette & Boundary Rules (`dlib.py`) * **Canvas**: SVG `width="3840" height="2160" viewBox="0 0 1920 1080"`, explicit white background (`#FFFFFF`), font `Segoe UI, Arial, sans-serif`. Title top-centre at `(960, 56)` (`40px` bold navy `#0B1F3A`); footer bottom-centre at `(960, 1058)` (`18px` `#4A5A6A`, defaults to `"APEX"`). * **Semantic Colours**: * **Shared platform (`BL` / `BLF`)**: Stroke `#0078D4`, fills `#E6F1FB` / `#F5FAFF` * **Workload (`TL` / `TLF`)**: Stroke `#008272`, fills `#E3F4F1` / `#F2FAF8` * **Legacy / management (`SL` / `GRF`)**: Stroke `#5B6B7B`, fill `#EEF2F6` * **Migration / notes / policy / exceptions (`AM` / `AMB` / `AMF`)**: Stroke `#D18B00` / `#F2A900` on fill `#FFF6DD` * **Identity (`PU` / `PUF`)**: Stroke `#7B3FA0`, fill `#F3ECF8` * **Distinct Boundaries** (containment = boundary, relationship = arrow): * **Tenant**: `tenant(x=30, y=80, w=1860, h=940, name="Microsoft Entra tenant")` — heavy navy solid border (`NAVY`, `sw=3`, `rx=14`) + `ten` icon * **Management group**: Filled rounded box with `mg` icon * **Subscription**: `sub(x, y, w, h, name, col, fill, badgetxt, bw)` — solid `2.5px` border + `sub` icon + top-right scope badge * **Resource group**: `rg(x, y, w, h, name, col)` — dashed `1.8px` border (`stroke-dasharray="7 4"`) + `rg` icon * **VNet / Subnet**: Solid box inside RG with `vnet` / `subnet` icon and CIDR block in the header * **Semantic Connectors (`arr(d, kind=...)`, `vbracket(...)`, `legend(...)`, `chevron(...)`)**: * `net` (solid blue `#0078D4`): network/request traffic * `id` (dashed purple `#7B3FA0`): identity, RBAC, token/secret authorization * `dns` (dotted teal `#008272`): private DNS resolution * `tel` (dashed slate `#5B6B7B`): diagnostics and OpenTelemetry/Log Analytics * `mig` (solid amber `#D18B00`): replication / migration cutover path * `off` (dashed grey `#8A98A8`): disabled / decommissioned path * **Typography & Anti-Crowding (`gen02` / `gen08` / `gen09` Visual Standard)**: * Body `16–22px`, headers `20–26px`, badges `15px` bold, nothing below `~12px`. * Keep service cards compact (`1–2` lines, `h=44..84`) with concise titles (`"Blob Storage"`, `"Azure OpenAI"`, `"snet-pe / Private endpoints"`). Never pack 3–4 lines of spec-sheet prose into wide full-width text blocks. * Group stacked Private Endpoint or VNet-integrated targets with `vbracket(x, y1, y2)` and label connectors directly with inline `chip()` / `chip2()` pills. * Anchor end-to-end overview diagrams with an outer `tenant()` frame, a top-left visual `legend(60, 172, ...)` listing only used connector kinds, and a bottom 5-stage `chevron()` bar + summary `chip()`. * Keep a `20px+` gap between sibling boxes, leave dedicated routing corridors for orthogonal connectors (`M...H...V...`), and use Unicode `→` (`\u2192`) rather than ASCII `->` in text labels. ## Official Azure SVG Icons (`assets/`) All **705 official Microsoft Azure Public Service Icons (v23)** plus `assets/github.svg` (`gh`) are vendored offline under [`assets/`](assets/). * **Curated short keys (`dlib.P`)**: `ten`, `mg`, `sub`, `rg`, `pol`, `bud`, `arc`, `rsv`, `bvault`, `vnet`, `subnet`, `fw`, `fwp`, `waf`, `dns`, `bas`, `nat`, `peer`, `rt`, `pe`, `pip`, `nsg`, `afd`, `agw`, `lb`, `vpngw`, `er`, `vm`, `disk`, `app`, `asp`, `func`, `swa`, `aks`, `acr`, `aci`, `aca`, `signalr`, `sqlmi`, `sqls`, `sqldb`, `arcsql`, `cosmos`, `postgres`, `mysql`, `redis`, `blob`, `st`, `cont`, `queue`, `apim`, `sb`, `evg`, `evh`, `logic`, `adf`, `databricks`, `kv`, `mi`, `user`, `entra`, `appreg`, `entapp`, `mon`, `law`, `ai`, `openai`, `foundry`, `search`, `cog`, `ml`, `code`, `cli`, `dev`, plus `gh` (GitHub). * **Full v23 auto-lookup**: Any of the 705 v23 icons can also be referenced by service slug (e.g., `"event-hubs"`, `"front-door-and-cdn-profiles"`) via `dlib.resolve_icon_path(k)`. * **Gradient ID Namespacing**: Azure SVG icons share internal gradient/clip IDs (`id="a"`, `url(#a)`). `dlib.symbols()` embeds only used icons as `` and prefixes every internal `id`, `url(#...)`, and `href="#..."` with `{k}-`. Never inline raw SVG icon files without this prefixing. * **Non-Azure shapes**: No official Azure icon exists for a generic browser, laptop, folder, or Microsoft Graph — use clean labelled `box()`/`card()` containers or the `folder()` glyph; never invent lookalike icons. ## Quantitative Charts & Legacy Helpers (`diagram_io.py`) For quantitative charts (`matplotlib`), import `save_figure` from [`scripts/diagram_io.py`](scripts/diagram_io.py) so every chart emits `.png` + `.svg` siblings (`DPI 150`). See [`references/python-charts.md`](references/python-charts.md) and [`references/waf-cost-charts.md`](references/waf-cost-charts.md). Legacy `diagrams` (`diagram_kwargs` + `embed_svg_images`) and `graphviz` (`render_graphviz`) helpers remain in `diagram_io.py` for backward compatibility. ## Rules **DO:** Use `scripts/dlib.py` (`finish` or `diagram_io.render_dlib`) for architecture, network, DNS, identity, cutover, and swimlane diagrams · Use `save_figure` from `scripts/diagram_io.py` for WAF/cost/compliance `matplotlib` charts · Fix all `WARN overflow:` hints before finishing · Keep orthogonal connectors (`M...H...V...`) in routing corridors · Escape `<`, `>`, `&` in SVG text · Verify both `.svg` and `.png` outputs exist and are non-empty. **DON'T:** Call `plt.savefig(...)`, `Diagram(..., outformat=...)`, or `dot.render(...)` directly · Shrink font sizes below `12px` to cram text · Route arrows across unrelated cards or text labels · Include real tenant/subscription/object IDs or credentials · Use Mermaid for rendered PNG/SVG deliverables. ## Scope Exclusions Does NOT: produce inline Mermaid diagrams (`apex-mermaid`) · generate Bicep/Terraform · create ADRs (`apex-azure-adr`) · deploy resources. ## Scripts `scripts/dlib.py` (primary AAC + overview SVG & 4K PNG Azure diagram engine) · `scripts/topology_render.py` (semantic `*.topology.json` → ELK auto-layout → AAC SVG + PNG; invoked via `diagram_io.render_topology()` inside the standard `.py` wrapper) · `scripts/diagram_io.py` (shared PNG+SVG output helper: `render_dlib`, `render_topology`, `save_figure`, `diagram_kwargs`, `render_graphviz`, `embed_svg_images`) · `scripts/generate_diagram.py` (interactive template generator) · `scripts/multi_diagram_generator.py` (ERD, swimlane, timeline, wireframe helpers) · `scripts/ascii_to_diagram.py` (ASCII art → diagram conversion) · `scripts/verify_installation.py` (prerequisites & `dlib`/v23 icon check) ## Reference Index | File | Content | | --- | --- | | `references/common-patterns.md` | Ready-to-use `dlib.py` Azure architecture templates (hub-spoke, vending, DNS, identity, cutover, swimlanes) | | `references/azure-components.md` | Curated `dlib.P` icon keys and full 705-icon Azure v23 component catalog | | `references/python-charts.md` | Chart execution, `dlib` & `matplotlib` design tokens, output standards | | `references/waf-cost-charts.md` | WAF pillar bar, cost donut & projection `matplotlib` chart implementations | | `references/business-process-flows.md` | Workflow and swimlane diagram patterns | | `references/entity-relationship-diagrams.md` | Database ERD patterns | | `references/integration-services.md` | Integration service diagram patterns | | `references/migration-patterns.md` | Migration and cutover architecture patterns | | `references/sequence-auth-flows.md` | Authentication and identity flow patterns | | `references/timeline-gantt-diagrams.md` | Project timeline and Gantt diagrams | | `references/ui-wireframe-diagrams.md` | UI mockup and wireframe patterns | | `references/iac-to-diagram.md` | Generate diagrams from Bicep/Terraform/ARM templates |