Quoting and HTML-like labelsΒΆ

The graph-building methods of Graph and Digraph objects automatically take care of quoting (and escaping quotes) where needed (whitespace, keywords, double quotes, etc.):

>>> import graphviz

>>> q = graphviz.Digraph()

>>> q.edge('spam', 'eggs eggs')
>>> q.edge('node', '"here\'s a quote"')

>>> print(q.source)
digraph {
    spam -> "eggs eggs"
    "node" -> "\"here's a quote\""
}

If a string starts with '<' and ends with '>', it is passed on as is, i.e. without quoting/escaping: The content between the angle brackets is treated by the Graphviz layout engine as special HTML string that can be used for HTML-like labels:

>>> h = graphviz.Graph('html_table')

>>> h.node('tab', label='''<<TABLE>
...  <TR>
...    <TD>left</TD>
...    <TD>right</TD>
...  </TR>
... </TABLE>>''')
_images/html_table.svg

For strings that should literally begin with '<' and end with '>', use the graphviz.nohtml() function to disable the special meaning of angled parenthesis and apply normal quoting/escaping:

>>> d = graphviz.Digraph('diamond', format='svg')

>>> d.node('diamond', label=graphviz.nohtml('<>'))

>>> print(d.source)
digraph diamond {
    diamond [label="<>"]
}
>>> doctest_mark_exe()

>>> d.render(directory='doctest-output').replace('\\', '/')
'doctest-output/diamond.gv.svg'
_images/diamond.svg

Historical note

Before version 0.8.2, the only workaround was to add leading or trailing space (label=' <>'):