Existing filesΒΆ

To directly render an existing DOT source file (e.g. created with other tools), you can use the graphviz.render() function.

>>> doctest_mark_exe()

>>> import pathlib
>>> import graphviz

>>> src = 'digraph "the holy hand grenade" { rankdir=LR; 1 -> 2 -> 3 -> lob }'
>>> filepath = pathlib.Path('doctest-output/the-holy-hand-grenade.gv')
>>> filepath.write_text(src, encoding='ascii')
66

>>> graphviz.render('dot', 'png', filepath).replace('\\', '/')
'doctest-output/the-holy-hand-grenade.gv.png'

To directly display the rendered visualization of an existing DOT source file inside a Jupyter notebook or Qt Console, you can use graphviz.Source.from_file() (alternative constructor):

_images/qtconsole-source.png

Note that render() and view() on Source instances returned by graphviz.Source.from_file() skip writing the loaded file back. The same holds for save(). The instances resolve default .save(skip_existing=None) to .save(skip_existing_run=True) to skip writing the read source back into the same file (specifically the same path that it was loaded from). Call .save(skip_existing=False) if you want to re-write the loaded source.

Historical note

Before version 0.18 of this library, Source.save(), Source.render(), and Source.view(), wrote the content read into source back into the file. It was advised to use graphviz.render() and graphviz.view() to directly work on files if the superflous saving needed to be avoided.