Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# Based on http://matthiaseisen.com/articles/graphviz/
import graphviz as gv
styles = {
'graph': {
'label': 'Network Map',
'fontsize': '16',
'fontcolor': 'white',
'bgcolor': '#333333',
'rankdir': 'BT',
},
'nodes': {
'fontname': 'Helvetica',
'shape': 'box',
'fontcolor': 'white',
'color': '#006699',
'style': 'filled',
'fillcolor': '#006699',
'margin': '0.4',
},
'edges': {
'style': 'dashed',
'color': 'green',
'arrowhead': 'open',
'fontname': 'Courier',
'fontsize': '14',
'fontcolor': 'white',
}
}
def apply_styles(graph, styles):
graph.graph_attr.update(
('graph' in styles and styles['graph']) or {}
)
graph.node_attr.update(
('nodes' in styles and styles['nodes']) or {}
)
graph.edge_attr.update(
('edges' in styles and styles['edges']) or {}
)
return graph
def draw_topology(topology_dict, output_filename='img/topology'):
nodes = set([key[0] for key in topology_dict.keys() + topology_dict.values()])
g1 = gv.Graph(format='svg')
for node in nodes:
g1.node(node)
for key, value in topology_dict.iteritems():
head, t_label = key
tail, h_label = value
g1.edge(head, tail, headlabel=h_label, taillabel=t_label, label=" "*12)
# headport='nw' tailport='s'
g1 = apply_styles(g1, styles)
print(g1.source)
filename = g1.render(filename=output_filename)
print "Graph saved in", filename
diag5 = {('R3', 'Fa0/1'): ('R1', 'Fa0/1'),
('R2', 'Fa3/0'): ('R1', 'Fa3/0'),
('R2', 'Fa1/0'): ('R1', 'Fa0/1'),
('R4', 'Fa4/0'): ('R1', 'Fa4/0'),
('R2', 'Fa0/0'): ('R3', 'Fa0/0'),
('R5', 'Fa4/0'): ('R3', 'Fa3/0'),
('R4', 'Fa1/0'): ('R3', 'Fa0/0'),
('R4', 'Fa1/0'): ('R2', 'Fa1/0'),
('R4', 'Fa0/1'): ('R5', 'Fa0/1')}
draw_topology(diag5)
@natenka

This comment has been minimized.

Copy link
Owner Author

@natenka natenka commented Feb 2, 2017

img/topology.svg:

topology

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.