PyDotPlus Homepage

PyDotPlus is an improved version of the old pydot project that provides a Python Interface to Graphviz’s Dot language.

Quick Guide

Warning

Please Write.

API Reference

API Reference

Table of Contents

GraphViz Module

Graphviz’s dot language Python interface.

This module provides with a full interface to create handle modify and process graphs in Graphviz’s dot language.

class pydotplus.graphviz.Cluster(graph_name='subG', obj_dict=None, suppress_disconnected=False, simplify=False, **attrs)[source]

Class representing a cluster in Graphviz’s dot language.

This class implements the methods to work on a representation of a cluster in Graphviz’s dot language.

cluster(
graph_name=’subG’, suppress_disconnected=False, attribute=value, ...

)

graph_name:
the cluster’s name (the string ‘cluster’ will be always prepended)
suppress_disconnected:
defaults to false, which will remove from the cluster any disconnected nodes.

All the attributes defined in the Graphviz dot language should be supported.

Attributes can be set through the dynamically generated methods:

set_[attribute name], i.e. set_color, set_fontname

or using the instance’s attributes:

Cluster.obj_dict[‘attributes’][attribute name], i.e.

cluster_instance.obj_dict[‘attributes’][‘label’] cluster_instance.obj_dict[‘attributes’][‘fontname’]
class pydotplus.graphviz.Common[source]

Common information to several classes.

Should not be directly used, several classes are derived from this one.

create_attribute_methods(obj_attributes)[source]
get(name)[source]

Get an attribute value by name.

Given an attribute ‘name’ it will get its value. There’s always the possibility of using the methods:

get_‘name’()

which are defined for all the existing attributes.

get_attributes()[source]
get_parent_graph()[source]
get_sequence()[source]
set(name, value)[source]

Set an attribute value by name.

Given an attribute ‘name’ it will set its value to ‘value’. There’s always the possibility of using the methods:

set_‘name’(value)

which are defined for all the existing attributes.

set_parent_graph(parent_graph)[source]
set_sequence(seq)[source]
class pydotplus.graphviz.Dot(*argsl, **argsd)[source]

A container for handling a dot language file.

This class implements methods to write and process a dot language file. It is a derived class of the base class ‘Graph’.

create(prog=None, format='ps')[source]

Creates and returns a Postscript representation of the graph.

create will write the graph to a temporary dot file and process it with the program given by ‘prog’ (which defaults to ‘twopi’), reading the Postscript output and returning it as a string is the operation is successful. On failure None is returned.

There’s also the preferred possibility of using:

create_‘format’(prog=’program’)

which are automatically defined for all the supported formats. [create_ps(), create_gif(), create_dia(), ...]

If ‘prog’ is a list instead of a string the fist item is expected to be the program name, followed by any optional command-line arguments for it:

[‘twopi’, ‘-Tdot’, ‘-s10’]
set_graphviz_executables(paths)[source]

This method allows to manually specify the location of the GraphViz executables.

The argument to this method should be a dictionary where the keys are as follows:

{‘dot’: ‘’, ‘twopi’: ‘’, ‘neato’: ‘’, ‘circo’: ‘’, ‘fdp’: ‘’}

and the values are the paths to the corresponding executable, including the name of the executable itself.

set_prog(prog)[source]

Sets the default program.

Sets the default program in charge of processing the dot file into a graph.

set_shape_files(file_paths)[source]

Add the paths of the required image files.

If the graph needs graphic objects to be used as shapes or otherwise those need to be in the same folder as the graph is going to be rendered from. Alternatively the absolute path to the files can be specified when including the graphics in the graph.

The files in the location pointed to by the path(s) specified as arguments to this method will be copied to the same temporary location where the graph is going to be rendered.

write(path, prog=None, format='raw')[source]

Given a filename ‘path’ it will open/create and truncate such file and write on it a representation of the graph defined by the dot object and in the format specified by ‘format’. ‘path’ can also be an open file-like object, such as a StringIO instance.

The format ‘raw’ is used to dump the string representation of the Dot object, without further processing. The output can be processed by any of graphviz tools, defined in ‘prog’, which defaults to ‘dot’ Returns True or False according to the success of the write operation.

