Block diagram using Graphviz

less than 1 minute read

Graphviz

Graphviz facilitates the creation and rendering of graph descriptions in the DOT language of the Graphviz graph drawing software from Python.

Prerequisites

Installing Graphviz

  • Windows Executables
    Follow the blog for installating executables on window (Graphviz)[https://forum.graphviz.org/t/new-simplified-installation-procedure-on-windows/224]
  • Ubuntu Package
    sudo apt install graphviz
    
  • Install python library
    pip install graphviz
    

Example

  • Generate block diagram using python from data of dictionary dynamically.
from graphviz import Digraph


dot = Digraph(comment='The Round Table')
dot.format = 'png'
dot.attr(rankdir='LR')
dot.attr('node', shape='circle')

# Sample data as dictionary
dict_data = {'test suite 1':[{'test case 1':[{'test 1':'pass'},{'test 2':'fail'}]}, {'test case 2':[{'test 22':'pass'},{'test 23':'fail'}]}]}

# Node
for d in dict_data:    
    dot.node(d, d)
    for dd in dict_data[d]:            
        for ddd in dd:            
            dot.node(ddd, ddd)                     
            for dddd in dd[ddd]:                
                for t in dddd:
                    if dddd[t] == 'pass':
                        dot.node(t, t, color='green')
                    else:
                        dot.node(t, t, color='red')
# Edge
for d in dict_data:        
    for dd in dict_data[d]:              
        for ddd in dd:            
            dot.edge(d, ddd)
            for dddd in dd[ddd]:                
                for t in dddd:                    
                    dot.edge(ddd, t)         

dot.render('round-table', view=True) 

Leave a comment