Mermaid Exporter

For any details about the Mermaid language, see mermaid

class anytree.exporter.mermaidexporter.MermaidExporter(node, graph='graph', name='TD', options=None, indent=0, nodenamefunc=None, nodefunc=None, edgefunc=None, maxlevel=None)[source]

Mermaid Exporter.

Parameters:

node (Node) – start node.

Keyword Arguments:
  • graph – Mermaid graph type.

  • name – Mermaid graph name.

  • options – list of options added to the graph.

  • indent (int) – number of spaces for indent.

  • nodenamefunc – Function to extract node name from node object. The function shall accept one node object as argument and return the name of it. Returns a unique identifier by default.

  • nodefunc – Function to decorate a node with attributes. The function shall accept one node object as argument and return the attributes. Returns [{node.name}] and creates therefore a rectangular node by default.

  • edgefunc – Function to decorate a edge with attributes. The function shall accept two node objects as argument. The first the node and the second the child and return edge. Returns --> by default.

  • maxlevel (int) – Limit export to this number of levels.

>>> from anytree import Node
>>> root = Node("root")
>>> s0 = Node("sub0", parent=root, edge=2)
>>> s0b = Node("sub0B", parent=s0, foo=4, edge=109)
>>> s0a = Node("sub0A", parent=s0, edge="")
>>> s1 = Node("sub1", parent=root, edge="")
>>> s1a = Node("sub1A", parent=s1, edge=7)
>>> s1b = Node("sub1B", parent=s1, edge=8)
>>> s1c = Node("sub1C", parent=s1, edge=22)
>>> s1ca = Node("sub1Ca", parent=s1c, edge=42)

A top-down graph:

>>> from anytree.exporter import MermaidExporter
>>> for line in MermaidExporter(root):
...     print(line)
graph TD
N0["root"]
N1["sub0"]
N2["sub0B"]
N3["sub0A"]
N4["sub1"]
N5["sub1A"]
N6["sub1B"]
N7["sub1C"]
N8["sub1Ca"]
N0-->N1
N0-->N4
N1-->N2
N1-->N3
N4-->N5
N4-->N6
N4-->N7
N7-->N8

A customized graph with round boxes and named arrows:

>>> def nodefunc(node):
...     return '("%s")' % (node.name)
>>> def edgefunc(node, child):
...     return f"--{child.edge}-->"
>>> options = [
...     "%% just an example comment",
...     "%% could be an option too",
... ]
>>> for line in MermaidExporter(root, options=options, nodefunc=nodefunc, edgefunc=edgefunc):
...     print(line)
graph TD
%% just an example comment
%% could be an option too
N0("root")
N1("sub0")
N2("sub0B")
N3("sub0A")
N4("sub1")
N5("sub1A")
N6("sub1B")
N7("sub1C")
N8("sub1Ca")
N0--2-->N1
N0---->N4
N1--109-->N2
N1---->N3
N4--7-->N5
N4--8-->N6
N4--22-->N7
N7--42-->N8
to_markdown_file(filename)[source]

Write graph to filename.

>>> from anytree import Node
>>> root = Node("root")
>>> s0 = Node("sub0", parent=root)
>>> s0b = Node("sub0B", parent=s0)
>>> s0a = Node("sub0A", parent=s0)
>>> s1 = Node("sub1", parent=root)
>>> s1a = Node("sub1A", parent=s1)
>>> s1b = Node("sub1B", parent=s1)
>>> s1c = Node("sub1C", parent=s1)
>>> s1ca = Node("sub1Ca", parent=s1c)
>>> from anytree.exporter import MermaidExporter
>>> MermaidExporter(root).to_markdown_file("tree.md")