ASCII Visualisation of a graph of nodes in python
I have a class called Node
class Node:
def __init__(self,name, childList, parentList):
self.name = name
# a list of all nodes which are children of this node
# may have length 0 to many
self.childList = childList
# a list of all nodes which are parents of this node
# may have length 0 to many
self.parentList = parentList
I have a li开发者_Python百科st of Nodes (nodeList). These Nodes may be in each others parentLists or childLists. I want to be able to visualise the relationships between the Nodes dictated in their childLists and parentlists onto stdout (as an ASCII drawing).
e.g where the names below are names of the nodes in the nodeList.
Classifier
|
|
FeatureCombiner
/ \
/ \
/ \
FeatureGenerator1 FeatureGenerator2
\ /
\ /
\ /
\ /
\ /
\ /
\ /
Image Loader
Classifier has an empty parentList and a childList of length 1 containing FeatureCombiner. FeatureGenerator1 and 2 have the same parent and childList containing FeatureCombiner and Image Loader respectively. Image Loader has an empty childList and a parentList containing FeatureGenerator1 and 2.
Thankyou in advance, Matt
We had a similar problem in DVC project and after lurking around and even trying to port Graph::Easy into Python, we've realized that there is no cross-platform python library that can do that and not require some heavy dependencies like Graphviz. So we've ended up with using an amazing library called Grandalf that handled the layout for us, then we render the results ourselves in ASCII and present them using a pager (this is how stuff like git log
makes it's output nice and scrollable both horizontally and vertically). See the whole code here.
+-------------------+ +--------------------+
| test_data.csv.dvc | | train_data.csv.dvc |
+-------------------+ +--------------------+
** **
*** ***
** **
+-------------------+
| featurization.dvc |
+-------------------+
*** ***
** ***
** **
+--------------+ **
| training.dvc | **
+--------------+ ***
*** ***
** **
** **
+---------+
| Dvcfile |
+---------+
This is non-trivial to do in ascii as evidenced by the lack of complete answers in:
Python ASCII Graph Drawing
That said, there are a lot of tools available to draw graphs in non-ascii ways. Check out the plotting capabilities associated with NetworkX and Matplotlib for starters:
http://networkx.lanl.gov/
http://matplotlib.sourceforge.net/
and also pydot:
http://code.google.com/p/pydot/
Perhaps port the ASCII graph layout logic from Perl's Graph::Easy
?
精彩评论