Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get output of pydot graph without intermediate file?

I have a very simple graph that I want to plot as svg. For example:

# graph.dot
graph { 
        a -- b; 
        b -- c; 
    } 

I am currently using pydot to read the file and then generate the svg file as follows:

import pydot
graphs = pydot.graph_from_dot_file('graph.dot')
graphs[0].write_svg('graph.svg') # there is only 1 graph so the 0 index.

However, I need to do this without the need of intermediate files graph.dot and graph.svg. I have the code content of graph.dot in a string, corresponding to which I need the svg output in string.

I need something like:

graph_dot = "... ..." # string, I have this
graph_svg = convert_dot_to_svg(graph_dot) 
# i need something like convert_dot_to_svg()

My question is not limited to pydot only. If anyone knows a web api using which I can do this, then also it will do.

Thanks a Lot, in advance.

like image 625
Harsh Trivedi Avatar asked Jul 11 '16 07:07

Harsh Trivedi


1 Answers

After spending some more time looking at the available methods on pydot object and graph object, it could be figured out:

The following code works:

import pydot    
dot_string = """graph { 
                    a -- b; 
                    b -- c; 
                } """

graphs = pydot.graph_from_dot_data( dot_string )
svg_string = graphs[0].create_svg() 
like image 117
Harsh Trivedi Avatar answered Nov 03 '22 00:11

Harsh Trivedi