Graph object cannot have name "graph" as it breaks GraphViz
Naming Graph "graph", eg. graph = pydot.Dot("graph", graph_type="graph")
leads to following Dot string:
graph graph {
1 [label=ROOT];
2 [label=S];
2 -- 1;
3 [label=NP];
3 -- 2;
4 [label=VP];
4 -- 2;
5 [label=DT];
5 -- 3;
6 [label=VBZ];
6 -- 4;
7 [label=NP];
7 -- 4;
8 [label=This];
8 -- 5;
9 [label=is];
9 -- 6;
10 [label=DT];
10 -- 7;
11 [label=NN];
11 -- 7;
12 [label=a];
12 -- 10;
13 [label=test];
13 -- 11;
}
and following GraphViz error on any interaction:
"dot" with args ['-Tpng', '/tmp/tmpa165fw3n'] returned code: 1
stdout, stderr:
b''
b"Error: /tmp/tmpa165fw3n: syntax error in line 1 near 'graph'\n"
Obviously a very minor issue, but confusing for first-time users.
I ran into the same issue when using "subgraph" as a node name. A little digging turned out they are reserved as dot file keywords (for anyone who comes across this PR).
The exception message displayed if a node name is invalid:
"dot" with args ['-Tsvg', 'C:\\Users\\***\\AppData\\Local\\Temp\\tmplrgvkgbm'] returned code: 1
stdout, stderr:
b''
b"Error: C:\\Users\\***\\AppData\\Local\\Temp\\tmplrgvkgbm: syntax error in line 5 near '['\r\n"
I think this might be too cryptic for a dot file noob like me to figure out.
According to that grammar, ID values can optionally be quoted — but since any ID might be a keyword, it's probably a good idea for any code generating graphs to just always quote all of them.
dot can't parse the OP's graph, but it has no issues with this one:
graph "graph" {
"1" [label=ROOT];
"2" [label=S];
"2" -- "1";
"3" [label=NP];
"3" -- "2";
"4" [label=VP];
"4" -- "2";
"5" [label=DT];
"5" -- "3";
"6" [label=VBZ];
"6" -- "4";
"7" [label=NP];
"7" -- "4";
"8" [label=This];
"8" -- "5";
"9" [label=is];
"9" -- "6";
"10" [label=DT];
"10" -- "7";
"11" [label=NN];
"11" -- "7";
"12" [label=a];
"12" -- "10";
"13" [label=test];
"13" -- "11";
}
(It only "needs" to quote the "graph", but @peterhs73 's comment shows why it's safer to just quote ALL of the IDs, even numeric ones. As the grammar says, an ID is just a string; there's no difference between 1 and "1" as an ID. But there is a difference between graph or subgraph and "graph" or "subgraph".)