55 lines
1.6 KiB
Python
55 lines
1.6 KiB
Python
import graphviz
|
|
|
|
# One cluster of nodes per block
|
|
class Graph():
|
|
def __init__(self):
|
|
self.dot = None
|
|
|
|
|
|
|
|
# One node per block
|
|
class RecordGraph:
|
|
def __init__(self):
|
|
self.dot = None
|
|
self.edges = []
|
|
self.node_counter = 0
|
|
|
|
def create_graph(self, config, data):
|
|
# TODO: process config, use for graph init
|
|
self.dot = graphviz.Graph('testgraph', graph_attr={'center': 'true', 'compound': 'true'}, node_attr={'shape': 'record'})
|
|
self.dot.format = 'svg' # TODO: move to export
|
|
|
|
blocks = data.get('blocks', [])
|
|
for block in blocks:
|
|
self.node_counter += 1
|
|
|
|
# Create node id
|
|
node_id = f'nr{self.node_counter}'
|
|
links = block.get('links', [])
|
|
if len(links) > 0:
|
|
node_id = f'n{links[0]}'
|
|
|
|
# Create text table for node
|
|
texts = []
|
|
for text in block.get('texts', []):
|
|
person = text.get('text', '').replace('\n', '\\n')
|
|
link = text.get('links', [])
|
|
if len(link) > 0:
|
|
person = fr'<l{link[0]}> {person}'
|
|
self.edges.append((fr'{node_id}:l{link[0]}', fr'n{link[0]}'))
|
|
texts.append(person)
|
|
|
|
# Add node
|
|
table = r"|".join(texts)
|
|
self.dot.node(node_id, table)
|
|
|
|
# Add edges
|
|
self.dot.edges(self.edges)
|
|
|
|
def export_graph(self, out_dir):
|
|
self.dot.render(directory=out_dir)
|
|
|
|
|
|
def __str__(self):
|
|
return self.dot.source
|