Skip to content

D2 Render Options

c4.renderers.d2.options.D2RenderOptionsBuilder

Builder for constructing D2RenderOptions.

direction

direction(direction: D2Direction | None) -> Self

Set the D2 layout direction.

Parameters:

Name Type Description Default
direction D2Direction | None

One of "up", "down", "left", "right", or None to omit the directive.

required

Returns:

Type Description
Self

The updated render options builder.

layout

layout(layout: D2Layout) -> Self

Set the D2 layout engine for image export.

Parameters:

Name Type Description Default
layout D2Layout

One of "dagre" or "elk".

required

Returns:

Type Description
Self

The updated render options builder.

theme

theme(theme: int | None) -> Self

Set the D2 theme ID.

Parameters:

Name Type Description Default
theme int | None

A non-negative integer D2 theme ID, or None to omit it.

required

Returns:

Type Description
Self

The updated render options builder.

title_near

title_near(position: D2NearPosition | None) -> Self

Set the generated diagram title placement.

Parameters:

Name Type Description Default
position D2NearPosition | None

One of the supported D2 near positions, or None to render the title node without forcing placement.

required

Returns:

Type Description
Self

The updated render options builder.

sequence_diagram

sequence_diagram(enabled: bool = True) -> Self

Render DynamicDiagram output as a D2 sequence diagram.

Parameters:

Name Type Description Default
enabled bool

Whether DynamicDiagram output should include shape: sequence_diagram.

True

Returns:

Type Description
Self

The updated render options builder.

auto_number_relationships

auto_number_relationships(enabled: bool = True) -> Self

Prefix relationship labels with their render order.

Parameters:

Name Type Description Default
enabled bool

Whether relationship labels should be prefixed with "1. ", "2. ", etc.

True

Returns:

Type Description
Self

The updated render options builder.

include_type_label

include_type_label(enabled: bool = True) -> Self

Include or omit element type labels from Markdown labels.

Parameters:

Name Type Description Default
enabled bool

Whether type labels should be included.

True

Returns:

Type Description
Self

The updated render options builder.

include_technology

include_technology(enabled: bool = True) -> Self

Include or omit technology from visible element labels.

Parameters:

Name Type Description Default
enabled bool

Whether technology should be included.

True

Returns:

Type Description
Self

The updated render options builder.

include_properties

include_properties(enabled: bool = True) -> Self

Include or omit element properties in D2 output.

Parameters:

Name Type Description Default
enabled bool

Whether properties should be included.

True

Returns:

Type Description
Self

The updated render options builder.

bidirectional_relationships

bidirectional_relationships(
    strategy: D2BidirectionalRelationshipStrategy,
) -> Self

Set how bidirectional relationships are rendered.

Parameters:

Name Type Description Default
strategy D2BidirectionalRelationshipStrategy

"two_edges" or "single_edge".

required

Returns:

Type Description
Self

The updated render options builder.

fully_qualified_relationships

fully_qualified_relationships(enabled: bool = True) -> Self

Use fully qualified D2 paths for relationship endpoints.

Parameters:

Name Type Description Default
enabled bool

Whether relationship endpoints should always use full nested D2 paths.

True

Returns:

Type Description
Self

The updated render options builder.

legend

legend(legend: D2Legend | None) -> Self

Set a structured D2 legend.

Parameters:

Name Type Description Default
legend D2Legend | None

Legend options, or None to omit the D2 legend block.

required

Returns:

Type Description
Self

The updated render options builder.

build

build() -> D2RenderOptions

Build and return the final D2RenderOptions instance.

default classmethod

default() -> Self

Return a new builder with default D2 render options.

c4.renderers.d2.options.D2RenderOptions dataclass

Render options for the D2 renderer.

Attributes:

Name Type Description
direction D2Direction | None

Overall diagram layout direction. Set to None to omit the D2 direction directive.

layout D2Layout

D2 layout engine used by image export backends.

theme int | None

Optional D2 theme ID.

title_near D2NearPosition | None

Optional D2 near placement for generated title nodes. Set to None to preserve the title without forcing placement.

sequence_diagram bool

Whether DynamicDiagram output should be emitted as a D2 sequence diagram.

auto_number_relationships bool

Whether relationship labels should be prefixed with their render order.

include_type_label bool

Whether element type labels appear in Markdown labels.

include_technology bool

Whether element technology appears in labels.

include_properties bool

Whether element properties are emitted.

bidirectional_relationships D2BidirectionalRelationshipStrategy

How bidirectional relationships are rendered. "two_edges" preserves the existing behavior; "single_edge" emits one D2 <-> edge.

fully_qualified_relationships bool

Whether relationship endpoints always use fully qualified D2 paths.

legend D2Legend | None

Optional structured D2 legend.

