Skip to content

Commit

Permalink
Simple graph visualization using pyvis package
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsekar committed Jul 1, 2021
1 parent 9114bd5 commit 7a8b715
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ obj_tables
setuptools
pyyaml
backports.cached_property
pyvis
parameterized
52 changes: 52 additions & 0 deletions wc_rules/graph/vis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from pathlib import Path
import networkx as nx
from pyvis.network import Network

def visualize_graph_container(g):
# g is a GraphContainer
nxg = nx.DiGraph()
size = 20
for idx,node in g.iter_nodes():
classname = node.__class__.__name__
nxg.add_node(idx,
size=size,
group=classname,
title=classname,
)
# edge size
value = 5
for edge in g.iter_edges():
arrows = dict()
n1,a1,n2,a2 = edge.unpack()
if a1 < a2:
arrows.update({'to':{'enabled':True,'type':'arrow'}})
if a1 > a2:
arrows.update({'from':{'enabled':True,'type':'arrow'}})
nxg.add_edge(*edge.nodes(),
title=edge.pprint(),
arrows=arrows
)
return nxg

class VisUtil:

converters = {
'GraphContainer': visualize_graph_container
}

def __init__(self,folder='.'):
self.folder = Path(folder)
self.network_args = ['500px','500px']

def write(self,x,name,converter=None):
if converter is None:
converter = self.converters[x.__class__.__name__]
filename = f'{name}.html'
path = self.folder / filename
self.folder.mkdir(parents=True,exist_ok=True)
nx_graph = converter(x)
nt = Network(*self.network_args)
nt.from_nx(nx_graph)
nt.show(str(path))
return

0 comments on commit 7a8b715

Please sign in to comment.