1
votes

I'm trying to find a way to visualize a directed graph of nodes in a way such that I can run a script in Python that modifies the graph, and in turn, outputs a new graph. The goal is really to be able to programmatically add and remove edges from the graph in real time.

My current implementation is using GraphViz and Python 3.6 on a Windows box, and outputting it into a .dot file. I found a viewer online under a Github project by the name of "gvuv", but it doesn't support Python 3.6.

My graph is currently defined by a dictionary of sets, where each key in the dictionary is a node, and each value is a set containing all nodes with a directed edge. If anyone has a different way of viewing this information, please let me know as well.

Example graph:

graph = { "a" : set("c"),
      "b" : set("c", "e"),
      "c" : set("a", "b", "d", "e"),
      "d" : set("c"),
      "e" : set("c", "b"),
      "f" : set()
    } 
2

2 Answers

0
votes

Since your question is not really clear, may I ask if you only wish to visualize the result of your python script? Are you looking for a python library to manage and visualize graphs? Do you want to do more with your graphs? If your goal is simply to build graphs and display the result, why not stick with graphviz and use the GraphViz python module. It allows you to build from very simple to advanced graphs and render the result in different formats. A simple example extracted from the documentation:

from graphviz import Digraph

# Create a directed graph
g = Digraph('G', filename='graph.gv', format='png')

# Add an edge between two nodes
# The nodes are created on the fly
g.edge('Hello', 'World')  

# Display the result
g.view()
# Render/save to disk
g.render()

Hope this helps in your endeavor.

0
votes

Here is what ended up working in the end. I outputted the file as a 'png' rather than a .dot file (though I believe any image filetype would work), and then found a tool online called "JPEGViewer" that will automatically update a file in the same window if the file is changed somewhere else. The poc code below changes an arrow from black to green and back repeatedly, and then I can use JPEGViewer on the output file to accomplish the task.

from graphviz import Digraph
from time import sleep

dot = Digraph(comment='The Round Table', format='png', strict=True)

dot.node('A', 'one', color="red")
dot.node('B', 'two', color="red")
dot.node('C', 'three', color="red")
dot.node('D', 'four', color="red")
dot.node('E', 'five', color="red")
dot.node('F', 'six', color="red")
dot.node('G', 'device1', color="sienna")
dot.node('H', 'device2', color="green")




dot.edges(['AB', 'AG', 'AD', 'AE', 'BH', 'BC', 'BF'])
dot.edges(['BA', 'GA', 'DA', 'EA', 'HB', 'CB', 'FB'])
dot.edges(['CD', 'DC', 'EF', 'FE'])



while (True):
    dot.edge('H', 'B', color="black")
    sleep(.5)
    dot.render("TestGraphLiveUpdate")
    dot.view()
    dot.edge('H', 'B', color="green")
    sleep(.5)
    dot.render("TestGraphLiveUpdate")
    dot.view()