def to_graph(dp, *, debug: bool = False) -> "graphviz.Digraph":
    """Visualizes a DataPipe by returning a :class:`graphviz.Digraph`, which is a graph of the data pipeline.
    This allows you to visually inspect all the transformation that takes place in your DataPipes.

    .. note::

        The package :mod:`graphviz` is required to use this function.

    .. note::

        The most common interfaces for the returned graph object are:

        - :meth:`~graphviz.Digraph.render`: Save the graph to a file.
        - :meth:`~graphviz.Digraph.view`: Open the graph in a viewer.

    Args:
        dp: DataPipe that you would like to visualize (generally the last one in a chain of DataPipes).
        debug (bool): If ``True``, renders internal datapipes that are usually hidden from the user
            (such as ``ChildDataPipe`` of `demux` and `fork`). Defaults to ``False``.

    Example:
        >>> from torchdata.datapipes.iter import IterableWrapper
        >>> from torchdata.datapipes.utils import to_graph
        >>> dp = IterableWrapper(range(10))
        >>> dp1, dp2 = dp.demux(num_instances=2, classifier_fn=lambda x: x % 2)
        >>> dp1 = dp1.map(lambda x: x + 1)
        >>> dp2 = dp2.filter(lambda _: True)
        >>> dp3 = dp1.zip(dp2).map(lambda t: t[0] + t[1])
        >>> g = to_graph(dp3)
        >>> g.view()  # This will open the graph in a viewer
    """
    try:
        import graphviz
    except ModuleNotFoundError:
        raise ModuleNotFoundError(
            "The package `graphviz` is required to be installed to use this function. "
            "Please `pip install graphviz` or `conda install -c conda-forge graphviz`."
        ) from None

    # The graph style as well as the color scheme below was copied from https://github.com/szagoruyko/pytorchviz/
    # https://github.com/szagoruyko/pytorchviz/blob/0adcd83af8aa7ab36d6afd139cabbd9df598edb7/torchviz/dot.py#L78-L85
    node_attr = dict(
        style="filled",
        shape="box",
        align="left",
        fontsize="10",
        ranksep="0.1",
        height="0.2",
        fontname="monospace",
    )
    graph = graphviz.Digraph(node_attr=node_attr, graph_attr=dict(size="12,12"))

    for node in to_nodes(dp, debug=debug):
        fillcolor: Optional[str]
        if not node.parents:
            fillcolor = "lightblue"
        elif not node.childs:
            fillcolor = "darkolivegreen1"
        else:
            fillcolor = None

        graph.node(name=repr(node), label=str(node), fillcolor=fillcolor)

        for child in node.childs:
            graph.edge(repr(node), repr(child))

    return graph
Visualizes a DataPipe by returning a :class:`graphviz.Digraph`, which is a graph of the data pipeline.
    This allows you to visually inspect all the transformation that takes place in your DataPipes.

    .. note::

        The package :mod:`graphviz` is required to use this function.

    .. note::

        The most common interfaces for the returned graph object are:

        - :meth:`~graphviz.Digraph.render`: Save the graph to a file.
        - :meth:`~graphviz.Digraph.view`: Open the graph in a viewer.

    Args:
        dp: DataPipe that you would like to visualize (generally the last one in a chain of DataPipes).
        debug (bool): If ``True``, renders internal datapipes that are usually hidden from the user
            (such as ``ChildDataPipe`` of `demux` and `fork`). Defaults to ``False``.

    Example:
        >>> from torchdata.datapipes.iter import IterableWrapper
        >>> from torchdata.datapipes.utils import to_graph
        >>> dp = IterableWrapper(range(10))
        >>> dp1, dp2 = dp.demux(num_instances=2, classifier_fn=lambda x: x % 2)
        >>> dp1 = dp1.map(lambda x: x + 1)
        >>> dp2 = dp2.filter(lambda _: True)
        >>> dp3 = dp1.zip(dp2).map(lambda t: t[0] + t[1])
        >>> g = to_graph(dp3)
        >>> g.view()  # This will open the graph in a viewer
    
python/dgl/graphbolt/datapipes/visualization.py