Visualizing the DAG#

To visualize the DAG of the project, first, install pygraphviz and graphviz. For example, you can both install with conda

$ conda install -c conda-forge pygraphviz

After that, pytask offers two interfaces to visualize your project’s DAG.

Command-line interface#

You can quickly create a visualization with this command.

$ pytask dag

It generates a dag.pdf in the current working directory.

If you do not want to generate a PDF, use pytask dag --output-path or, shorter, pytask dag -o to choose a different format inferred from the file-ending. Select any format supported by graphviz.

$ pytask dag -o dag.png

You can change the graph’s layout by using the pytask dag --layout option. Its default is set to dot and produces a hierarchical structure. graphviz supports other layouts, which are listed here.

Programmatic Interface#

The programmatic and interactive interface allows customizing the figure.

Similar to pytask.build(), there exists pytask.build_dag() which returns the DAG as a networkx.DiGraph.

from pathlib import Path

import networkx as nx
from my_project.config import BLD
from my_project.config import SRC
from pytask import build_dag
from pytask import Product
from typing_extensions import Annotated


def task_draw_dag(path: Annotated[Path, Product] = BLD / "dag.svg") -> None:
    dag = build_dag({"paths": SRC})

    # Set shapes to hexagons.
    nx.set_node_attributes(dag, "hexagon", "shape")

    # Export with pygraphviz and dot.
    graph = nx.nx_agraph.to_agraph(dag)
    graph.draw(path, prog="dot")

Customization works best on the networkx.DiGraph. For example, here, we set the shape of all nodes to hexagons by adding the property to the node attributes.

For drawing, you better switch to pygraphviz since the matplotlib backend handles shapes with texts poorly. Here we store the graph as a .svg.