1

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)

qwertzuiop
  • 685
  • 2
  • 10
  • 24
  • Possible duplicate of [Converting dot to png in python](https://stackoverflow.com/questions/5316206/converting-dot-to-png-in-python) –  Jul 29 '19 at 23:41
  • Note that according to [the on-topic rules](https://stackoverflow.com/help/on-topic), "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam". That being said, [`graphviz`](https://pypi.org/project/graphviz/) is probably what you're looking for. – Hamms Jul 29 '19 at 23:43
  • @JustinEzequiel I don't know if it is really the same question since I try to do it directly from a String, without passing by a file. However, as you can see it in my answer, I didn't find a way to do it without creating a file – qwertzuiop Jul 29 '19 at 23:57

1 Answers1

3

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')