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.main()
, there exists pytask.build_dag()
which returns the
DAG as a networkx.DiGraph
.
@pytask.mark.produces(BLD / "dag.svg")
def task_draw_dag(produces):
dag = pytask.build_dag({"paths": SRC})
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.
nx.set_node_attributes(dag, "hexagon", "shape")
For drawing, you better switch to pygraphviz since the matplotlib backend handles shapes
with texts poorly. Here we store the graph as a .svg
.
graph = nx.nx_agraph.to_agraph(dag)
graph.draw(path, prog=layout)