pythongraphvizpygraphviz

Capture pygraphviz image rendering without saving to a file?


Does pygraphviz allow for you to render an image to a variable? I would like to serve up dynamic images via a webpage without having to render the graphs to disk.


Solution

  • According to the source code if you call the draw() method of an AGraph object while omitting the path argument (or setting it to None) it will return a bytes object instead of saving to a file. Do not forget to specify the format parameter.

    For example, assuming that you already have an AGraph object named A, and you want to plot this graph, you could do this:

    from IPython.display import Image, display
    
    img_bytes = A.draw(format="png")
    img = Image(img_bytes)
    display(img)