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)

3
  • Possible duplicate of Converting dot to png in python Commented Jul 29, 2019 at 23:41
  • Note that according to the on-topic rules, "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 is probably what you're looking for. Commented Jul 29, 2019 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 Commented Jul 29, 2019 at 23:57

1 Answer 1

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')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.