treetrace.ext.graphviz

 1import subprocess
 2
 3from ..tracing.data import DataWithMime
 4
 5
 6def dot_to_png(dot_content: str) -> DataWithMime:
 7    p = subprocess.Popen(
 8        ["dot", "-Tpng"],
 9        stdout=subprocess.PIPE,
10        stdin=subprocess.PIPE,
11        stderr=subprocess.PIPE,
12    )
13    (stdout, stderr) = p.communicate(dot_content.encode())
14    if p.returncode != 0:
15        raise Exception(f"Calling 'dot' failed:\n{stderr}")
16    return DataWithMime(stdout, mime_type="image/png")