Skip to content

pydantic_graph.mermaid

MermaidConfig

Bases: TypedDict

Parameters to configure mermaid chart generation.

Attributes

start_node

Identifiers of nodes that start the graph.

Type: Sequence[NodeIdent] | NodeIdent

highlighted_nodes

Identifiers of nodes to highlight.

Type: Sequence[NodeIdent] | NodeIdent

highlight_css

CSS to use for highlighting nodes.

Type: str

title

The title of the diagram.

Type: str | None

edge_labels

Whether to include edge labels in the diagram.

Type: bool

notes

Whether to include notes on nodes in the diagram, defaults to true.

Type: bool

image_type

The image type to generate. If unspecified, the default behavior is 'jpeg'.

Type: Literal[‘jpeg’, ‘png’, ‘webp’, ‘svg’, ‘pdf’]

pdf_fit

When using image_type=‘pdf’, whether to fit the diagram to the PDF page.

Type: bool

pdf_landscape

When using image_type=‘pdf’, whether to use landscape orientation for the PDF.

This has no effect if using pdf_fit.

Type: bool

pdf_paper

When using image_type=‘pdf’, the paper size of the PDF.

Type: Literal[‘letter’, ‘legal’, ‘tabloid’, ‘ledger’, ‘a0’, ‘a1’, ‘a2’, ‘a3’, ‘a4’, ‘a5’, ‘a6’]

background_color

The background color of the diagram.

If None, the default transparent background is used. The color value is interpreted as a hexadecimal color code by default (and should not have a leading ’#’), but you can also use named colors by prefixing the value with '!'. For example, valid choices include background_color='!white' or background_color='FF0000'.

Type: str

theme

The theme of the diagram. Defaults to ‘default’.

Type: Literal[‘default’, ‘neutral’, ‘dark’, ‘forest’]

width

The width of the diagram.

Type: int

height

The height of the diagram.

Type: int

scale

The scale of the diagram.

The scale must be a number between 1 and 3, and you can only set a scale if one or both of width and height are set.

Type: Annotated[float, Ge(1), Le(3)]

httpx_client

An HTTPX client to use for requests, mostly for testing purposes.

Type: httpx.Client

direction

The direction of the state diagram.

Type: StateDiagramDirection

generate_code

def generate_code(
    graph: Graph[Any, Any, Any],
    start_node: Sequence[NodeIdent] | NodeIdent | None = None,
    highlighted_nodes: Sequence[NodeIdent] | NodeIdent | None = None,
    highlight_css: str = DEFAULT_HIGHLIGHT_CSS,
    title: str | None = None,
    edge_labels: bool = True,
    notes: bool = True,
    direction: StateDiagramDirection | None,
) -> str

Generate Mermaid state diagram code for a graph.

Returns

str — The Mermaid code for the graph.

Parameters

graph : Graph[Any, Any, Any]

The graph to generate the image for.

start_node : Sequence[NodeIdent] | NodeIdent | None Default: None

Identifiers of nodes that start the graph.

highlighted_nodes : Sequence[NodeIdent] | NodeIdent | None Default: None

Identifiers of nodes to highlight.

highlight_css : str Default: DEFAULT_HIGHLIGHT_CSS

CSS to use for highlighting nodes.

title : str | None Default: None

The title of the diagram.

edge_labels : bool Default: True

Whether to include edge labels in the diagram.

notes : bool Default: True

Whether to include notes in the diagram.

direction : StateDiagramDirection | None

The direction of flow.

request_image

def request_image(
    graph: Graph[Any, Any, Any],
    kwargs: Unpack[MermaidConfig] = {},
) -> bytes

Generate an image of a Mermaid diagram using mermaid.ink.

Returns

bytes — The image data.

Parameters

graph : Graph[Any, Any, Any]

The graph to generate the image for.

**kwargs : Unpack[MermaidConfig] Default: \{\}

Additional parameters to configure mermaid chart generation.

save_image

def save_image(
    path: Path | str,
    graph: Graph[Any, Any, Any],
    kwargs: Unpack[MermaidConfig] = {},
) -> None

Generate an image of a Mermaid diagram using mermaid.ink and save it to a local file.

Returns

None

Parameters

path : Path | str

The path to save the image to.

graph : Graph[Any, Any, Any]

The graph to generate the image for.

**kwargs : Unpack[MermaidConfig] Default: \{\}

Additional parameters to configure mermaid chart generation.

DEFAULT_HIGHLIGHT_CSS

The default CSS to use for highlighting nodes.

Default: 'fill:#fdff32'

StateDiagramDirection

Used to specify the direction of the state diagram generated by mermaid.

  • 'TB': Top to bottom, this is the default for mermaid charts.
  • 'LR': Left to right
  • 'RL': Right to left
  • 'BT': Bottom to top

Default: Literal['TB', 'LR', 'RL', 'BT']

NodeIdent

A type alias for a node identifier.

This can be:

  • A node instance (instance of a subclass of BaseNode).
  • A node class (subclass of BaseNode).
  • A string representing the node ID.

Type: TypeAlias Default: 'type[BaseNode[Any, Any, Any]] | BaseNode[Any, Any, Any] | str'