Skip to content
c4-diagrams logo
A Python DSL for C4 model diagrams.

c4-diagrams

Release PyPI - Python Version Build status Maintainability Code Coverage License Ask DeepWiki

c4-diagrams is a Python DSL for defining C4 model architecture diagrams as code.

The package provides first-class abstractions for C4 entities — people, systems, containers, components, boundaries, and relationships — allowing you to describe software architecture in Python and render it into multiple diagram formats.

Features

  • Declarative Python DSL for C4 modeling
  • First-class C4 entities and relationships
  • Multiple rendering backends
  • Suitable for documentation, ADRs, and architecture reviews
  • Portable core DSL for the common C4 subset, with backend-specific features available through contrib extensions

Rendering backends

Backend What you describe Formats Docs
PlantUML Portable diagram, rendered as C4-PlantUML PNG, SVG, EPS, LaTeX, TXT, UTXT PlantUML backend
Mermaid Portable diagram, rendered as Mermaid C4 (experimental) PNG, SVG, PDF Mermaid backend
D2 Portable diagram, rendered as D2 PNG, SVG, PDF D2 backend
Structurizr Structurizr Workspace (model, views, styles) PNG, SVG, PDF Structurizr backend
LikeC4 LikeC4 Project (specification, model, views) PNG, JPG LikeC4 backend

For a side-by-side feature matrix, see the comparison.

Example

Note

PlantUML, Mermaid and D2 render the same portable diagram (the PlantUML tab uses RelRight to lay it out left to right). Structurizr and LikeC4 describe their own object (a Workspace or a Project), so their DSL differs from the portable one.

Here is a system context diagram of an online shop, defined in Python and rendered with each backend:

from c4 import Person, System, SystemContextDiagram
from c4.contrib.plantuml import RelRight

with SystemContextDiagram("Online Shop") as diagram:
    customer = Person("Customer", "Buys goods online.")
    shop = System("Online Shop", "Takes orders from customers.")
    payments = System("Payment Provider", "Processes card payments.")

    customer >> RelRight("Buys goods") >> shop
    shop >> RelRight("Takes payments") >> payments

The model is the same as in the portable code. The only difference is RelRight from c4.contrib.plantuml instead of Rel: PlantUML lays diagrams out top-down by default, and the portable model has no layout setting, so the direction is set per relationship.

c4 render overview.py --plantuml
Rendered PlantUML source
@startuml
' convert it with additional command line argument -DRELATIVE_INCLUDE="relative/absolute" to use locally
!if %variable_exists("RELATIVE_INCLUDE")
    !include %get_variable_value("RELATIVE_INCLUDE")/C4_Context.puml
!else
    !include https://raw.githubusercontent.com/plantuml-stdlib/C4-PlantUML/master/C4_Context.puml
!endif

title Online Shop

Person(customer, "Customer", "Buys goods online.")

System(online_shop, "Online Shop", "Takes orders from customers.")

System(payment_provider, "Payment Provider", "Processes card payments.")

Rel_Right(customer, online_shop, "Buys goods")
Rel_Right(online_shop, payment_provider, "Takes payments")

@enduml
c4 export overview.py --plantuml > diagram.png

PlantUML overview diagram
diagram.png

See the PlantUML backend.

from c4 import Person, Rel, System, SystemContextDiagram

with SystemContextDiagram("Online Shop") as diagram:
    customer = Person("Customer", "Buys goods online.")
    shop = System("Online Shop", "Takes orders from customers.")
    payments = System("Payment Provider", "Processes card payments.")

    customer >> Rel("Buys goods") >> shop
    shop >> Rel("Takes payments") >> payments
c4 render overview.py --mermaid
Rendered Mermaid source
C4Context
title Online Shop

Person(customer, "Customer", "Buys goods online.")

System(online_shop, "Online Shop", "Takes orders from customers.")

System(payment_provider, "Payment Provider", "Processes card payments.")

Rel(customer, online_shop, "Buys goods")

Rel(online_shop, payment_provider, "Takes payments")
c4 export overview.py --mermaid > diagram.png

Mermaid overview diagram
diagram.png

See the Mermaid backend.

from c4 import Person, Rel, System, SystemContextDiagram

