Skip to main content

Generate and render optionally themed Graphviz diagrams with simple, easy to maintain code.

Project description

PyPI Version Test Coverage Read the Docs Status View on GitHub

Generate and render Graphviz diagrams with clear, maintainable code by separating presentation from structure.

The heart of gvdot is the class Dot, a DOT language builder. Applications create diagrams using Dot methods, then either convert the object to DOT language text or render it as SVG or an image. Users can also interactively display Dot objects in notebooks.

Example

Suppose we want to generate diagrams of nondeterministic finite automata like this:

Example NFA

represented by instances of

@dataclass
class NFA:
    alphabet : str
    delta    : dict[str, list[list[str]]]
    final    : list[str]
    start    : str

where delta["q"][0] is the list of states reached from state $q$ by epsilon transitions, and delta["q"][i] is the list of states reached from $q$ by symbol alphabet[i-1].

We start by defining a theme, a normal Dot object from which other dot objects can inherit graph attributes, default attributes, and roles.

nfa_theme = (Dot()
    .all_default(fontsize=12)
    .node_default(shape="circle", style="filled", fillcolor="khaki")
    .node_role("init", label="", shape="none", width=0, height=0)
    .node_role("final", shape="doublecircle", penwidth=1.25)
    .graph(rankdir="LR", labelloc="t", fontsize=16))

The theme defines two gvdot roles, collections of Graphviz attribute values that applications can assign to diagram elements by name.

Having isolated presentation attributes in a theme, our generation code is straightforward.

def nfa_diagram(nfa:NFA, title:str):

    dot = Dot(directed=True).use_theme(nfa_theme)
    dot.graph(label=Markup(f"<b>{title}<br/></b>"))

    dot.node("_init_", role="init")
    dot.edge("_init_", nfa.start)

    for state in nfa.final:
        dot.node(state, role="final")

    for state, transitions in nfa.delta.items():
        merged = defaultdict(list)
        for index, targets in enumerate(transitions):
            for target in targets:
                merged[target].append(
                    nfa.alphabet[index-1] if index > 0 else '&epsilon;')
        for target, symbols in merged.items():
            dot.edge(state, target, label=", ".join(symbols))

    return dot

We can render and save the diagram above with

example = NFA("01", {
    "s0": [["q0", "r0"], [], []],
    "q0": [[], ["q1"], ["q0"]],
    "q1": [[], ["q1"], ["q2"]],
    "q2": [[], ["q3"], ["q0"]],
    "q3": [[], ["q1"], ["q4"]],
    "q4": [[], ["q4"], ["q4"]],
    "r0": [[], ["r0"], ["r1"]],
    "r1": [[], ["r0"], ["r2"]],
    "r2": [[], ["r3"], ["r1"]],
    "r3": [[], ["r3"], ["r3"]],
}, ["q4","r0","r1","r2"], "s0")

nfa_diagram(example,"Example NFA").save("example.svg")

In a notebook, we can directly display the diagram from a cell containing

nfa_diagram(example,"Example NFA").show()

You can find this NFA example and others in the examples directory.

Documentation

See gvdot.readthedocs.io for an overview and reference.

Installation

You can install gvdot from PyPI with

$ pip install gvdot

To ensure the optional notebook support is enabled, use

$ pip install gvdot[ipython]

You can also clone the repository and install it directly.

$ git clone https://github.com/escreven/gvdot.git
$ cd gvdot
$ pip install .

gvdot requires Python 3.12 or greater.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gvdot-1.0.0.tar.gz (16.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gvdot-1.0.0-py3-none-any.whl (16.7 kB view details)

Uploaded Python 3

File details

Details for the file gvdot-1.0.0.tar.gz.

File metadata

  • Download URL: gvdot-1.0.0.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for gvdot-1.0.0.tar.gz
Algorithm Hash digest
SHA256 5274c2b3674df53af28bd5d4ece2e174d3b9ead20a030a636b59ac508cf38241
MD5 b13f83c28ad0ac710f3f944de482c97a
BLAKE2b-256 428a9da7cc519f4be9e588a69e11ab7da0c64213589cfb6a0b166c3ab6b2f5f3

See more details on using hashes here.

File details

Details for the file gvdot-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: gvdot-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 16.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for gvdot-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f800db4459d758d221a2a91083fa9693d5b94c3c580cf3a87f347adec57fc6d8
MD5 a3891dbeba7cd3228aac59640e8041b6
BLAKE2b-256 18a9464b0a70aab6a776864c306ab097351830343ed25bdd959b5a75f502ff6a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page