Python interface to Graphviz's Dot language for creating, reading, editing, and visualizing graphs
—
Core graph objects and operations that form the foundation for building and manipulating graph structures in pydot. These classes provide a Python interface to Graphviz's graph model.
The primary container for representing complete graphs. Supports directed graphs (digraph), undirected graphs (graph), and strict mode for eliminating duplicate edges.
class Dot:
def __init__(self, graph_name='G', obj_dict=None, graph_type='digraph',
strict=False, suppress_disconnected=False, simplify=False, **attrs):
"""
Create a new graph container.
Parameters:
- graph_name (str): Name of the graph
- obj_dict (dict): Object dictionary for internal use
- graph_type (str): 'digraph' for directed, 'graph' for undirected
- strict (bool): Eliminate multiple edges between same nodes
- suppress_disconnected (bool): Hide disconnected nodes in output
- simplify (bool): Remove redundant elements
- **attrs: Additional graph attributes
"""
def add_node(self, node):
"""Add a node to the graph."""
def add_edge(self, edge):
"""Add an edge to the graph."""
def add_subgraph(self, subgraph):
"""Add a subgraph to the graph."""
def get_node(self, name):
"""
Get node by name.
Returns:
list: List of matching nodes (usually one element)
"""
def get_edge(self, src_or_list, dst=None):
"""
Get edge by source and destination nodes.
Parameters:
- src_or_list: Source node or list/tuple of (src, dst)
- dst: Destination node (if src_or_list is not a list/tuple)
Returns:
list: List of matching edges
"""
def get_nodes(self):
"""Get all nodes in the graph."""
def get_edges(self):
"""Get all edges in the graph."""
def get_subgraphs(self):
"""Get all subgraphs in the graph."""
def del_node(self, name, index=None):
"""
Delete node by name.
Parameters:
- name (str|Node): Node name or Node object
- index (int, optional): Specific index if multiple nodes with same name
Returns:
bool: True if node was deleted
"""
def del_edge(self, src_or_list, dst=None, index=None):
"""
Delete edge by source and destination.
Parameters:
- src_or_list: Source node or list/tuple of (src, dst)
- dst: Destination node (if src_or_list is not a list/tuple)
- index (int, optional): Specific index if multiple edges exist
Returns:
bool: True if edge was deleted
"""
def to_string(self):
"""
Generate raw DOT string representation.
Returns:
str: DOT language representation of the graph
"""
def get_graph_type(self):
"""Get the graph's type ('digraph' or 'graph')."""
def set_graph_defaults(self, **attrs):
"""Set default graph attributes."""
def get_graph_defaults(self):
"""Get default graph attributes."""
def set_node_defaults(self, **attrs):
"""Set default node attributes."""
def get_node_defaults(self):
"""Get default node attributes."""
def set_edge_defaults(self, **attrs):
"""Set default edge attributes."""
def get_edge_defaults(self):
"""Get default edge attributes."""Individual graph vertices with customizable visual and semantic attributes.
class Node:
def __init__(self, name='', obj_dict=None, **attrs):
"""
Create a new node.
Parameters:
- name (str): Unique identifier for the node
- obj_dict (dict): Object dictionary for internal use
- **attrs: Node attributes (shape, color, label, etc.)
"""
def get_name(self):
"""Get the node's name."""
def set_name(self, name):
"""Set the node's name."""
def to_string(self):
"""Generate DOT string representation of the node."""Connections between nodes with directional and styling properties.
class Edge:
def __init__(self, src='', dst='', obj_dict=None, **attrs):
"""
Create a new edge.
Parameters:
- src (str|Node|list|tuple): Source node identifier, Node object, or
sequence of edge definitions for multiple edges
- dst (str|Node): Destination node identifier or Node object
- obj_dict (dict): Object dictionary for internal use
- **attrs: Edge attributes (color, style, label, etc.)
"""
def get_source(self):
"""Get the source node."""
def get_destination(self):
"""Get the destination node."""
def set_source(self, src):
"""Set the source node."""
def set_destination(self, dst):
"""Set the destination node."""
def to_string(self):
"""Generate DOT string representation of the edge."""Foundation class providing common functionality for all graph-like objects.
class Graph:
def __init__(self, graph_name='G', obj_dict=None, graph_type='digraph',
strict=False, suppress_disconnected=False, simplify=False, **attrs):
"""Base graph class - use Dot class instead for main graphs."""All graph objects support dynamically generated getter/setter methods for Graphviz attributes:
For Dot/Graph objects - 73 attributes including:
get_bgcolor() / set_bgcolor(value) - Background colorget_rankdir() / set_rankdir(value) - Layout direction ('TB', 'LR', etc.)get_fontsize() / set_fontsize(value) - Default font sizeget_label() / set_label(value) - Graph title/labelFor Node objects - 38 attributes including:
get_shape() / set_shape(value) - Node shape ('box', 'circle', 'ellipse', etc.)get_color() / set_color(value) - Node border colorget_fillcolor() / set_fillcolor(value) - Node fill colorget_label() / set_label(value) - Node display textFor Edge objects - 61 attributes including:
get_color() / set_color(value) - Edge colorget_arrowhead() / set_arrowhead(value) - Arrow style ('normal', 'dot', 'diamond', etc.)get_style() / set_style(value) - Line style ('solid', 'dashed', 'dotted', etc.)get_label() / set_label(value) - Edge label textimport pydot
# Create directed graph
graph = pydot.Dot("example", graph_type="digraph")
# Add nodes with attributes
start = pydot.Node("start", shape="ellipse", style="filled", fillcolor="lightgreen")
process = pydot.Node("process", shape="box", label="Process Data")
end = pydot.Node("end", shape="ellipse", style="filled", fillcolor="lightcoral")
graph.add_node(start)
graph.add_node(process)
graph.add_node(end)
# Add edges
graph.add_edge(pydot.Edge("start", "process", label="input"))
graph.add_edge(pydot.Edge("process", "end", label="output"))
# Set graph properties
graph.set_rankdir("LR") # Left to right layout
graph.set_bgcolor("white")# Find nodes by name
nodes = graph.get_node("process")
if nodes:
process_node = nodes[0]
process_node.set_color("blue")
# Iterate through all nodes
for node in graph.get_nodes():
print(f"Node: {node.get_name()}")
# Iterate through all edges
for edge in graph.get_edges():
print(f"Edge: {edge.get_source()} -> {edge.get_destination()}")Install with Tessl CLI
npx tessl i tessl/pypi-pydot