def _build(G, target=None, full=False, outs=False): import networkx as nx from dvc.repo.graph import get_pipeline, get_pipelines if target: H = get_pipeline(get_pipelines(G), target) if not full: descendants = nx.descendants(G, target) descendants.add(target) H.remove_nodes_from(set(G.nodes()) - descendants) else: H = G if outs: G = nx.DiGraph() for stage in H.nodes: G.add_nodes_from(stage.outs) for from_stage, to_stage in nx.edge_dfs(H): G.add_edges_from([(from_out, to_out) for from_out in from_stage.outs for to_out in to_stage.outs]) H = G def _relabel(node): from dvc.stage import Stage return node.addressing if isinstance(node, Stage) else str(node) return nx.relabel_nodes(H, _relabel, copy=False)
def _show_ascii(G): from dvc.dagascii import draw from dvc.repo.graph import get_pipelines pipelines = get_pipelines(G) ret = [] for pipeline in pipelines: ret.append(draw(pipeline.nodes, pipeline.edges)) return "\n".join(ret)
def _build(G, target=None, full=False): import networkx as nx from dvc.repo.graph import get_pipeline, get_pipelines if target: H = get_pipeline(get_pipelines(G), target) if not full: descendants = nx.descendants(G, target) descendants.add(target) H.remove_nodes_from(set(G.nodes()) - descendants) else: H = G def _relabel(stage): return stage.addressing return nx.relabel_nodes(H, _relabel, copy=False)
def _show_mermaid(G, markdown: bool = False): from dvc.repo.graph import get_pipelines pipelines = get_pipelines(G) graph = "flowchart TD" total_nodes = 0 for pipeline in pipelines: node_ids = {} nodes = sorted(str(x) for x in pipeline.nodes) for node in nodes: total_nodes += 1 node_id = f"node{total_nodes}" graph += f"\n\t{node_id}[{node}]" node_ids[node] = node_id edges = sorted((str(a), str(b)) for b, a in pipeline.edges) for a, b in edges: graph += f"\n\t{node_ids[str(a)]}-->{node_ids[str(b)]}" if markdown: return f"```mermaid\n{graph}\n```" return graph