CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pydot

Python interface to Graphviz's Dot language for creating, reading, editing, and visualizing graphs

Pending
Overview
Eval results
Files

file-operations.mddocs/

File I/O Operations

Functions and methods for reading DOT files and strings, plus comprehensive output generation in various formats including images, vector graphics, and DOT files.

Capabilities

DOT Input Functions

Functions for parsing DOT language input from files and strings to create pydot graph objects.

def graph_from_dot_file(path, encoding=None):
    """
    Load graphs from DOT file.
    
    Parameters:
    - path (str|bytes): Path to DOT file
    - encoding (str, optional): File encoding (default: None for auto-detection)
    
    Returns:
    list[Dot]|None: List of Dot objects or None if parsing fails
    """

def graph_from_dot_data(s):
    """
    Create graphs from DOT description string.
    
    Parameters:
    - s (str): DOT language string
    
    Returns:
    list[Dot]|None: List of Dot objects or None if parsing fails
    """

Output Generation Methods

The Dot class provides comprehensive output generation through dynamically created methods for each supported format.

Image Formats

Generate raster and vector image files:

# PNG format
def create_png(self, prog=None, encoding=None):
    """Create PNG image as bytes."""
    
def write_png(self, path, prog=None, encoding=None):
    """Write PNG image to file."""

# SVG format  
def create_svg(self, prog=None, encoding=None):
    """Create SVG vector image as bytes."""
    
def write_svg(self, path, prog=None, encoding=None):
    """Write SVG vector image to file."""

# PDF format
def create_pdf(self, prog=None, encoding=None):
    """Create PDF document as bytes."""
    
def write_pdf(self, path, prog=None, encoding=None):
    """Write PDF document to file."""

# JPEG format
def create_jpeg(self, prog=None, encoding=None):
    """Create JPEG image as bytes."""
    
def write_jpeg(self, path, prog=None, encoding=None):
    """Write JPEG image to file."""

# GIF format
def create_gif(self, prog=None, encoding=None):
    """Create GIF image as bytes."""
    
def write_gif(self, path, prog=None, encoding=None):
    """Write GIF image to file."""

DOT Format Output

Generate DOT language representations:

def create_dot(self, prog=None, encoding=None):
    """
    Create GraphViz-processed DOT output as bytes.
    
    This version is processed by GraphViz and includes layout information.
    """
    
def write_dot(self, path, prog=None, encoding=None):
    """Write GraphViz-processed DOT output to file."""

def write_raw(self, path):
    """
    Write raw pydot DOT output to file.
    
    This version is generated directly by pydot without GraphViz processing.
    """

def to_string(self):
    """
    Generate raw DOT string representation.
    
    Returns:
    str: DOT language representation
    """

Additional Formats

Support for specialized formats:

# PostScript formats
def create_ps(self, prog=None, encoding=None): ...
def write_ps(self, path, prog=None, encoding=None): ...
def create_ps2(self, prog=None, encoding=None): ...  
def write_ps2(self, path, prog=None, encoding=None): ...

# Plain text formats
def create_plain(self, prog=None, encoding=None): ...
def write_plain(self, path, prog=None, encoding=None): ...

# Interactive formats
def create_xdot(self, prog=None, encoding=None): ...
def write_xdot(self, path, prog=None, encoding=None): ...

# Map formats for web
def create_cmap(self, prog=None, encoding=None): ...
def write_cmap(self, path, prog=None, encoding=None): ...
def create_cmapx(self, prog=None, encoding=None): ...
def write_cmapx(self, path, prog=None, encoding=None): ...

All Supported Output Formats

Complete list of supported formats (23 total): canon, cmap, cmapx, cmapx_np, dia, dot, fig, gd, gd2, gif, hpgl, imap, imap_np, ismap, jpe, jpeg, jpg, mif, mp, pcl, pdf, pic, plain, plain-ext, png, ps, ps2, svg, svgz, vml, vmlz, vrml, vtx, wbmp, xdot, xlib

GraphViz Program Selection

All output methods accept optional parameters:

# Parameters for all create_* and write_* methods:
# - prog (str, optional): GraphViz program to use ('dot', 'neato', 'fdp', 'sfdp', 'twopi', 'circo')
# - encoding (str, optional): Output encoding (default: None)

Usage Examples

Loading from DOT Files

import pydot

# Load graph from file
graphs = pydot.graph_from_dot_file("network.dot")
if graphs:
    graph = graphs[0]  # Get first graph
    print(f"Loaded graph: {graph.get_name()}")

# Load with specific encoding
graphs = pydot.graph_from_dot_file("unicode_graph.dot", encoding="utf-8")

Parsing DOT Strings

dot_string = '''
digraph G {
    A -> B [label="edge"];
    B -> C [color=blue];
    C -> A [style=dashed];
}
'''

graphs = pydot.graph_from_dot_data(dot_string)
if graphs:
    graph = graphs[0]
    print(f"Nodes: {len(graph.get_nodes())}")
    print(f"Edges: {len(graph.get_edges())}")

Generating Different Output Formats

import pydot

# Create a simple graph
graph = pydot.Dot("output_example", graph_type="digraph")
graph.add_node(pydot.Node("A", shape="box"))
graph.add_node(pydot.Node("B", shape="circle"))
graph.add_edge(pydot.Edge("A", "B"))

# Generate PNG image
graph.write_png("diagram.png")

# Generate SVG for web
graph.write_svg("diagram.svg")

# Generate PDF for printing
graph.write_pdf("diagram.pdf")

# Get raw DOT string
dot_source = graph.to_string()
print(dot_source)

# Get GraphViz-processed DOT with layout
processed_dot = graph.create_dot()
with open("processed.dot", "wb") as f:
    f.write(processed_dot)

Using Different Layout Engines

# Use different GraphViz programs for layout
graph.write_png("dot_layout.png", prog="dot")      # Hierarchical
graph.write_png("neato_layout.png", prog="neato")  # Spring model
graph.write_png("circo_layout.png", prog="circo")  # Circular
graph.write_png("fdp_layout.png", prog="fdp")      # Force-directed

Getting Output as Bytes

# Get image data as bytes for further processing
png_data = graph.create_png()
svg_data = graph.create_svg()

# Use with web frameworks, image processing libraries, etc.
from PIL import Image
import io

image = Image.open(io.BytesIO(png_data))
image.show()

Install with Tessl CLI

npx tessl i tessl/pypi-pydot

docs

file-operations.md

graph-management.md

graph-utilities.md

index.md

subgraphs-clusters.md

tile.json