18

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 list 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

3 Answers 3

11

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 |                      
                     +---------+                      
Sign up to request clarification or add additional context in comments.

1 Comment

This is really great :)
8

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/

1 Comment

Thanks, these look like they could do this in an image format. I only thought of binary because I thought that it would be easier
5

Perhaps port the ASCII graph layout logic from Perl's Graph::Easy?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.