Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions metricflow_semantics/dag/dag_visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import logging
import os
from typing import TypeVar
from typing import TYPE_CHECKING, TypeVar

import graphviz
if TYPE_CHECKING:
import graphviz
from metricflow_semantics.dag.mf_dag import DagNode, MetricFlowDag
from metricflow_semantics.toolkit.id_helpers import mf_random_id

Expand Down Expand Up @@ -42,7 +43,20 @@ def render_via_graphviz(dag_graph: DagGraphT, file_path_without_svg_suffix: str)
Args:
dag_graph: The DAG to render.
file_path_without_svg_suffix: Path to the SVG file that should be created, without ".svg" suffix.

Raises:
RuntimeError: If the ``graphviz`` package is not installed.
"""
# `graphviz` is an optional dependency as rendering graphs is more of a debugging feature in tests / CLI.
# Since it's optional, import it locally only when needed.
try:
import graphviz
except ImportError as error:
raise RuntimeError(
"The `graphviz` Python package is required for DAG visualization."
" It can be installed with `pip install graphviz` or similar."
" Rendering may also require the `dot` executable from https://www.graphviz.org/ to be on your PATH."
) from error
dot = graphviz.Digraph(comment=dag_graph.dag_id, node_attr={"shape": "box", "fontname": "Courier"})
# Not quite correct if there are shared nodes.
for sink_node in dag_graph.sink_nodes:
Expand Down
Loading