コード例 #1
0
    def _dot(self, engine='fdp'):
        '''Generate dot representation (as object, not source).

        Requires graphviz module.

        Returns:
            string (SVG)
        '''
        try:
            from graphviz import Graph as gvGraph, Digraph as gvDigraph
        except:
            raise Exception('Missing module: graphviz')
        g = gvDigraph(engine=engine) if self.directed else gvGraph(
            engine=engine)
        for src in range(self.order):
            for dst in self.adjlists[src]:
                if self.directed or src <= dst:
                    g.edge(str(src), str(dst))
        return g
コード例 #2
0
def displaySVG(ref, filename='temp'):
    """Render a graph to SVG format.

    *Warning:* Made for use within IPython/Jupyter only.

    Args:
        ref (Graph).
        filename (str): Temporary filename to store SVG output.

    Returns:
        SVG: IPython SVG wrapper object for graph.

    """

    # Ensure all modules are available
    try:
        from graphviz import Graph as gvGraph, Digraph as gvDigraph
        from IPython.display import SVG
    except:
        raise Exception("Missing module: graphviz and/or IPython.")
    # Traverse graph and generate temporary Digraph/Graph object
    output_format = 'svg'
    if ref.directed:
        graph = gvDigraph(filename, format=output_format)
    else:
        graph = gvGraph(filename, format=output_format)
    if ref is not None:
        for src in range(ref.order):
            src_id = 'node_' + str(src)
            graph.node(src_id, label=str(src))
            for dst in ref.adjlists[src]:
                if ref.directed or src >= dst:
                    graph.edge(src_id, 'node_' + str(dst))
    # Render to temporary file and SVG object
    graph.render(filename=filename, cleanup=True)
    return SVG(filename + '.' + output_format)
コード例 #3
0
def renderGraph(graphs, display=False):
    #parent.graph_attr['rankdir'] = 'LR'

    filenames = {}

    for i, graph in enumerate(graphs):
        g = gvGraph(comment=GRAPH_NAME, format='png')

        for frame in graph.frames.values():
            g.node(frame.name + "_" + str(i), frame.name)

        for edge in graph.edges:
            l = list(edge.frames)
            g.edge(l[0].name + "_" + str(i),
                   l[1].name + "_" + str(i),
                   style=edge.style,
                   color=edge.color,
                   label=" " + edge.label + "\n" + " " + edge.name)

        filename = RENDER_PREFIX + graph.name + '.gv'
        filenames[graph.name] = "" + filename + '.png'
        g.render(filename, view=display)

    return filenames