Beispiel #1
0
def out_graph(graph:DiGraph) -> None:
  for src, dst_seq in graph.adjacency():
    out_jsonl([src] + list(dst_seq))
Beispiel #2
0
def subgraph(graph:DiGraph, nodes, include_outgoing=True):
  sg = DiGraph()
  for src, dst_seq in graph.adjacency():
    sg.add_edges_from((src, dst) for dst in dst_seq if src in nodes and include_outgoing or dst in nodes)
  return sg
Beispiel #3
0
    def plotly_lookml(G: DiGraph,
                      color_map: list[str],
                      plot_layout: str = "fdp") -> go.Figure:
        """Create an interactive plotly figure for the input `DiGraph` network

        Code source: https://plotly.com/python/network-graphs/

        Args:
            G: the DiGraph() network for LookML nodes
            color_map: color names for each node in `G`
            plot_layout: layout of the nodes positions using Pydot and Graphviz (options: 'dot', 'twopi', 'fdp', 'sfdp', 'circo')

        Returns:
            the interactive plotly figure with the LookML network rendered

        """
        # build layout (i.e. node coordinates / positions)
        pos = graphviz_layout(G, prog=plot_layout)

        edge_x = []
        edge_y = []
        for edge in G.edges():
            x0, y0 = pos[edge[0]]
            x1, y1 = pos[edge[1]]

            edge_x.append(x0)
            edge_x.append(x1)
            edge_x.append(None)
            edge_y.append(y0)
            edge_y.append(y1)
            edge_y.append(None)

        edge_trace = go.Scatter(
            x=edge_x,
            y=edge_y,
            line=dict(width=0.5, color="#888"),
            hoverinfo="none",
            mode="lines",
        )

        node_x = []
        node_y = []
        for node in G.nodes():
            x, y = pos[node]
            node_x.append(x)
            node_y.append(y)

        node_trace = go.Scatter(
            x=node_x,
            y=node_y,
            mode="markers",
            hoverinfo="text",
            marker=dict(
                # colorscale options
                # 'Greys' | 'YlGnBu' | 'Greens' | 'YlOrRd' | 'Bluered' | 'RdBu' |
                # 'Reds' | 'Blues' | 'Picnic' | 'Rainbow' | 'Portland' | 'Jet' |
                # 'Hot' | 'Blackbody' | 'Earth' | 'Electric' | 'Viridis' |
                colorscale="YlGnBu",
                color=[],
                size=10,
                line_width=2,
            ),
        )
        # Color node point
        node_text = []
        for node, adjacencies in G.adjacency():
            node_text.append(node)

        node_trace.marker.color = color_map
        node_trace.text = node_text

        def layout():
            layout = go.Layout(
                title="LookML Content Relationships Network",
                titlefont_size=16,
                showlegend=False,
                hovermode="closest",
                margin=dict(b=20, l=5, r=5, t=40),
                xaxis=dict(showgrid=False,
                           zeroline=False,
                           showticklabels=False),
                yaxis=dict(showgrid=False,
                           zeroline=False,
                           showticklabels=False),
            )
            return layout

        # Create Network Graph
        fig = go.Figure(data=[edge_trace, node_trace], layout=layout())
        fig.show()
        return fig