PyGraphviz requires:
-
Python (version 3.10, 3.11, or 3.12)
-
Graphviz (version 2.46 or later)
-
C/C++ Compiler
Note
These instructions assume you have Python and a C/C++ Compiler on your computer.
Warning
Do not use the default channels to install pygraphviz with conda
. The
conda-forge channel should be used instead:
conda install --channel conda-forge pygraphviz
Recommended#
We recommend installing Python packages using pip and virtual environments.
Linux#
We recommend installing Graphviz using your Linux system’s package manager.
Below are examples for some popular distributions.
Ubuntu and Debian#
$ sudo apt-get install graphviz graphviz-dev $ pip install pygraphviz
Fedora and Red Hat#
You may need to replace dnf
with yum
in the example below.
$ sudo dnf install graphviz graphviz-devel $ pip install pygraphviz
macOS#
We recommend installing Graphviz using the Homebrew package manager or MacPorts for macOS.
Homebrew#
$ brew install graphviz $ pip install pygraphviz
Note
Graphviz may be installed in a location that is not on the default search
path.
In this case, it may be necessary to manually specify the path to the
graphviz include and/or library directories, e.g.
pip install --config-settings="--global-option=build_ext" \ --config-settings="--global-option=-I$(brew --prefix graphviz)/include/" \ --config-settings="--global-option=-L$(brew --prefix graphviz)/lib/" \ pygraphviz
See the Advanced section for details.
MacPorts#
$ port install graphviz $ pip install pygraphviz $ pip install --config-settings="--global-option=build_ext" \ --config-settings="--global-option=-I/opt/local/include/" \ --config-settings="--global-option=-L/opt/local/lib/" \ pygraphviz
Advanced#
- The two main difficulties are
-
-
installing Graphviz and
-
informing pip where Graphviz is installed.
-
Providing path to Graphviz#
If you’ve installed Graphviz and pip
is unable to find Graphviz, then you need to
provide pip
with the path(s) where it can find Graphviz.
To do this, you first need to figure out where the binary files, includes files, and
library files for Graphviz are located on your file system.
Once you know where you’ve installed Graphviz, you will need to do something like
the following. There is an additional example using Chocolatey on Windows further
down the page.
Windows#
Historically, installing Graphviz and PyGraphviz on Windows has been challenging.
Fortunately, the Graphviz developers are working to fix this and
their recent releases have much improved the situation.
For this reason, PyGraphviz 1.7 only supports Graphviz 2.46.0 or higher on Windows.
We recommend either manually installing the official binary release of Graphviz or
using Chocolatey, which has been updated to Graphviz 2.46.0.
You may also need to install Visual C/C++, e.g. from here:
https://visualstudio.microsoft.com/visual-cpp-build-tools/
Assuming you have Python and Visual C/C++ installed,
we believe the following should work on Windows 10 (64 bit) using PowerShell.
Manual download#
-
Download and install 2.46.0 for Windows 10 (64-bit):
stable_windows_10_cmake_Release_x64_graphviz-install-2.46.0-win64.exe. -
Install PyGraphviz via
PS C:\> python -m pip install --config-settings="--global-option=build_ext" ` --config-settings="--global-option=-IC:\Program Files\Graphviz\include" ` --config-settings="--global-option=-LC:\Program Files\Graphviz\lib" ` pygraphviz
Chocolatey#
PS C:\> choco install graphviz PS C:\> python -m pip install --config-settings="--global-option=build_ext" ` --config-settings="--global-option=-IC:\Program Files\Graphviz\include" ` --config-settings="--global-option=-LC:\Program Files\Graphviz\lib" ` pygraphviz
FAQ#
- Q:
I followed the installation instructions but when I do:
I get an error like:
ImportError: libagraph.so.1: cannot open shared object file: No
such file or directoryWhat is wrong?
- A:
Some Unix systems don’t include the Graphviz library in the
default search path for the run-time linker. The path is often
something like/usr/lib/graphviz
or/sw/lib/graphviz
etc. and
it needs to be added to your search path.
On *nix systems, the preferred way to do this is by setting the
appropriate flags when building/installingpygraphviz
.
For example, if the Graphviz libraries are installed in
/opt/lib/mygviz/
on your system:pip install --global-option=build_ext \ --global-option="-L/opt/lib/mygviz/" \ --global-option="-R/opt/lib/mygviz/" \ pygraphvizIn this example, the
-L
and-R
flags tell the linker where to
look for the required Graphviz libraries at build time and run time,
respectively.- Q:
How do I compile pygraphviz under Windows?
- A:
See Windows for the latest on how to
install Graphviz and pygraphviz on Windows.- Q:
Why don’t you distribute a pygraphviz Windows installer?
- A:
We would very much like to make binary wheels available for
pygraphviz
,
but there are several complications.
pygraphviz
is a wrapper around Graphviz, which means that Graphviz
must be installed, and Graphviz header files, libraries and command
line executables must all be accessible for the wrapper.
The recommended use of the Graphviz CLI poses challenges for wheel
packaging.See also
This GitHub issue
for further discussion on wheels and packaging.
Description
The Confluence server must have the Graph Visualization Software (Graphviz) package installed in order for the Graphviz Diagrams for Confluence macros to work. The Graphviz package is free and supports a variety of operating systems including those supported by Confluence.
Test your Graphviz installation
After you have installed Graphviz, make sure the dot executable run from a command line using the same environment (especially the path) used to startup Confluence.
If Confluence cannot find the executable, you will get macro errors in Confluence like — No useable executable name defined in graphviz.properties…
Downloading Graphviz
Before you can use Graphviz Diagrams for Confluence, you will need to download and install Graph Visualization Software (Graphviz) on the system where you are running Confluence. Download the correct version of the software that matches your environment. You can find the available versions on the Graphviz Downloads page. Once the download is complete then following the instructions below for your operating system.
Graphviz on Windows
- Download the installer from this link
- The default installation path will be C:\Program Files (x86)\GraphvizX.XX\bin (Example: GraphvizX.XX → Graphviz2.38)
-
Open cmd window as administrator and go the location C:\Program Files (x86)\GraphvizX.XX\bin and run the below command:
-
Exit the command window.
-
Go to the Control Panel → System and Security → System, and on the right side navigation panel, you will see the link Advanced systems settings.
-
Once there in advance settings, a dialogue box will open which will show the button Environment Variables. Click on the button Environment Variables.
-
Select the entry «Path» on the system variables section and add C:\Program Files (x86)\GraphvizX.XX\bin to the existing path. Please see the below screenshot for reference.
Note: Please make sure you add «;» at the PATH which is already existing under User Variables and mention the path C:\Program Files (x86)\GraphvizX.XX\bin. Also please add a New path on the system variables section and add the Path C:\Program Files (x86)\GraphvizX.XX\bin - Click on Ok Button.
- Restart Confluence from a new cmd window.
- Go to the system information page on confluence instance.
- Verify if you can see C:\Program Files (x86)\GraphvizX.XX\bin on «Path» attribute on system information page in confluence.
- If the path is available, that means that the configuration changes are successful.
- Go the page where you have used the macro and verify if the add-ons work.
- Restart Confluence
-
Verify it works by creating a page with a simple flowchart macro like:
{flowchart} A -> B {flowchart}
Graphviz on Mac OSX
- Follow the instructions mentioned here.
- The installer should set up everything so that the dot appears under on the path
Graphviz on Linux and Unix
- Install the graphviz.rpm, the graphviz-graphs.rpm, and the graphviz-gd.rpm packages
- Open the directory GraphViz is installed under and confirm the path
- Log in as the user who runs Confluence or starts the Confluence service
- Append the path to the
bin
directory of GraphViz to the ‘Path’ variable
Last resort
The method mentioned in this section is not required and not recommended in most cases, but, if you can’t get your path set correctly to find the dot executable, then this is an alternative.
If you get errors about not finding dot.exe
- Download the add-on jar file
- Unzip the jar to a temporary directory
- Edit the graphviz.properties file
-
Replace dot.candidate.paths setting with something like (yes, use forward slashes ):
dot.candidate.paths=c:/PROGRA~1/Graphviz2.16/bin/dot.exe
-
- Zip all files in the temporary directory to create a new jar file
- Uninstall the Graphviz App and re-install your customized version
- GVIZ-1 will make this easier
Installation¶
graphviz provides a simple pure-Python interface for the Graphviz
graph-drawing software. It runs under Python 3.8+. To install it with pip, run
the following:
For a system-wide install, this typically requires administrator access. For an
isolated install, you can run the same inside a venv
or a virtualenv.
The only dependency is a working installation of Graphviz
(download page,
archived versions,
installation procedure for Windows).
After installing Graphviz, make sure that its bin/
subdirectory containing
the dot
layout command for rendering graph descriptions
is on your systems’ PATH
(sometimes done by the installer;
setting PATH
on Linux,
Mac,
and Windows):
On the command-line, dot -V
should print the version of your Graphiz installation.
Platform: Windows
Windows users might want to check the status of known issues
(gvedit.exe, sfdp, commands) and consider trying an older archived
version as a workaround (e.g. graphviz-2.38.msi).
Basic usage¶
The graphviz package provides two main classes: graphviz.Graph
and graphviz.Digraph
. They create graph descriptions in the DOT language for
undirected and directed graphs respectively.
They have the same API.
Create a graph by instantiating a new Graph
or
Digraph
object:
>>> import graphviz >>> dot = graphviz.Digraph('round-table', comment='The Round Table') >>> dot <graphviz.graphs.Digraph object at 0x...>
Their constructors allow to set the graph’s name
identifier,
the filename
for the DOT source and the rendered graph,
an optional comment
for the first source code line, etc.
Add nodes and edges to the graph object using its node()
and edge()
or edges()
methods:
>>> dot.node('A', 'King Arthur') >>> dot.node('B', 'Sir Bedevere the Wise') >>> dot.node('L', 'Sir Lancelot the Brave') >>> dot.edges(['AB', 'AL']) >>> dot.edge('B', 'L', constraint='false')
The node()
method takes a name
identifier as first argument
and an optional label
.
The edge()
method takes the names of start node and end node,
while edges()
takes an iterable of name pairs.
Keyword arguments are turned into (node and edge) attributes
(see extensive Graphviz docs on available attributes).
Check the generated DOT source code:
>>> print(dot.source) // The Round Table digraph "round-table" { A [label="King Arthur"] B [label="Sir Bedevere the Wise"] L [label="Sir Lancelot the Brave"] A -> B A -> L B -> L [constraint=false] }
Use the render()
method to save the DOT source code
and render it with the default dot
layout engine
(see below for using other layout engines).
Attention
Skip/ignore any doctest_mark_exe()
lines in documentation code examples.
>>> doctest_mark_exe() # skip this line >>> dot.render(directory='doctest-output').replace('\\', '/') 'doctest-output/round-table.gv.pdf'
Passing view=True
will automatically open the resulting (PDF, SVG, PNG,
etc.) file with your system’s default viewer application
for the rendered file type.
>>> doctest_mark_exe() # skip this line >>> dot.render(directory='doctest-output', view=True) 'doctest-output/round-table.gv.pdf'
Attention
Backslash-escapes and strings of the form <...>
have a special meaning in the DOT language
and are currently passed on as is by this library.
If you need to render arbitrary strings literally (e.g. from user input),
consider wrapping them with the graphviz.escape()
function first.
See the sections on Backslash escapes
and Quoting and HTML-like labels below for details.
Formats¶
To use a different output file format than the default PDF,
you can use the format
argument when creating your Graph
or Digraph
object:
>>> import graphviz >>> g = graphviz.Graph(format='png')
You can also change the format
attribute on an existing graph
object:
>>> doctest_mark_exe() # skip this line >>> dot = graphviz.Digraph('hello') >>> dot.edge('hello', 'world') >>> dot.format = 'svg' >>> dot.render(directory='doctest-output').replace('\\', '/') 'doctest-output/hello.gv.svg'
Piped output¶
To directly access the raw results from the Graphviz dot
layout command as binary bytes
or as decoded str
(for plain-text formats like SVG)
instead of writing to a file, use the pipe()
method
of your Graph
or Digraph
object:
>>> import graphviz >>> h = graphviz.Graph('hello', format='svg') >>> h.edge('Hello', 'World')
>>> doctest_mark_exe() # skip this line >>> h.pipe(format='pdf')[:4] b'%PDF' >>> print(h.pipe(encoding='utf-8')) <?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE svg ... </svg>
Tip
Because pipe()
returns the raw stdout
from the layout subprocess by default (bytes
),
you usually want to decode the return value
when piping into formats like 'svg'
or 'plain'
,
Caution
The output for pipe()
is buffered in memory,
so avoid this method if the data size is large.
Jupyter notebooks¶
Graph
and Digraph
objects have a
_repr_mimebundle_()
method so they can be rendered and displayed
directly inside a Jupyter notebook.
For an example, check the examples/graphviz-notebook.ipynb
file
in the source repository/distribution
(or the same notebook in nbviewer).
This also allows direct displaying within the Jupyter Qt Console
(also the one inside Spyder IDE):
By default _repr_mimebundle_()
uses 'svg'
format.
You can use the graphviz.set_jupyter_format()
to override the default format
that is used for displaying in IPython/Jupyter.
(example,
nbviewer).
Hint
You can also use display_svg()
, display_png()
, or .display_jpeg()
from IPython.display to display the rendered
Graph
or Digraph
as SVG, PNG or JPEG
in IPython/Jupyter.
Styling¶
Use the graph_attr
, node_attr
, and edge_attr
arguments
of the Graph
and Digraph
constructors to change
the default attributes for your graph, nodes, and edges.
>>> import graphviz >>> ps = graphviz.Digraph('pet-shop', node_attr={'shape': 'plaintext'}) >>> ps.node('parrot') >>> ps.node('dead') >>> ps.edge('parrot', 'dead')
After creation, the graph_attr
, node_attr
, and
edge_attr
attributes be edited on instances:
>>> ps.graph_attr['rankdir'] = 'LR' >>> ps.edge_attr.update(arrowhead='vee', arrowsize='2') >>> print(ps.source) digraph "pet-shop" { graph [rankdir=LR] node [shape=plaintext] edge [arrowhead=vee arrowsize=2] parrot dead parrot -> dead }
Attributes¶
To directly add DOT att_stmt
attribute statements,
call the attr()
method
of the Graph
or Digraph
instance
with the wanted target as first argument and the attributes as keyword args.
Hint
Attribute statements affect all later graphs, nodes, or edges
within the same (sub-)graph.
>>> import graphviz >>> ni = graphviz.Graph('ni') >>> ni.attr('node', shape='rarrow') >>> ni.node('1', 'Ni!') >>> ni.node('2', 'Ni!') >>> ni.node('3', 'Ni!', shape='egg') >>> ni.attr('node', shape='star') >>> ni.node('4', 'Ni!') >>> ni.node('5', 'Ni!')
If you omit the first attr()
argument, the method can be used
to set arbitrary attributes as key-value pairs targeting
the current (sub-)graph
(e.g. for rankdir
, label
,
or setting rank='same'
within a subgraph context,
example):
>>> ni.attr(rankdir='LR') >>> ni.edges(['12', '23', '34', '45']) >>> print(ni.source) graph ni { node [shape=rarrow] 1 [label="Ni!"] 2 [label="Ni!"] 3 [label="Ni!" shape=egg] node [shape=star] 4 [label="Ni!"] 5 [label="Ni!"] rankdir=LR 1 -- 2 2 -- 3 3 -- 4 4 -- 5 }
Node ports & compass¶
The edge()
and edges()
methods use the
colon-separated node[:port[:compass]]
format
for tail
and head
nodes.
This allows to specify an optional node port
plus an optional compass
point the edge should aim at
for the given tail or head node (example).
Caution
As colons are used to indicate port
and compass
for edges,
node names containing one or more literal colons :
are currently not supported.
GH #54
Tip
There is no such restriction for the label
argument,
so you can work around by choosing a colon-free name
together with the wanted label
as demonstrated below
>>> import graphviz >>> cpp = graphviz.Digraph('C++') >>> cpp.node('A', 'std::string') >>> cpp.node('B', '"spam"') >>> cpp.edge('A', 'B') >>> print(cpp.source) digraph "C++" { A [label="std::string"] B [label="\"spam\""] A -> B }
Backslash escapes¶
The Graphviz layout engines support a number of
escape sequences such as \n
, \l
, \r
(for placement of multi-line labels:
centered, left-justified, right-justified)
and \N
, \G
, \L
(expanded to the current node name, graph name, object label).
To be able to use them from this library (e.g., for labels),
backslashes in strings are (mostly) passed on as is.
Attention
This means that literal backslashes need to be escaped (doubled) by the user.
As the backslash is also special in Python string
literals
a second level of doubling is needed.
E.g. label='\\\\'
for a label that is rendered as single literal backlash: \
.
>>> import graphviz >>> e = graphviz.Digraph('escapes') >>> e.node('backslash', label=r'\\') >>> e.node('multi_line', label=r'centered\nleft\lright\r') >>> print(e.source) digraph escapes { backslash [label="\\"] multi_line [label="centered\nleft\lright\r"] }
To disable any special character meaning in a string
(e.g. from user input to be rendered literally),
use the graphviz.escape()
function
(similar to the re.escape()
function):
>>> bs = graphviz.Digraph('literal_backslash') >>> bs.node(graphviz.escape('\\')) >>> print(bs.source) digraph literal_backslash { "\\" }
>>> doctest_mark_exe() # skip this line >>> bs.render(format='svg', directory='doctest-output').replace('\\', '/') 'doctest-output/literal_backslash.gv.svg'
Historical note
To prevent breaking the internal quoting mechanism, the special meaning
of \"
as a backslash-escaped quote has been disabled
since version 0.14
of this library.
E.g. both label='"'
and label='\\"'
now produce the same DOT source [label="\""]
(a label that renders as a literal quote).
See also examples/graphviz-escapes.ipynb
(nbviewer).
Quoting and HTML-like labels¶
The graph-building methods of Graph
and Digraph
objects
automatically take care of quoting (and escaping quotes) where needed
(whitespace, keywords, double quotes, etc.):
>>> import graphviz >>> q = graphviz.Digraph() >>> q.edge('spam', 'eggs eggs') >>> q.edge('node', '"here\'s a quote"') >>> print(q.source) digraph { spam -> "eggs eggs" "node" -> "\"here's a quote\"" }
If a string starts with '<'
and ends with '>'
,
it is passed on as is, i.e. without quoting/escaping:
The content between the angle brackets is treated by
the Graphviz layout engine as special HTML string
that can be used for HTML-like labels:
>>> h = graphviz.Graph('html_table') >>> h.node('tab', label='''<<TABLE> ... <TR> ... <TD>left</TD> ... <TD>right</TD> ... </TR> ... </TABLE>>''')
For strings that should literally begin with '<'
and end with '>'
,
use the graphviz.nohtml()
function to disable the special meaning of angled
parenthesis and apply normal quoting/escaping:
>>> d = graphviz.Digraph('diamond', format='svg') >>> d.node('diamond', label=graphviz.nohtml('<>')) >>> print(d.source) digraph diamond { diamond [label="<>"] }
>>> doctest_mark_exe() # skip this line >>> d.render(directory='doctest-output').replace('\\', '/') 'doctest-output/diamond.gv.svg'
Historical note
Before version 0.8.2
,
the only workaround was to add leading or trailing space
(label=' <>'
):
Subgraphs & clusters¶
Graph
and Digraph
objects have a
subgraph()
method for adding a subgraph to the instance.
There are two ways to use it: Either with a ready-made instance
of the same kind as the only argument (whose content is added as a subgraph)
or omitting the graph
argument
(returning a context manager for defining the subgraph content more elegantly
within a with
-block).
First option, with graph
as the only argument:
>>> import graphviz >>> p = graphviz.Graph(name='parent') >>> p.edge('spam', 'eggs') >>> c = graphviz.Graph(name='child', node_attr={'shape': 'box'}) >>> c.edge('foo', 'bar') >>> p.subgraph(c)
Second usage, with a with
-block (omitting the graph
argument):
>>> p = graphviz.Graph('parent') >>> p.edge('spam', 'eggs') >>> with p.subgraph(name='child', node_attr={'shape': 'box'}) as c: ... c.edge('foo', 'bar')
Both produce the same result:
>>> print(p.source) graph parent { spam -- eggs subgraph child { node [shape=box] foo -- bar } }
Tip
If the name
of a subgraph begins with 'cluster'
(all lowercase),
the layout engine treats it as a special cluster subgraph
(example ).
See the Subgraphs and Clusters section in DOT language.
When subgraph()
is used as a context manager,
the new graph instance is created with strict=None
copying the parent graph values for directory
,
engine
, format
, renderer
, formatter
,
and encoding
:
>>> doctest_mark_exe() # skip this line >>> p = graphviz.Graph('parent', directory='doctest-output') >>> with p.subgraph(name='child') as c: ... c.edge('bacon', 'eggs') ... c.render().replace('\\', '/') 'doctest-output/child.gv.pdf'
Note
These copied attributes are only relevant for rendering the subgraph
independently (i.e. as a stand-alone graph) from within the with
-block.
Engines¶
To use a different layout engine than the default dot
when rendering your graph, you can use the engine
argument
on the constructor of Graph
or Digraph
.
>>> import graphviz >>> g = graphviz.Graph(engine='neato')
You can also change the engine
attribute
on an existing instance:
neato
no-op flag¶
The neato layout engine
supports an additional rendering flag
that allows more control over the node positioning and the edge layout
via the pos,
overlap,
and splines attributes.
Use the neato_no_op
keyword argument of
render()
or pipe()
to pass it to the layout command:
>>> doctest_mark_exe() # skip this line >>> import graphviz >>> n = graphviz.Digraph(name='splines', engine='neato', ... graph_attr={'splines': 'true'}, ... node_attr={'shape': 'point'}) >>> n.node('a', pos='0,0!', color='blue') >>> n.node('b', pos='100,0!', color='green') >>> n.node('c', pos='50,50!', color='red') >>> n.edge('a', 'b', pos='0,0 30,66 70,60 100,0') >>> n.render(neato_no_op=2, directory='doctest-output').replace('\\', '/') 'doctest-output/splines.gv.pdf'
Unflatten¶
To preprocess the DOT source of a Graph
or Digraph
with
the unflatten preprocessor
(manpage, PDF),
use the unflatten()
method.
>>> import graphviz >>> w = graphviz.Digraph('wide') >>> w.edges(('0', str(i)) for i in range(1, 10))
>>> doctest_mark_exe() # skip this line >>> w.view()
Hint
unflatten()
improves the aspect ratio of graphs
with many leaves or disconnected nodes.
>>> u = w.unflatten(stagger=3)
>>> doctest_mark_exe() # skip this line >>> u.view()
The method returns a Source
object
that you can render()
, view()
, etc.
with the same basic API as Graph
or Digraph
objects
(minus modification, see details below).
>>> u = w.unflatten(stagger=2) >>> u <graphviz.sources.Source object at 0x...>
>>> doctest_mark_exe() # skip this line >>> u.view()
Custom DOT statements¶
To add arbitrary statements to the created DOT source, you can use the
body
attribute of Graph
and Digraph
objects.
It holds the verbatim list
of (str
) lines to be written to the source file
(including their final newline).
Use its append()
or extend()
method:
>>> import graphviz >>> rt = graphviz.Digraph(comment='The Round Table') >>> rt.body.append('\t"King Arthur" -> {\n\t\t"Sir Bedevere", "Sir Lancelot"\n\t}\n') >>> rt.edge('Sir Bedevere', 'Sir Lancelot', constraint='false') >>> print(rt.source) // The Round Table digraph { "King Arthur" -> { "Sir Bedevere", "Sir Lancelot" } "Sir Bedevere" -> "Sir Lancelot" [constraint=false] }
Attention
Note that you might need to correctly quote/escape identifiers
and strings containing whitespace or other special characters
when using this method.
Using raw DOT¶
To render a ready-made DOT source code string
(instead of assembling one with the higher-level interface
of Graph
or Digraph
),
create a graphviz.Source
object holding your DOT string:
>>> import graphviz >>> src = graphviz.Source('digraph "the holy hand grenade" { rankdir=LR; 1 -> 2 -> 3 -> lob }') >>> src <graphviz.sources.Source object at 0x...>
Use the render()
method to save and render it:
>>> doctest_mark_exe() # skip this line >>> src.render('doctest-output/holy-grenade.gv').replace('\\', '/') 'doctest-output/holy-grenade.gv.pdf'
>>> doctest_mark_exe() # skip this line >>> src.render('doctest-output/holy-grenade.gv', view=True).replace('\\', '/') 'doctest-output/holy-grenade.gv.pdf'
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() # skip this line >>> 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):
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.
Integration with viewers¶
On platforms such as Windows, viewer programs opened by render()
with view=True
(or equivalently with the view()
shortcut-method)
might lock the (PDF, PNG, etc.) file for as long as the viewer is open
(blocking re-rendering it with a Permission denied
error).
Tip
You can use the tempfile.mktemp()
function
from the stdlib tempfile
module to render to a different file
for each invocation. This avoids needing to close the viewer window
each time within such an incremental workflow
(and also serves to preserves the intermediate steps).
>>> import tempfile >>> import graphviz >>> g = graphviz.Graph() >>> g.node('spam')
>>> doctest_mark_exe() # skip this line >>> g.view(tempfile.mktemp('.gv')) 'C:\\Users\\User\\AppData\\Local\\Temp\\tmp3aoie8d0.gv.pdf' >>> g.view(tempfile.mktemp('.gv')) 'C:\\Users\\User\\AppData\\Local\\Temp\\tmphh4ig7a_.gv.pdf'
Other options:
-
use a viewer that support live updates
-
use the Jupyter notebook or Qt Console
(display the current version of the rendered graph
in repeated add/render/view cycles)
For the next two lectures we will be using the GraphViz tool suite for visualising graph-like
data structures. GraphViz is available on all modern operating
systems and is easy to install. Below, I provide brief instructions on
how to obtain it. As its input, GraphViz accepts a text file in a
special format, which it can then convert to an image of a graph,
taking care of positioning the nodes and rendering the edges between
them. Some examples for using GraphViz can be found by this link.
11.5.1. Microsoft Windows 10¶
Assuming that you have WSL and Ubuntu Linux installed, execute the following commands from the terminal:
sudo apt install -y graphviz evince
This will install GraphViz as well as the Evince viewer for PDF documents.
Once complete, download this file. You can do it from the command line as follows:
wget https://ilyasergey.net/YSC2229/resources/graph.dot
And execute the following command from the command line:
dot -Tpdf graph.dot -o graph.pdf
This should produce the file graph.pdf
, which you should be view by calling:
If it looks like a graph with four nodes, you’re all set for the next
lecture. Don’t worry if your WSL window turns black after that (it
happened to me once after performing this operation). Just restart
your Ubuntu XServer as usual and it should work fine.
You may also open graph.pdf
with your Windows PDF viewer (e.g.,
Adobe Acrobat) by opening the current folder in Windows Explorer. Just
type in the terminal:
11.5.2. Linux¶
Follow the same steps as for MS Windows 10 above, using the corresponding package manager.
11.5.3. Mac OS X¶
Install GraphViz by executing the following command in the terminal:
brew install graphviz wget
The second utility wget
allows download files from the command line.
For convenience of viewing PDF files add the following line into your configuration file ~/.profile
:
alias preview='open -a Preview'
After that, open a new terminal window or tab and download this file. You can do it from the command line as follows:
wget https://ilyasergey.net/YSC2229/resources/graph.dot
And execute the following command from the command line:
dot -Tpdf graph.dot -o graph.pdf
This should produce the file graph.pdf
, which you should be view by calling:
If it looks like a graph with four nodes, you’re all set for the next lecture.
Skip to content
Graphviz installation happens in two parts:
- Graphviz System Library
- Graphviz Python Library
Now, if you use the Anaconda environment, then the installation becomes quite straightforward. All you need to run is
conda install -c conda-forge python-graphviz
This will install both the system library and the python library. However, if you are looking for the pip install, method, then follow the steps below:
- Go to the downloads page of Graphviz, and download the executable for Windows (depending on your PC type, 64-bit or 32-bit).
- Run the .exe file. You will be asked whether you want to add graphviz to the system PATH. Either select the option for ‘ALL Users’ or ‘Current User’.
- Complete the installation. This takes care of the graphviz system library
- For the graphviz python library, run the following pip command:
pip install graphviz
- Restart the IDE on which you wish to run Graphviz. If that doesn’t work, restart your PC and try again.
Note: If you just install the graphviz python library and not the system library, or if you don’t add the system library to the system PATH, you will get the following error on trying to run, say .visualize() on a dask dataframe:
ExecutableNotFound: failed to execute ['dot', '-Kdot', '-Tpng'], make sure the Graphviz executables are on your systems' PATH
For any more help related to graphviz installation, you can check out their forum post.
If after installing graphviz, you wish to customize the task graphs generated on clicking .visualize() in dask, then read this post to see how you can do that.
Found this post helpful? Then check out further posts on Python on iotespresso.com. Also, follow IoT Espresso on Twitter to get notified about every new post.