Skip to content

LikeC4 Views

Views are created on project.views. LikeC4 has no fixed C4 view types: an element view shows any part of the model, a deployment view shows the deployment, and a dynamic view shows an ordered sequence of interactions. Keys are unique across all view types.

Factory Source Takes
element_view(key, title=None, of=None, ...) view key of scope include, exclude, style, auto_layout
deployment_view(key, title=None, ...) deployment view key include, exclude, style, auto_layout
dynamic_view(key, title=None, variant=, ...) dynamic view key step, back, auto_layout

All three also take description, tags and links. Rules are written in the order they are added.

Python call Generated DSL
project.views.element_view("index", "Landscape", of=shop).include(Wildcard())
view index of shop {
  title "Landscape"
  include *
}
view = project.views.element_view("index")
view.include(Children(shop), Descendants(shop))
include shop., shop.*
view = project.views.element_view("index")
view.include(Between(customer, shop))
view.include(Between(customer, shop, both=True))
view.include(Incoming(shop), Outgoing(shop), InOut(shop))
include customer -> shop
include customer <-> shop
include -> shop
include shop ->
include -> shop ->
view = project.views.element_view("index")
view.include(Where(shop, tag="legacy"))
view.include(RawPredicate("shop.api"))
include shop where tag is #legacy
include shop.api
view = project.views.element_view("index")
view.include(Wildcard()).exclude(customer)
exclude customer
view = project.views.element_view("index")
view.style(shop, color="brand", opacity=40)
style shop {
  color brand
  opacity 40%
}
view = project.views.element_view("index")
view.auto_layout("LeftRight", rank_sep=80, node_sep=60)
autoLayout LeftRight 80 60
view = project.views.element_view("index")
view.include(shop, navigate_to="flow")
include shop with { navigateTo flow }
view = project.views.deployment_view("live", "Live")
view.include(Wildcard())
deployment view live {
  title "Live"
  include *
}
flow = project.views.dynamic_view(
    "flow", "Checkout", variant="sequence")
flow.step(customer, shop, "pays", technology="HTTPS")
flow.back(customer, shop, "confirms")
dynamic view flow {
  title "Checkout"
  variant sequence
  customer -> shop "pays" { technology "HTTPS" }
  customer <- shop "confirms"
}

Predicates

include, exclude and style take elements (or deployment nodes and instances) and these predicate objects:

Object Written as Meaning
Wildcard() * The view scope, or all top-level elements.
Children(x) x.* Direct children of x.
Descendants(x) x.** All nested elements of x.
[Between(a, b)][c4.contrib.likec4.Between] a -> b Relationships from a to b; both=True writes <->.
[Incoming(x)][c4.contrib.likec4.Incoming] -> x Relationships that enter x.
[Outgoing(x)][c4.contrib.likec4.Outgoing] x -> Relationships that leave x.
[InOut(x)][c4.contrib.likec4.InOut] -> x -> Relationships that enter or leave x.
Where(x, tag=, kind=) x where tag is #t ... A filter over another item.
RawPredicate(text) text as is Anything else; checked only by likec4 validate.

Rel is not a view predicate

Rel is the relationship expression (a >> Rel("title") >> b) used in the model. In a view, a relationship predicate is Between, Incoming, Outgoing or InOut.

include(..., navigate_to="view-key") writes with { navigateTo ... }. A style rule needs at least one property and cannot target a relationship predicate. Where cannot wrap a Where or a RawPredicate, and the relationship predicates cannot connect them either; write a RawPredicate instead.

An element view of X may only include elements inside X. The renderer cannot check this statically.

Auto layout

view.auto_layout(direction="TopBottom", rank_sep=None, node_sep=None) adds an autoLayout rule. The directions are listed in the Reference. node_sep needs rank_sep.

Deployment views

deployment_view works over deployment nodes and instances. Its include takes no navigate_to: with { ... } overrides are not generated in deployment views.

Dynamic views

dynamic_view(key, title, variant="diagram") takes a flat list of steps. variant is "diagram" or "sequence".

  • view.step(source, target, title, **properties) writes source -> target.
  • view.back(receiver, sender, title, **properties) writes receiver <- sender.
  • Step properties are description, technology, notes, navigate_to (a dynamic view key) and color.

Steps connect model elements. Parallel and alternative blocks are not supported.

from c4.contrib.likec4 import Project

project = Project("Checkout")
project.specification.element("actor").element("system")

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

flow = project.views.dynamic_view("checkout", "Checkout", variant="sequence")
flow.step(customer, shop, "pays", technology="HTTPS")
flow.back(customer, shop, "receives a receipt")
print(project.render())

Upstream: views, dynamic views, predicates.