xflr6 / graphviz

Simple Python interface for Graphviz
https://graphviz.readthedocs.io
MIT License
1.65k stars 213 forks source link

Add DOT file import #135

Closed serenalotreck closed 3 years ago

serenalotreck commented 3 years ago

I'm writing a pipeline that creates a .gv file using this library. Later on, I want to be able to import the .gv file produced as a Digraph object, and make a new Digraph object and corresponding .gv file with a subset of the nodes/edges from the original graph.

As far as I can tell, there is no way to do this currently. This is the only reference I was able to find in the docs regarding using an existing DOT file, and it only concerns rendering a graph, and does not read it in as a Graph/Digraph object.

Is there a way to do this currently? And if not, if there a possibility of adding this feature?

Desired behavior: Provide a file path of a valid DOT file to the Graph or Digraph constructor, to instantiate a class instance containing the nodes/edges from the DOT file as instance attributes (edge_attr, node_attr)

EDIT: I found this stack overflow post which shows how to import from a file. However, this only creates an instance of the Source class, that has the attribute source, with the entire DOT string. Since I want to access individual lines of the original string, this is an inconvenient form for the information -- it's easier for me to skip using this and simply read the file in line by line. I would still like to be able to import the file as a Graph or Digraph object to be able to more easily access the edges and nodes.

xflr6 commented 3 years ago

Confirming that parsing DOT source code is out-of-scope for this library, i.e. it is strictly for creating and rendering graphs and not for parsing DOT into a rich datastructure that supports editing or transformations (it handles DOT source as a simple collection of lines; so in simple cases some users might get away with string-transformations directly on the lines but that might not be the best way to do this). There are other libraries which support that: https://graphviz.readthedocs.io/en/stable/#see-also.

I found this stack overflow post which shows how to import from a file.

See also https://graphviz.readthedocs.io/en/stable/manual.html#existing-files (note that this section is about rendering existing DOT source, not about parsing into a Graph instance, which is out-of-scope):

To directly display the graph of an existing DOT source file inside a Jupyter notebook or Qt Console, you can use the Source.from_file()-classmethod (alternate constructor):

xflr6 commented 3 years ago

I'm writing a pipeline that creates a .gv file using this library. Later on, I want to be able to import the .gv file produced as a Digraph object, and make a new Digraph object and corresponding .gv file with a subset of the nodes/edges from the original graph.

Just an idea: If you are assembling the Graph object with this library, maybe in practise it would just be a matter of materializing the arguments for the .node() and .edge() method-calls into one or more collections in your code (or what ever data structure you have that decides what nodes and edges are added), to be able to apply all of them or only a subset?