graphviz - How to create a DOT file in Python? -


i have digital circuit simulator , need draw circuit diagram in question (and answer) block diagram layout dot/graphviz

this first encounter dot , graphviz. fortunately dot language specification available , there many examples well.

however 1 detail still unclear me , i'm asking total newbie: have complete data draw graph. how create dot file it?

as text line line?

# simplified pseudocode dotlines = ["digraph circuit {"] node in all_nodes:     dotlines.append("  {}[{}];".format(node.name, node.data)) edge in all_edges:     dotlines.append("  {} -> {};".format(edge.from_name, edge.to_name)) dotlines.append['}'] dot = "\n".join(dotlines) 

or should convert data somehow , use module exports in dot format?

you might consider pygraphviz.

>>> import pygraphviz pgv >>> g=pgv.agraph() >>> g.add_node('a') >>> g.add_edge('b','c') >>> g strict graph {         a;         b -- c; } 

i disagree @matteoitalia's comment (perhaps it's matter of taste). should become familiar available packages task. start off simple graphs, , don't see reason use (very simple) package. @ point, graphs' complexity might grow, you'll keep on rolling own solution readily available.


Comments