Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graph object cannot have name "graph" as it breaks GraphViz #282

Open
Claygirl opened this issue Jan 24, 2022 · 3 comments
Open

Graph object cannot have name "graph" as it breaks GraphViz #282

Claygirl opened this issue Jan 24, 2022 · 3 comments

Comments

@Claygirl
Copy link

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"
@Claygirl
Copy link
Author

Obviously a very minor issue, but confusing for first-time users.

@peterhs73
Copy link

peterhs73 commented Feb 2, 2022

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.

@ferdnyc
Copy link
Contributor

ferdnyc commented Dec 8, 2023

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".)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants