8

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.

Harsh Trivedi
  • 1,594
  • 14
  • 27
  • 1
    I've never used pydot (but I do know Graphviz), but a quick look at the pydot docs suggests that you can use a file-like object instead of a file. So you should try StringIO or BytesIO from the standard [io](https://docs.python.org/3/library/io.html) module. – PM 2Ring Jul 11 '16 at 08:04
  • @PM2Ring you are right that I can use `io` to take input. But how to replace the `write_svg` function? – Harsh Trivedi Jul 11 '16 at 08:09
  • @PM2Ring As mentioned, question isn't limited to pydot itself. Can you tell me a way to get the svg string corresponding to dot string in Graphviz. Any way is fine. – Harsh Trivedi Jul 11 '16 at 08:10
  • 1
    If `.write_svg` accepts an open file handle instead of a file name, then it will "just work" if you supply it a StringIO or BytesIO object. If you've never used the `io` module before, I suggest you study the docs & do a few experiments; it's a _very_ useful module. – PM 2Ring Jul 11 '16 at 08:12
  • Ok cool ... Thanks :) – Harsh Trivedi Jul 11 '16 at 08:14
  • Sorry, I don't have any specifc advice about DOT -> SVG conversion; I normally use the Graphviz command line programs to convert a DOT file to SVG. – PM 2Ring Jul 11 '16 at 08:14
  • No problem, and thanks a lot for your time and valuable comments. Finally, solution could be figured out. I have posted the answer on what worked. – Harsh Trivedi Jul 11 '16 at 08:29

1 Answers1

11

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() 
Harsh Trivedi
  • 1,594
  • 14
  • 27
  • FWIW, `pydot` still needs `graphviz` to be installed. Quoted from [this blog post](http://johanlouwers.blogspot.com/2019/01/solved-oserror-errno-2-dot-not-found-in.html): "If we look at the Pydot Pypi page you can see already a hint on this as it will tell you the following; Pydot is an interface to Graphviz and can parse and dump into the DOT language used by Grapgiz. Pydot is written in pure Python." – RayLuo Mar 17 '21 at 05:26