Backslash escapesΒΆ

The Graphviz layout engines support a number of escape sequences such as \n, \l, \r (for placement of multi-line labels: centered, left-justified, right-justified) and \N, \G, \L (expanded to the current node name, graph name, object label). To be able to use them from this library (e.g., for labels), backslashes in strings are (mostly) passed on as is.

Attention

This means that literal backslashes need to be escaped (doubled) by the user. As the backslash is also special in Python string literals a second level of doubling is needed. E.g. label='\\\\' for a label that is rendered as single literal backlash: \.

Tip

Doubling of backslashes can be avoided by using raw string literals (r'...') instead. This is similar to the solution proposed for the stdlib re module. See also https://en.wikipedia.org/wiki/Leaning_toothpick_syndrome.

>>> import graphviz

>>> e = graphviz.Digraph('escapes')

>>> e.node('backslash', label=r'\\')
>>> e.node('multi_line', label=r'centered\nleft\lright\r')

>>> print(e.source)
digraph escapes {
    backslash [label="\\"]
    multi_line [label="centered\nleft\lright\r"]
}
_images/escapes.svg

To disable any special character meaning in a string (e.g. from user input to be rendered literally), use the graphviz.escape() function (similar to the re.escape() function):

>>> bs = graphviz.Digraph('literal_backslash')

>>> bs.node(graphviz.escape('\\'))

>>> print(bs.source)
digraph literal_backslash {
    "\\"
}
>>> doctest_mark_exe()  # skip this line

>>> bs.render(format='svg', directory='doctest-output').replace('\\', '/')
'doctest-output/literal_backslash.gv.svg'
_images/literal_backslash.svg

Historical note

To prevent breaking the internal quoting mechanism, the special meaning of \" as a backslash-escaped quote has been disabled since version 0.14 of this library. E.g. both label='"' and label='\\"' now produce the same DOT source [label="\""] (a label that renders as a literal quote). See also examples/graphviz-escapes.ipynb (nbviewer).