def intermediary_to_schema(tables, relationships, output): """ Transforms and save the intermediary representation to the file chosen. """ dot_file = _intermediary_to_dot(tables, relationships) graph = AGraph() graph = graph.from_string(dot_file) extension = output.split('.')[-1] graph.draw(path=output, prog='dot', format=extension)
def show_er_diagram(): er_diagram = BytesIO() er_diagram.name = 'er_diagram' table, relationships = all_to_intermediary(db.Model) dot_file = _intermediary_to_dot(table, relationships) graph = AGraph().from_string(dot_file) graph.draw(er_diagram, prog='dot', format='png') er_diagram.seek(0) return send_file(er_diagram, attachment_filename='er_diagram.png', mimetype='image/png')
def from_dot(filename, strict=True, directed=True, string=None): """ :param filename: :param strict: :param directed: :param string: :return: """ A = AGraph(directed=directed, strict=strict) with open(filename) as reader: data = reader.read() A.from_string(data) return A
def AGraphComputerSetMultiDiGraph(spsg: nx.MultiDiGraph, cf: Callable) -> AGraph: A = nx.nx_agraph.to_agraph(spsg) A = AGraph(directed=True) A.node_attr["style"] = "filled" A.node_attr["shape"] = "rectangle" A.node_attr["fixedsize"] = "false" A.node_attr["fontcolor"] = "black" for node in spsg.nodes: A.add_node(node_2_string(node)) edges = spsg.edges(data=True) for edge in edges: s, t, data_dict = edge computer_set = data_dict["computers"] ss, st = tuple(map(node_2_string, (s, t))) A.add_edge(ss, st) Ae = A.get_edge(ss, st) Ae.attr["label"] = "\n".join([c.__name__ for c in computer_set]) return A
def plot_dot_graph(graph, filename=None): """ Plots a graph in graphviz dot notation. :param graph: the dot notation graph :type graph: str :param filename: the (optional) file to save the generated plot to. The extension determines the file format. :type filename: str """ if not plot.pygraphviz_available: logger.error( "Pygraphviz is not installed, cannot generate graph plot!") return if not plot.PIL_available: logger.error("PIL is not installed, cannot display graph plot!") return agraph = AGraph(graph) agraph.layout(prog='dot') if filename is None: filename = tempfile.mktemp(suffix=".png") agraph.draw(filename) image = Image.open(filename) image.show()
def draw(svg_file, V, A, multigraph=True, showlabel=False, ignore=None): try: from pygraphviz.agraph import AGraph except: print "Failed to load pygraphviz!" return g = AGraph(rankdir="LR", directed=True, bgcolor="white", text="black", font_color="white", ranksep="1.0", nodesep="0.10", strict=not multigraph) #g.node_attr["shape"] = "point" g.node_attr["shape"] = "circle" g.node_attr["color"] = "black" g.node_attr["fontcolor"] = "black" g.node_attr["fontstyle"] = "bold" g.node_attr["penwidth"] = "2.0" M = max(i for (u, v, i) in A if type(i) == int) if multigraph: colors = uniquecolors(2 * M) shuffle(colors) for (u, v, i) in A: if ignore != None and (u, v) in ignore: continue assert u != v if i == M or type(i) != int: g.add_edge(u, v, color="black", penwidth=4) else: lbl = str(i) if showlabel else "" g.add_edge(u, v, color=colors[i % len(colors)], penwidth="%d" % 4, label=lbl) else: colors = uniquecolors(M + 1) shuffle(colors) links = {} for (u, v, i) in A: if (u, v) not in links: links[u, v] = [] links[u, v].append(i) for (ind, (u, v)) in enumerate(links): if ignore != None and (u, v) in ignore: continue assert u != v if M in links[u, v] or any( type(e) != int for e in links[u, v]): g.add_edge(u, v, color="black", penwidth=4) if len(links[u, v]) != 1: lbl = ",".join(map(str, links[u, v, ])) if showlabel else "" g.add_edge(u, v, color=colors[ind % len(colors)], penwidth="%d" % 4, label=lbl) g.draw(svg_file, format="svg", prog="dot") print "SVG file '%s' generated!" % svg_file
def draw_cfg(self, no_content=False): filepath = f"cfg-img/{self.name}.svg" graph = AGraph(directed=True) ids = set() def add_node( id_: int, color: str, content: str, shape: str, label: str, style: Optional[str] = None, **kwargs, ): kwargs.update(color=color, shape=shape, penwidth="4") if style is not None: kwargs.update(style=style) if no_content: kwargs.update(label=label) else: kwargs.update(label=content, tooltip=label) graph.add_node(id_, **kwargs) def get_id(node: CfgNode) -> int: return id(node) def traverse(node: CfgNode): id_ = get_id(node) if id_ in ids: return ids.add(id_) if isinstance(node, StartNode): add_node( id_, color="green", label="start", shape="ellipse", content=f"{node.requires}", ) graph.add_edge(id_, get_id(node.next_node)) traverse(node.next_node) elif isinstance(node, AssignmentNode): add_node( id_, color="blue", label="assign", shape="rectangle", content=f"{node.var.var} := {node.expression}", ) graph.add_edge(id_, get_id(node.next_node)) traverse(node.next_node) elif isinstance(node, CondNode): add_node( id_, color="red", label="condition", shape="diamond", content=f"{node.condition}", ) graph.add_edge(id_, get_id(node.true_br), label="T") graph.add_edge(id_, get_id(node.false_br), label="F") traverse(node.true_br) traverse(node.false_br) elif isinstance(node, EndNode): add_node( id_, color="black", label="halt", shape="ellipse", content=f"{node.assertion}", ) elif isinstance(node, AssertNode): add_node( id_, color="purple", label="assert", shape="house", content=f"{node.assertion}", ) graph.add_edge(id_, get_id(node.next_node)) traverse(node.next_node) elif isinstance(node, AssumeNode): add_node( id_, color="pink", label="assume", shape="oval", content=f"{node.expression}", ) graph.add_edge(id_, get_id(node.next_node)) traverse(node.next_node) elif isinstance(node, DummyNode): add_node(id_, color="yellow", label="dummy", shape="star", content="???") else: assert False traverse(self.cfg) graph.draw(path=filepath, prog="dot")
def draw_graph(svg_file, V, A, show_labels=False, ignore=None, back=None, loss=None, graph_attrs=None, verbose=False): """Draw an arc-flow graph in .svg format.""" from pygraphviz.agraph import AGraph if ignore is None: ignore = [] if back is None: back = [] if loss is None: loss = [] elif not isinstance(loss, (tuple, list)): loss = [loss] g = AGraph(rankdir="LR", directed=True, bgcolor="white", ranksep="1.0", nodesep="0.10", strict=False) if graph_attrs is not None: for attr, value in graph_attrs.items(): g.graph_attr[attr] = value g.node_attr["shape"] = "ellipse" g.node_attr["color"] = "black" g.node_attr["fontcolor"] = "black" g.node_attr["penwidth"] = "2.0" lbls = sorted(set(i for (u, v, i) in A if i not in loss), key=lambda lbl: (repr(type(lbl)), lbl)) colors = Colors.uniquecolors(len(lbls) + 1, v=0.5, p=0.0) used = set() for (u, v, i) in A: if (u, v) in ignore: continue used.add(u) used.add(v) if (u, v) in back: u, v = v, u d = "back" else: d = "front" if i in loss: g.add_edge(u, v, color="black", style="dashed", penwidth=2, dir=d) else: lbl = str(i) if show_labels else "" color = colors[lbls.index(i) % len(colors)] g.add_edge(u, v, color=color, penwidth=2, label=lbl, dir=d) for v in V: if v not in used: g.add_node(v) g.draw(svg_file, format="svg", prog="dot") if verbose: print("SVG file '{0}' generated!".format(svg_file))