with SystemContextDiagram("Online Shop") as diagram:
    customer = Person("Customer", "Buys goods online.")
    shop = System("Online Shop", "Takes orders from customers.")
    payments = System("Payment Provider", "Processes card payments.")

    customer >> Rel("Buys goods") >> shop
    shop >> Rel("Takes payments") >> payments
c4 render overview.py --d2
Rendered D2 source
direction: right
__title: ||md
  # Online Shop
|| {
  near: top-center
}
classes: {
  c4_person: {
    style.fill: "#f5f1ff"
    style.stroke: "#6f4bb2"
    style.font-color: "#211436"
  }
}
customer: ||md
  ## Customer

  [Person]

  Buys goods online.
|| {
  shape: c4-person
  class: ["c4_person"]
}
online_shop: ||md
  ## Online Shop

  [Software System]

  Takes orders from customers.
|| {
  shape: rectangle
}
payment_provider: ||md
  ## Payment Provider

  [Software System]

  Processes card payments.
|| {
  shape: rectangle
}
customer -> online_shop: "Buys goods"
online_shop -> payment_provider: "Takes payments"
c4 export overview.py --d2 > diagram.png

D2 overview diagram
diagram.png

See the D2 backend.

from c4.contrib.structurizr import Workspace

workspace = Workspace("Online Shop")
with workspace.model as model:
    customer = model.Person(
        "Customer", description="Buys goods online.", alias="customer"
    )
    shop = model.SoftwareSystem(
        "Online Shop", description="Takes orders from customers.", alias="shop"
    )
    payments = model.SoftwareSystem(
        "Payment Provider",
        description="Processes card payments.",
        alias="payments",
    )

    customer.uses(shop, "Buys goods")
    shop.uses(payments, "Takes payments")

workspace.views.SystemLandscapeView(key="landscape")
c4 render overview.py --structurizr
Rendered Structurizr DSL
workspace "Online Shop" {
  model {
    customer = person "Customer" "Buys goods online." {
      tags "Person"
    }
    shop = softwareSystem "Online Shop" "Takes orders from customers." {
      tags "Software System"
    }
    payments = softwareSystem "Payment Provider" "Processes card payments." {
      tags "Software System"
    }
    customer -> shop "Buys goods" {
      tags "Relationship"
    }
    shop -> payments "Takes payments" {
      tags "Relationship"
    }
  }
  views {
    systemLandscape "landscape" {
      include *
      autoLayout lr
    }
    styles {
      element "Person" {
        shape Person
        background #08427b
        color #ffffff
        fontSize 22
      }
      element "Software System" {
        background #1168bd
        color #ffffff
      }
      element "Container" {
        background #438dd5
        color #ffffff
      }
      element "Component" {
        background #85bbf0
        color #000000
      }
      element "Database" {
        shape Cylinder
      }
      element "Queue" {
        shape Pipe
      }
      element "External" {
        background #999999
        color #ffffff
      }
      element "Boundary" {
        stroke #444444
        strokeWidth 1
        border Dashed
      }
    }
  }
}
c4 export overview.py --structurizr --structurizr-view-key landscape > diagram.png

Structurizr overview diagram
diagram.png

See the Structurizr backend.

from c4.contrib.likec4 import Project, Rel, Wildcard

project = Project("Online Shop")

spec = project.specification
spec.element("actor", shape="person")
spec.element("system")

model = project.model
customer = model.element("actor", "Customer", alias="customer")
shop = model.element("system", "Online Shop", alias="shop")
payments = model.element("system", "Payment Provider", alias="payments")

customer >> Rel("buys goods") >> shop
shop >> Rel("takes payments") >> payments

index = project.views.element_view("index", "Landscape")
index.include(Wildcard())
index.auto_layout("LeftRight")
c4 render overview.py --likec4
Rendered LikeC4 source
specification {
  element actor { style { shape person } }
  element system
}

model {
  customer = actor "Customer"
  shop = system "Online Shop"
  payments = system "Payment Provider"
  customer -> shop "buys goods"
  shop -> payments "takes payments"
}

views {
  view index {
    title "Landscape"
    include *
    autoLayout LeftRight
  }
}
c4 export overview.py --likec4 > diagram.png

LikeC4 overview diagram
diagram.png

See the LikeC4 backend.

More diagram types and backend-specific features are on the Examples page.