GvGen

1.0 · maintenance · verified Thu Apr 16

GvGen is a Python class designed to generate Graphviz DOT files, which are used to describe graphs. It provides a simple API for creating nodes, defining connections (edges), and applying various Graphviz properties to customize the appearance of the graph. The library simplifies the programmatic creation of complex graph structures, with its current version 1.0 focusing on stable functionality since its release in February 2020.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a simple directed graph with nodes and edges using GvGen, apply basic styling, and then generate a Graphviz DOT file. It also provides instructions for rendering the DOT file into an image using the Graphviz command-line tool.

import os
from gvgen import GvGen

# Create a new graph instance
graph = GvGen()

# Create some nodes
node_a = graph.newItem("NodeA")
node_b = graph.newItem("NodeB")
node_c = graph.newItem("NodeC")

# Link nodes
graph.newLink(node_a, node_b)
graph.newLink(node_b, node_c, label="path")
graph.newLink(node_c, node_a, "back-ref", style='dotted')

# Add some properties to a node
graph.property(node_a, 'color', 'red')
graph.property(node_b, 'shape', 'box')

# Output the DOT source to a file
dot_file_path = "example_graph.dot"
with open(dot_file_path, "w") as f:
    graph.dot(f)

print(f"DOT file generated at: {dot_file_path}")
print("To render this graph, ensure Graphviz is installed and run: ")
print(f"  dot -Tpng {dot_file_path} -o example_graph.png")
print(f"  xdg-open example_graph.png # On Linux to view")

view raw JSON →