graphvizpygraphviz

Pygraphviz: specify dot renderer



I'm trying to render a graph as a SVG. The problem is with PyGraphviz, but I made a dot file to debug/make it easier to share. Note that my label contains text in bold so I have to use HTML-like labels, the table is not mandatory but this is what I read I should use when I searched through similar problems in SO.
digraph test {
    node [shape=plain];
    n1 [label=< 
    <table border="1" cellborder="0" cellspacing="0">
        <tr><td align="center"><b>Not centered when using default renderer for SVG</b></td></tr>
        <tr><td align="text">Lorem ipsum</td></tr>
    </table> >];
}

The graphviz documentation specifies that "In addition, all of these markups are currently only available via the cairo and svg renderers."

Indeed, when I render via the command line with dot -Tsvg ./test.dot -o test1.svg, I get a wrong result with the text not centered in the node. Whereas when I specify the renderer dot -Tsvg:svg ./test.dot -o test2.svg, everything works well.

Now the problem is that calling G.draw('test3.svg', prog='dot') probably calls the first command, which produces the wrongly aligned result.
I tried to specify the renderer with G.draw('test4.svg', prog='dot', args='-Tsvg:svg') or G.draw('test5.svg', prog='dot', format='svg:svg') but in both cases the produced svg is invalid (there's two <?xml ...> tags for some reason).

So is there a way to specify the renderer via pygraphviz? Or should I dump the graph in a .dot file and call dot with subprocess.run and the correct arguments?

Thanks


Solution

  • After checking PyGraphviz's source code, I managed to make it work with G.draw("test.svg", prog= 'dot', format='svg:cairo').

    The format argument is deduced from the filename when it's not provided which caused Dot to output the graph twice in the same SVG file.