Source code in c4/renderers/d2/options.py
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
@dataclass(frozen=True)
class D2RenderOptions:
    """
    Render options for the D2 renderer.

    Attributes:
        direction: Overall diagram layout direction. Set to `None` to omit the
            D2 direction directive.
        layout: D2 layout engine used by image export backends.
        theme: Optional D2 theme ID.
        title_near: Optional D2 `near` placement for generated title nodes.
            Set to `None` to preserve the title without forcing placement.
        sequence_diagram: Whether DynamicDiagram output should be emitted as a
            D2 sequence diagram.
        auto_number_relationships: Whether relationship labels should be
            prefixed with their render order.
        include_type_label: Whether element type labels appear in Markdown
            labels.
        include_technology: Whether element technology appears in labels.
        include_properties: Whether element properties are emitted.
        bidirectional_relationships: How bidirectional relationships are
            rendered. ``"two_edges"`` preserves the existing behavior;
            ``"single_edge"`` emits one D2 ``<->`` edge.
        fully_qualified_relationships: Whether relationship endpoints always
            use fully qualified D2 paths.
        legend: Optional structured D2 legend.
    """

    direction: D2Direction | None = "right"
    layout: D2Layout = "dagre"
    theme: int | None = None
    title_near: D2NearPosition | None = "top-center"
    sequence_diagram: bool = False
    auto_number_relationships: bool = False
    include_type_label: bool = True
    include_technology: bool = True
    include_properties: bool = False
    bidirectional_relationships: D2BidirectionalRelationshipStrategy = (
        "two_edges"
    )
    fully_qualified_relationships: bool = True
    legend: D2Legend | None = None

    def __post_init__(self) -> None:
        """Validate render option values."""
        if self.direction is not None and self.direction not in _D2_DIRECTIONS:
            directions = ", ".join(
                repr(direction) for direction in _D2_DIRECTIONS
            )
            raise ValueError(
                f"D2 direction must be one of {directions}, or None. "
                f"Got {self.direction!r}."
            )

        if self.layout not in _D2_LAYOUTS:
            layouts = ", ".join(repr(layout) for layout in _D2_LAYOUTS)
            raise ValueError(
                f"D2 layout must be one of {layouts}. Got {self.layout!r}."
            )

        if self.theme is not None:
            if isinstance(self.theme, bool) or not isinstance(self.theme, int):
                raise TypeError(
                    "D2 theme must be None or a non-negative integer theme ID. "
                    f"Got {self.theme!r}."
                )

            if self.theme < 0:
                raise ValueError(
                    "D2 theme must be None or a non-negative integer theme ID. "
                    f"Got {self.theme!r}."
                )

        if (
            self.title_near is not None
            and self.title_near not in _D2_NEAR_POSITIONS
        ):
            positions = ", ".join(
                repr(position) for position in _D2_NEAR_POSITIONS
            )
            raise ValueError(
                f"D2 title near position must be one of {positions}, or None. "
                f"Got {self.title_near!r}."
            )

        if (
            self.bidirectional_relationships
            not in _D2_BIDIRECTIONAL_RELATIONSHIP_STRATEGIES
        ):
            strategies = ", ".join(
                repr(strategy)
                for strategy in _D2_BIDIRECTIONAL_RELATIONSHIP_STRATEGIES
            )
            raise ValueError(
                "D2 bidirectional relationship strategy must be one of "
                f"{strategies}. Got {self.bidirectional_relationships!r}."
            )

        _validate_legend(self.legend)

c4.renderers.d2.options.D2Legend dataclass

Structured D2 legend render options.

Source code in c4/renderers/d2/options.py
72
73
74
75
76
77
@dataclass(frozen=True)
class D2Legend:
    """Structured D2 legend render options."""

    label: str = "Legend"
    items: list[D2LegendElement | D2LegendRel] = field(default_factory=list)

c4.renderers.d2.options.D2LegendElement dataclass

A D2 legend node sample.

Source code in c4/renderers/d2/options.py
46
47
48
49
50
51
52
53
54
55
@dataclass(frozen=True)
class D2LegendElement:
    """A D2 legend node sample."""

    label: str
    alias: str | None = None
    shape: str | None = None
    style: StyleExtensions | dict[str, D2StyleValue] | None = None
    icon: str | None = None
    classes: list[str] | None = None

c4.renderers.d2.options.D2LegendRel dataclass

A D2 legend relationship sample.

Source code in c4/renderers/d2/options.py
58
59
60
61
62
63
64
65
66
67
68
69
@dataclass(frozen=True)
class D2LegendRel:
    """A D2 legend relationship sample."""

    label: str
    alias: str | None = None
    source: str | None = None
    target: str | None = None
    bidirectional: bool = False
    style: StyleExtensions | dict[str, D2StyleValue] | None = None
    classes: list[str] | None = None
    hide_endpoints: bool | None = None