Demo interactive graph stepper...
# Get dot file
!wget https://api.parliament.uk/procedures/work-packages/9/parse/log.dot
!head log.dot
Import required packages for mapio/GraphvizAnim
:
from gvanim import Animation
from gvanim.jupyter import interactive
ga = Animation()
Load in data:
with open("log.dot") as f:
# top and tail first line - leaves us with edge definitions
dot = f.read().split('\n')[1:-1]
dot[:3]
Build up the anination a step at a time:
ga = Animation()
ga.label_node( 1, "start" )
nodes = {}
nc = 2
for i,n in enumerate(dot[:100], start=1):
ga.next_step()
from_to = [i.strip() for i in n.split('->')]
from_node = from_to[0]
to_node = from_to[1]
for node in [from_node, to_node]:
if node not in nodes:
nodes[node] = nc
nc = nc +1
ga.label_node(nodes[from_node], from_node )
ga.label_node(nodes[to_node], to_node )
ga.add_edge(nodes[from_node], nodes[to_node] )
View the animation interactively:
interactive( ga, 400 )
We can also render an animated gif...
# Create an image frame for each step...
graphs = ga.graphs()
files = render( graphs, 'process', 'png' )
# Then an animated gif for each frame
from gvanim import Animation, render, gif
gif( files, 'process', 50 )
# And display the image
from IPython.display import Image
Image('process.gif')
from IPython.display import Image
Image('process.gif')