ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 16 Sep 2015 22:00:52 +0200load a graph from a dot filehttps://ask.sagemath.org/question/29477/load-a-graph-from-a-dot-file/ Sage allows me to get the representation of a Graph, G, in the [dot language](https://en.wikipedia.org/wiki/DOT_(graph_description_language)) by using the method
> [G.graphviz_string()](http://doc.sagemath.org/html/en/reference/graphs/sage/graphs/generic_graph.html#sage.graphs.generic_graph.GenericGraph.graphviz_string)
I can even have this string written directly to a file by using:
> [G.graphviz_to_file_named('my_graph_file.dot')](http://doc.sagemath.org/html/en/reference/graphs/sage/graphs/generic_graph.html#sage.graphs.generic_graph.GenericGraph.graphviz_to_file_named)
Is there a way of reloading G from such a file? I tried doing
> G = Graph('my_graph_file.dot')
> G = Graph(open('my_graph_file.dot'))
> G = Graph(open('my_graph_file.dot').read())
Where the contents of 'my_graph_file.dot' are:
> graph MyGraph{
> foo -- {bar}
> bar -- {foo; bas}
> bas -- {bar}
> }
But none of these worked.
Wed, 16 Sep 2015 21:47:01 +0200https://ask.sagemath.org/question/29477/load-a-graph-from-a-dot-file/Answer by Nathann for <p>Sage allows me to get the representation of a Graph, G, in the <a href="https://en.wikipedia.org/wiki/DOT_(graph_description_language)">dot language</a> by using the method </p>
<blockquote>
<p><a href="http://doc.sagemath.org/html/en/reference/graphs/sage/graphs/generic_graph.html#sage.graphs.generic_graph.GenericGraph.graphviz_string">G.graphviz_string()</a></p>
</blockquote>
<p>I can even have this string written directly to a file by using:</p>
<blockquote>
<p><a href="http://doc.sagemath.org/html/en/reference/graphs/sage/graphs/generic_graph.html#sage.graphs.generic_graph.GenericGraph.graphviz_to_file_named">G.graphviz_to_file_named('my_graph_file.dot')</a></p>
</blockquote>
<p>Is there a way of reloading G from such a file? I tried doing</p>
<blockquote>
<p>G = Graph('my_graph_file.dot')</p>
<p>G = Graph(open('my_graph_file.dot'))</p>
<p>G = Graph(open('my_graph_file.dot').read())</p>
</blockquote>
<p>Where the contents of 'my_graph_file.dot' are:</p>
<blockquote>
<p>graph MyGraph{</p>
<p>foo -- {bar}</p>
<p>bar -- {foo; bas}</p>
<p>bas -- {bar}</p>
<p>}</p>
</blockquote>
<p>But none of these worked.</p>
https://ask.sagemath.org/question/29477/load-a-graph-from-a-dot-file/?answer=29478#post-id-29478This should work:
sage: import networkx
sage: g = Graph(networkx.read_dot('your_dot_file.dot'))
Currently you can *write* .dot files with Graph.export_to_file but not read them yet. We have "been about to write it" several times now, gut given that none of us apparently needs it, it still isn't actual code.
If you want to start contributing to Sage, that's a good starting point: useful contribution, and not technically complicated.
NathannWed, 16 Sep 2015 22:00:52 +0200https://ask.sagemath.org/question/29477/load-a-graph-from-a-dot-file/?answer=29478#post-id-29478