There’s also the preferred possibility of using:

write_‘format’(path, prog=’program’)

which are automatically defined for all the supported formats. [write_ps(), write_gif(), write_dia(), ...]

class pydotplus.graphviz.Edge(src='', dst='', obj_dict=None, **attrs)[source]

A graph edge.

This class represents a graph’s edge with all its attributes.

edge(src, dst, attribute=value, ...)

src: source node’s name dst: destination node’s name

All the attributes defined in the Graphviz dot language should be supported.

Attributes can be set through the dynamically generated methods:

set_[attribute name], i.e. set_label, set_fontname

or directly by using the instance’s special dictionary:

Edge.obj_dict[‘attributes’][attribute name], i.e.

edge_instance.obj_dict[‘attributes’][‘label’] edge_instance.obj_dict[‘attributes’][‘fontname’]
get_destination()[source]

Get the edge’s destination node name.

get_source()[source]

Get the edges source node name.

parse_node_ref(node_str)[source]
to_string()[source]

Returns a string representation of the edge in dot language.

exception pydotplus.graphviz.Error(value)[source]

General error handling class.

class pydotplus.graphviz.Graph(graph_name='G', obj_dict=None, graph_type='digraph', strict=False, suppress_disconnected=False, simplify=False, **attrs)[source]

Class representing a graph in Graphviz’s dot language.

This class implements the methods to work on a representation of a graph in Graphviz’s dot language.

graph(graph_name=’G’, graph_type=’digraph’,
strict=False, suppress_disconnected=False, attribute=value, ...)
graph_name:
the graph’s name
graph_type:
can be ‘graph’ or ‘digraph’
suppress_disconnected:
defaults to False, which will remove from the graph any disconnected nodes.
simplify:
if True it will avoid displaying equal edges, i.e. only one edge between two nodes. removing the duplicated ones.

All the attributes defined in the Graphviz dot language should be supported.

Attributes can be set through the dynamically generated methods:

set_[attribute name], i.e. set_size, set_fontname

or using the instance’s attributes:

Graph.obj_dict[‘attributes’][attribute name], i.e.

graph_instance.obj_dict[‘attributes’][‘label’] graph_instance.obj_dict[‘attributes’][‘fontname’]
add_edge(graph_edge)[source]

Adds an edge object to the graph.

It takes a edge object as its only argument and returns None.

add_node(graph_node)[source]

Adds a node object to the graph.

It takes a node object as its only argument and returns None.

add_subgraph(sgraph)[source]

Adds an subgraph object to the graph.

It takes a subgraph object as its only argument and returns None.

del_edge(src_or_list, dst=None, index=None)[source]

Delete an edge from the graph.

Given an edge’s (source, destination) node names all matching edges(s) will be deleted if ‘index’ is not specified or set to None. If there are several matching edges and ‘index’ is given, only the edge in that position will be deleted.

‘index’ should be an integer specifying the position of the edge to delete. If index is larger than the number of matching edges, no action is taken.

If edges are deleted it returns True. If no action is taken it returns False.

del_node(name, index=None)[source]

Delete a node from the graph.

Given a node’s name all node(s) with that same name will be deleted if ‘index’ is not specified or set to None. If there are several nodes with that same name and ‘index’ is given, only the node in that position will be deleted.

‘index’ should be an integer specifying the position of the node to delete. If index is larger than the number of nodes with that name, no action is taken.

If nodes are deleted it returns True. If no action is taken it returns False.

get_edge(src_or_list, dst=None)[source]

Retrieved an edge from the graph.

Given an edge’s source and destination the corresponding Edge instance(s) will be returned.

If one or more edges exist with that source and destination a list of Edge instances is returned. An empty list is returned otherwise.

get_edge_defaults(**attrs)[source]
get_edge_list()[source]

Get the list of Edge instances.

This method returns the list of Edge instances composing the graph.

get_edges()[source]
get_graph_defaults(**attrs)[source]
get_graph_type()[source]
get_name()[source]

Get the graph’s name.

get_next_sequence_number()[source]
get_node(name)[source]

Retrieve a node from the graph.

Given a node’s name the corresponding Node instance will be returned.

