3

Does anyone know a trick how to convert a Digraph into a io.StringIO png? The only code I could find is to save it to disk, but I would like to leave out any disk usage and to process it in memory instead:

from graphviz import Digraph
import io

dot = Digraph(comment='The Round Table')
dot.node('A', 'King Arthur')

# instead of this...
dot.render('test-output/round-table.gv', view=True)

# ... I need something like this:
data = io.StringIO()
dot.export_to_png(dot)
Daniel Stephens
  • 2,371
  • 8
  • 34
  • 86

1 Answers1

2

Something like this?

from graphviz import Digraph
import io

dot = Digraph(comment='The Round Table', format='gv')
dot.node('A', 'King Arthur')

data = io.StringIO()

print("writing")
data.write( dot.pipe().decode('utf-8') )

print("reading")
data.seek(0)
print(data.read())

# print(data.getvalue())


data.close()
carlsborg
  • 2,628
  • 19
  • 21
  • 1
    Perfect! That's it. But just wondering, some characters might be not available in utf-8, so I just used latin-1 instead and `pipe(format="png")` instead. Anyway, that worked perfectly! Thanks – Daniel Stephens Dec 16 '18 at 21:42