Documentation that stays current is documentation that lives in code. Diagram-as-code tools let you define architecture diagrams, system designs, and flowcharts using plain text — version-controlled alongside your source code, reviewable in pull requests, and rendered automatically in CI/CD pipelines.
This guide compares three leading open-source diagram-as-code frameworks: D2 (Declarative Diagramming), Structurizr (C4 model implementation), and Python Diagrams (cloud architecture prototyping). Each targets a different use case, from general-purpose diagrams to formal architecture modeling.
Comparison at a Glance
| Feature | D2 | Structurizr | Python Diagrams |
|---|---|---|---|
| Language | Custom DSL (D2 markup) | Java DSL, Kotlin DSL, CLI (JSON/workspace) | Python API |
| Diagram Types | Flowcharts, sequence, class, state, ERD, architecture | C4 model (context, container, component, code) | Cloud architecture diagrams |
| Output Formats | SVG, PNG, PDF, HTML | PNG, SVG, PlantUML, Mermaid, JSON | PNG, SVG |
| Cloud Provider Icons | Generic icons | Generic + custom icons | AWS, GCP, Azure, Alibaba, OCI, DigitalOcean |
| C4 Model Support | Manual (no built-in enforcement) | Native (C4 model is the core design) | No |
| Self-Hosting | CLI binary, Docker, Go library | Java JAR, Docker, web app | Python package (pip) |
| Live Preview | d2lang.com playground, VS Code extension | Structurizr Lite (local web server) | None (script execution) |
| GitHub Stars | 23,600+ | 560+ (CLI), larger for Java DSL | 42,200+ |
| License | Elastic 2.0 | MIT | MIT |
| Best For | General-purpose diagramming | Formal software architecture | Cloud infrastructure documentation |
D2: Modern Declarative Diagramming
D2 by Terrastruct is a modern diagram scripting language designed to be both human-readable and powerful. It supports flowcharts, sequence diagrams, entity-relationship diagrams, state machines, and class diagrams — all from a single DSL.
How It Works
D2 uses a clean, intuitive syntax that resembles markdown for diagrams. You define objects, connections, and layouts in plain text, and D2 renders them into beautiful SVG or PNG output.
| |
Docker-Based Rendering Pipeline
| |
CI/CD Integration
| |
Key Strengths
- Clean syntax: Easier to read and write than PlantUML or Graphviz
- Layout engine: Multiple layout options (DAGRE, ELK, TALA) for different visual styles
- Rich icon library: Built-in icons for cloud services, databases, and common architecture patterns
- Theme support: Built-in themes (neutral, flagship terra, origami) for consistent branding
Limitations
- Elastic License 2.0 — not OSI-approved open source (free for most uses, but restrictions on competing with D2)
- No built-in C4 model enforcement
- Younger ecosystem than PlantUML or Mermaid
Structurizr: Formal Software Architecture with the C4 Model
Structurizr implements the C4 model for software architecture, created by Simon Brown. It provides formal semantics for describing software systems at four levels of abstraction: Context, Container, Component, and Code.
How It Works
Structurizr uses a workspace model where you define software systems, containers, components, and their relationships using a DSL (or JSON). The CLI renders these definitions into C4-compliant diagrams.
| |
Structurizr Lite (Self-Hosted)
Structurizr Lite is a self-hosted, read-only viewer for Structurizr workspaces:
| |
The workspace directory should contain a workspace.dsl or workspace.json file. Navigate to http://localhost:8080 to view your diagrams.
Key Strengths
- C4 model enforcement: Built-in semantics ensure consistent architecture documentation
- Multiple views: Same model generates context, container, and component diagrams
- Integration exports: Export to PlantUML, Mermaid, WebSequenceDiagrams, and more
- Team collaboration: Define architecture once, generate many views
Limitations
- DSL learning curve (though JSON alternative exists)
- Structurizr Lite is read-only — editing requires the commercial SaaS or Structurizr CLI
- Smaller community compared to general-purpose diagram tools
Python Diagrams: Cloud Architecture Prototyping
Python Diagrams lets you draw cloud system architecture diagrams in pure Python. It supports major cloud providers (AWS, GCP, Azure) with accurate icons and automatic layout.
How It Works
You write Python scripts that import node types from the diagrams library and connect them using Python operators. The result is a rendered PNG or SVG diagram.
| |
Docker-Based Rendering
| |
| |
Key Strengths
- Python-native: Leverage Python full ecosystem (loops, functions, imports)
- Rich cloud icons: Accurate, up-to-date icons for AWS, GCP, Azure, and more
- Programmatic generation: Generate diagrams dynamically from inventory data
- Simple API: Clean, intuitive syntax — draw diagrams like writing code
Limitations
- Python-only — no DSL or markup language
- No built-in web preview (requires running the script)
- Diagram customization is limited compared to D2 or PlantUML
- No standard output format beyond PNG/SVG
Choosing the Right Tool
| Use Case | Recommended Tool |
|---|---|
| General-purpose diagrams (flowcharts, ERD, sequence) | D2 |
| Formal software architecture documentation | Structurizr |
| Cloud infrastructure diagrams | Python Diagrams |
| C4 model compliance | Structurizr |
| CI/CD diagram generation | D2 or Python Diagrams |
| Multi-cloud architecture docs | Python Diagrams |
| Architecture review documentation | Structurizr |
For broader diagram generation (including PlantUML and Mermaid), see our text-to-diagram platforms guide. For API documentation, check our API documentation tools comparison.
Why Self-Host Diagram Generation?
Self-hosted diagram-as-code tools keep your architecture documentation private and version-controlled. Unlike SaaS diagram tools, they:
- Integrate directly into your CI/CD pipeline for automatic diagram updates
- Work offline — no internet connection required
- Avoid vendor lock-in — diagrams are plain text, stored in your git repository
- Support code review — diagram changes appear as diffs in pull requests
FAQ
Is D2 truly open source?
D2 uses the Elastic License 2.0, which allows free use, modification, and distribution for most purposes. However, it prohibits creating a competing D2 service. For most personal and commercial uses, this is effectively free. If you need a strictly OSI-approved license, consider PlantUML, Mermaid, or Python Diagrams.
What is the C4 model and why does it matter?
The C4 model is a hierarchical approach to software architecture documentation with four levels: Context (system and external dependencies), Container (deployable units), Component (modules within containers), and Code (classes/functions). Structurizr enforces this model, ensuring architecture docs are consistent and comprehensive.
Can I convert D2 diagrams to Mermaid or PlantUML?
D2 does not natively export to Mermaid or PlantUML formats. However, you can render D2 diagrams as SVG and embed them in Mermaid-compatible documentation. For direct format conversion, tools like Kroki support multiple diagram languages through a unified API.
How do I version-control diagram changes?
Since diagram-as-code tools store diagrams as plain text (DSL files, Python scripts, or JSON), they work natively with git. Every diagram change produces a clean diff in pull requests, making architecture evolution trackable over time.
Can Python Diagrams generate interactive diagrams?
Python Diagrams generates static PNG/SVG output only. For interactive diagrams, consider using D2 HTML output or Structurizr web viewer, which support clickable elements and drill-down navigation between architecture levels.