If one or more nodes exist with that name a list of Node instances is returned. An empty list is returned otherwise.

get_node_defaults(**attrs)[source]
get_node_list()[source]

Get the list of Node instances.

This method returns the list of Node instances composing the graph.

get_nodes()[source]

Get the list of Node instances.

get_simplify()[source]

Get whether to simplify or not.

Refer to set_simplify for more information.

get_strict(val)[source]

Get graph’s ‘strict’ mode (True, False).

This option is only valid for top level graphs.

get_subgraph(name)[source]

Retrieved a subgraph from the graph.

Given a subgraph’s name the corresponding Subgraph instance will be returned.

If one or more subgraphs exist with the same name, a list of Subgraph instances is returned. An empty list is returned otherwise.

get_subgraph_list()[source]

Get the list of Subgraph instances.

This method returns the list of Subgraph instances in the graph.

get_subgraphs()[source]
get_suppress_disconnected(val)[source]

Get if suppress disconnected is set.

Refer to set_suppress_disconnected for more information.

get_top_graph_type()[source]
get_type()[source]

Get the graph’s type, ‘graph’ or ‘digraph’.

set_edge_defaults(**attrs)[source]
set_graph_defaults(**attrs)[source]
set_name(graph_name)[source]

Set the graph’s name.

set_node_defaults(**attrs)[source]
set_parent_graph(parent_graph)[source]
set_simplify(simplify)[source]

Set whether to simplify or not.

If True it will avoid displaying equal edges, i.e. only one edge between two nodes. removing the duplicated ones.

set_strict(val)[source]

Set graph to ‘strict’ mode.

This option is only valid for top level graphs.

set_suppress_disconnected(val)[source]

Suppress disconnected nodes in the output graph.

This option will skip nodes in the graph with no incoming or outgoing edges. This option works also for subgraphs and has effect only in the current graph/subgraph.

set_type(graph_type)[source]

Set the graph’s type, ‘graph’ or ‘digraph’.

to_string()[source]

Returns a string representation of the graph in dot language.

It will return the graph and all its subelements in string from.

exception pydotplus.graphviz.InvocationException(value)[source]

To indicate that a ploblem occurred while running any of the GraphViz executables.

class pydotplus.graphviz.Node(name='', obj_dict=None, **attrs)[source]

A graph node.

This class represents a graph’s node with all its attributes.

node(name, attribute=value, ...)

name: node’s name

All the attributes defined in the Graphviz dot language should be supported.

add_style(style)[source]
get_name()[source]

Get the node’s name.

get_port()[source]

Get the node’s port.

set_name(node_name)[source]

Set the node’s name.

to_string()[source]

Returns a string representation of the node in dot language.

class pydotplus.graphviz.Subgraph(graph_name='', obj_dict=None, suppress_disconnected=False, simplify=False, **attrs)[source]

Class representing a subgraph in Graphviz’s dot language.

This class implements the methods to work on a representation of a subgraph in Graphviz’s dot language.

subgraph(
graph_name=’subG’, suppress_disconnected=False, attribute=value, ...

)

graph_name:
the subgraph’s name
suppress_disconnected:
defaults to false, which will remove from the subgraph any disconnected nodes.

All the attributes defined in the Graphviz dot language should be supported.

Attributes can be set through the dynamically generated methods:

set_[attribute name], i.e. set_size, set_fontname

or using the instance’s attributes:

Subgraph.obj_dict[‘attributes’][attribute name], i.e.

subgraph_instance.obj_dict[‘attributes’][‘label’] subgraph_instance.obj_dict[‘attributes’][‘fontname’]
pydotplus.graphviz.find_graphviz()[source]

Locate Graphviz’s executables in the system.

Tries three methods:

First: Windows Registry (Windows only) This requires Mark Hammond’s pywin32 is installed.

Secondly: Search the path It will look for ‘dot’, ‘twopi’ and ‘neato’ in all the directories specified in the PATH environment variable.

Thirdly: Default install location (Windows only) It will look for ‘dot’, ‘twopi’ and ‘neato’ in the default install location under the “Program Files” directory.

It will return a dictionary containing the program names as keys and their paths as values.

