qwertzuiop
qwertzuiop

Reputation: 735

How to generate graph image from DOT code in python?

Is there any python library which is able to generate an image (no matter which format) from DOT code ?

Something like that:

import magic_library
dot_txt = 'digraph G {\n    start -> end;\n}'
magic_library.generateImage(code = dot_txt, file=f.png)

I didn't find anything

EDIT 1: This works but I have to create a file.

import os
import pydot

s = 'digraph G {\n    start -> end;\n}'

text_file = open("f.dot", "w")
text_file.write(s)
text_file.close()

(graph,) = pydot.graph_from_dot_file('f.dot')
graph.write_png('f.png')

os.remove("f.dot")

EDIT 2: The accepted answer works perfectly (and is straightforward, not like my previous code)

Upvotes: 1

Views: 1392

Answers (1)

user5386938
user5386938

Reputation:

From the docs, you should be able to use graph_from_dot_data:

import pydot
dot_txt = 'digraph G {\n    start -> end;\n}'
graph, = pydot.graph_from_dot_data(dot_txt)
graph.write_png('f.png')

Upvotes: 3

Related Questions