GvGen
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
-
FileNotFoundError: [Errno 2] No such file or directory: 'dot'
cause The Graphviz command-line tools (specifically the 'dot' executable) are not installed or not accessible in your system's PATH.fixInstall Graphviz on your operating system. For example, on Ubuntu/Debian: `sudo apt-get install graphviz`. On macOS with Homebrew: `brew install graphviz`. On Windows, download from graphviz.org and ensure it's added to your PATH. -
ModuleNotFoundError: No module named 'gvgen'
cause The GvGen Python package has not been installed in your current Python environment.fixInstall the library using pip: `pip install GvGen`. -
My graph.dot file is empty or doesn't contain the graph definition.
cause The `dot()` method of the GvGen object writes the graph definition to a file-like object. If it's not explicitly called, or if the file isn't properly opened in write mode, the output file will be empty or incorrect.fixEnsure you are calling `graph.dot(file_object)` after defining all nodes and links, and that the file is opened correctly for writing (e.g., `with open('output.dot', 'w') as f:`).
Warnings
- gotcha GvGen strictly generates Graphviz DOT files and does not include functionality to render these files into images (e.g., PNG, SVG, PDF). You must have the Graphviz command-line tools (e.g., `dot`, `neato`) installed on your system to convert the generated `.dot` files into visual graphs.
- deprecated The GvGen library has not seen active development or new releases since version 1.0 in February 2020. While functional, it is in a maintenance state, meaning new features or prompt updates for major Python changes are unlikely. Users seeking more actively maintained or feature-rich Graphviz Python bindings might consider alternatives like the official `graphviz` Python package.
Install
-
pip install GvGen
Imports
- GvGen
from gvgen import GvGen
Quickstart
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")