Python with Graphviz examples

We can reproduce the same topology graph as before using the Python Graphviz package which we have installed:

$ python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
>>> from graphviz import Digraph
>>> my_graph = Digraph(comment="My Network")
>>> my_graph.node("core")
>>> my_graph.node("distribution")
>>> my_graph.node("access1")
>>> my_graph.node("access2")
>>> my_graph.edge("core", "distribution")
>>> my_graph.edge("distribution", "access1")
>>> my_graph.edge("distribution", "access2")

The code basically produces what you would normally write in the DOT language but in a more Pythonic way. You can view the source of the graph before the graph generation:

>>> print(my_graph.source)
// My Network
digraph {
core
distribution
access1
access2
core -> distribution
distribution -> access1
distribution -> access2
}

The graph can be rendered by the render() method; by default, the output format is PDF:

>>> my_graph.render("output/chapter8_gv_3.gv")
'output/chapter8_gv_3.gv.pdf'

The Python package wrapper closely mimics all the API options of Graphviz. You can find documentation about the options on the Graphviz Read the Docs website (http://graphviz.readthedocs.io/en/latest/index.html). You can also refer to the source code on GitHub for more information (https://github.com/xflr6/graphviz). We are now ready to use the tool to map out our network.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.156.231