If this fails, it returns None.

class pydotplus.graphviz.frozendict(*args, **kw)[source]
clear
pop
popitem
setdefault
update
pydotplus.graphviz.get_fobj(fname, mode='w+')[source]

Obtain a proper file object.

fname : string, file object, file descriptor
If a string or file descriptor, then we create a file object. If fname is a file object, then we do nothing and ignore the specified mode parameter.
mode : str
The mode of the file to be opened.
fobj : file object
The file object.
close : bool
If fname was a string, then close will be True to signify that the file object should be closed after writing to it. Otherwise, close will be False signifying that the user, in essence, created the file object already and that subsequent operations should not close it.
pydotplus.graphviz.graph_from_adjacency_matrix(matrix, node_prefix='', directed=False)[source]

Creates a basic graph out of an adjacency matrix.

The matrix has to be a list of rows of values representing an adjacency matrix. The values can be anything: bool, int, float, as long as they can evaluate to True or False.

pydotplus.graphviz.graph_from_dot_data(data)[source]

Load graph as defined by data in DOT format.

The data is assumed to be in DOT format. It will be parsed and a Dot class will be returned, representing the graph.

pydotplus.graphviz.graph_from_dot_file(path)[source]

Load graph as defined by a DOT file.

The file is assumed to be in DOT format. It will be loaded, parsed and a Dot class will be returned, representing the graph.

pydotplus.graphviz.graph_from_edges(edge_list, node_prefix='', directed=False)[source]

Creates a basic graph out of an edge list.

The edge list has to be a list of tuples representing the nodes connected by the edge. The values can be anything: bool, int, float, str.

If the graph is undirected by default, it is only calculated from one of the symmetric halves of the matrix.

pydotplus.graphviz.graph_from_incidence_matrix(matrix, node_prefix='', directed=False)[source]

Creates a basic graph out of an incidence matrix.

The matrix has to be a list of rows of values representing an incidence matrix. The values can be anything: bool, int, float, as long as they can evaluate to True or False.

pydotplus.graphviz.is_string_like(obj)[source]

Check if obj is string.

pydotplus.graphviz.needs_quotes(s)[source]

Checks whether a string is a dot language ID.

It will check whether the string is solely composed by the characters allowed in an ID or not. If the string is one of the reserved keywords it will need quotes too but the user will need to add them manually.

pydotplus.graphviz.quote_if_necessary(s)[source]

Parser Module

Graphviz’s dot language parser.

The dotparser parses graphviz files in dot and dot files and transforms them into a class representation defined by pydotplus.

class pydotplus.parser.DefaultStatement(default_type, attrs)[source]
class pydotplus.parser.P_AttrList(toks)[source]
pydotplus.parser.add_defaults(element, defaults)[source]
pydotplus.parser.add_elements(g, toks, defaults_graph=None, defaults_node=None, defaults_edge=None)[source]
pydotplus.parser.do_node_ports(node)[source]
pydotplus.parser.get_port(node)[source]
pydotplus.parser.graph_definition()[source]
pydotplus.parser.parse_dot_data(data)[source]
pydotplus.parser.push_attr_list(str, loc, toks)[source]
pydotplus.parser.push_default_stmt(str, loc, toks)[source]
pydotplus.parser.push_edge_stmt(str, loc, toks)[source]
pydotplus.parser.push_graph_stmt(str, loc, toks)[source]
pydotplus.parser.push_node_stmt(s, loc, toks)[source]
pydotplus.parser.push_subgraph_stmt(str, loc, toks)[source]
pydotplus.parser.push_top_graph_stmt(str, loc, toks)[source]
pydotplus.parser.update_parent_graph_hierarchy(g, parent_graph=None, level=0)[source]

Installation

For the latest stable version:

pip install pydotplus

For the development version:

pip install https://github.com/carlos-jenkins/pydotplus/archive/master.zip

License

Copyright (c) 2014 Carlos Jenkins <carlos@jenkins.co.cr>
Copyright (c) 2014 Lance Hepler
Copyright (c) 2004-2011 Ero Carrera <ero@dkbza.org>
Copyright (c) 2004-2007 Michael Krause <michael@krause-software.de>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.