def view_parsing_graph(filename: str): """ :param filename: JSON format file """ import graphviz_artist as ga import graphviz_artist.attr as a import json with open(filename) as f: graph = json.load(f) starts = {v: k for k, v in graph['starts'].items()} g = ga.Graph(directed=True) nodes = {int(k): v for k, v in graph['nodes'].items()} gnodes = {} for i, node in nodes.items(): kind = node['kind'] ctor = kind['ctor'] if ctor == "NEntity": val = kind['val'] ctor = val['ctor'] if ctor == "ETerm": label = repr(val['val']) elif ctor == "ENonTerm": label = val['name'] else: label = ctor + " ..." elif ctor == "NReturn": label = "return" else: label = ctor + " ..." if label == 'Start ...': label = "Start " + str(starts[i]) label += " %d" % i gnodes[i] = g.new(a.Label(label), a.Shape.box) for i, node in nodes.items(): gnode = gnodes[i] for each in node['followed']: _ = gnode > gnodes[each] g.view()
import graphviz_artist as ga import graphviz_artist.attr as a g = ga.Graph(directed=True) g.update(attrs=(a.HorizontalGraph, )) math = g.new(a.Label("math : Python的一个library")) pi = g.new(a.Label("pi : math.pi")) r = g.new(a.Label("r : 1")) h = g.new(a.Label("h : 1")) S = g.new(a.Label("S : 2 * S_top + S_side")) S_top = g.new(a.Label("S_top : pi * r ** 2")) S_side = g.new(a.Label("S_side : C * h ")) C = g.new(a.Label("C : 2 * pi * r")) _ = math > pi > r > h > S_top > C > S_side > S g.view()
import graphviz_artist as ga # use attr module to see which Graphviz Attributes # could be auto-completed. import graphviz_artist.attr as attr # for some reason, the name of keyword arg has no actual semantics, # but the value does: # ga.Graph(kw=attr.XXX) == ga.Graph(_=XXX) == ga.Graph(x=XXX) g = ga.Graph(attr.HorizontalGraph) # `attr.Shape("<name>")` to specify the shape of nodes. n1 = g.new(attr.Label('hey'), attr.Shape.diamond) n2 = g.new(attr.Label('hey'), attr.Shape.hexagon) n3 = g.new(attr.Label('you'), attr.Shape.star) # `attr.Directed()` makes a directed edge. directed = attr.Directed() # `attr.Label` to specify the text that edges display edge_label = attr.Label("passed_here") # `attr.Penwidth` to decide the width of edge glyph. edge_size = attr.Penwidth(2.) # in `a < b[b_to_c_attrs...] > c`, the edge `b -> c` will have attribute `b_to_c_attrs`. _ = n3[directed, edge_label, edge_size] > n1[directed] == n2 > n3 g.save() assert """digraph {
import graphviz_artist as ga import graphviz_artist.attr as attr g = ga.Graph() n1 = g.new() n2 = g.new() n1.update(attr.Label("Hey")) n1.update(attr.Label("Holla")) _ = n1[attr.Label("ok")] == n2 g.save() assert """digraph { 0 1 0 [label=Hey] 0 [label=Holla] 0 -> 1 [label=ok dir=none] 1 -> 0 [label=ok dir=none] }""" == str(g.g)