to_mermaid()

Render a knowledge graph as a Mermaid diagram string.

Usage

Source

to_mermaid(
    kg,
    *,
    direction="LR",
    max_nodes=100,
    max_label_length=30,
    show_weights=True,
    node_type=None
)

Produces a Mermaid graph definition suitable for rendering in Markdown, Quarto documents, or any Mermaid-compatible viewer.

Parameters

kg: KnowledgeGraph

The knowledge graph to render.

direction: str = "LR"

Graph direction: "LR" (left-right), "TB" (top-bottom), "RL", or "BT".

max_nodes: int = 100

Maximum number of nodes to include (default 100).

max_label_length: int = 30

Truncate node labels longer than this.

show_weights: bool = True

If True, annotate edges with their weight.

node_type: NodeType | None = None
If provided, only include nodes of this type.

Returns

str
A valid Mermaid graph definition.

Examples

import talk_box as tb

kg = tb.KnowledgeGraph(":memory:")
kg.add_node(tb.Node(id="doc-1", node_type=tb.NodeType.DOCUMENT, name="README"))
kg.add_node(tb.Node(id="e-py", node_type=tb.NodeType.ENTITY, name="Python"))
kg.add_edge(tb.Edge(source="doc-1", target="e-py", relation="mentions"))

print(tb.to_mermaid(kg))