<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Graphviz usage in Python notebooks
Graphviz is a library/program for algorithmic redering of graphs, given their nodes and edges. It can be used to create diagrams programmatically.
Step1:  There are many available Python packages providing APIs for Graphviz. In no particular order
Step2:  Render
To render the graph in the notebook we call the create_svg() method, and the return rendered SVG can be output as the Notebook cell results by using the IPython SVG call
Step3:  We can also set properties on the graph nodes
Step4:  It is also possible to render as PNG and display as image (though the quality, in general, will be lower)
Step5:  Graphviz
The graphviz package offers a very similar procedure
Step6:  Render
Step7:  Furthermode, the package provides a useful facility for notebooks
Step8:  It is also possible to directly provide a buffer containing a graph written in dot language, by using the Source class
<ASSISTANT_TASK:>
Python Code:
from __future__ import print_function
Explanation: Graphviz usage in Python notebooks
Graphviz is a library/program for algorithmic redering of graphs, given their nodes and edges. It can be used to create diagrams programmatically.
End of explanation
import pydot
# Create a graph and set defaults
dot = pydot.Dot()                                                           
dot.set('rankdir', 'TB')                                                    
dot.set('concentrate', True)                                                
dot.set_node_defaults(shape='record')                                       
# Add nodes and edges
node = pydot.Node(1, label="FROM")
dot.add_node(node)
node = pydot.Node(2, label="TO")
dot.add_node(node)
dot.add_edge( pydot.Edge(1,2))
Explanation: There are many available Python packages providing APIs for Graphviz. In no particular order:
* pygraphviz
* graphviz
* pydot
* pydot-ng
* pydotplus
* graphviz-python. API provided by Graphviz itself.
The VM contains a couple of them: pydot and graphviz
pydot
End of explanation
from IPython.core.display import SVG
img = dot.create_svg()
SVG( data=img )
Explanation: Render
To render the graph in the notebook we call the create_svg() method, and the return rendered SVG can be output as the Notebook cell results by using the IPython SVG call
End of explanation
for n in dot.get_nodes():
    n.set('style', 'filled')
    n.set('fillcolor', 'aliceblue')
    n.set('fontsize', '10')
    n.set('fontname', 'Trebuchet MS, Tahoma, Verdana, Arial, Helvetica, sans-serif')
SVG( data=dot.create_svg() )
Explanation: We can also set properties on the graph nodes
End of explanation
from IPython.core.display import Image
Image( data=dot.create_png() )
Explanation: It is also possible to render as PNG and display as image (though the quality, in general, will be lower)
End of explanation
import graphviz as gv
g1 = gv.Graph(format='svg')
g1.node('A', 'Node A', tooltip='tooltip for node A')
g1.node('B')
g1.edge('A', 'B')
Explanation: Graphviz
The graphviz package offers a very similar procedure:
* Create a Graph or DiGraph object
* Use the node() and edge() methods to add nodes and edges, respectively
* Render the graph into a file (in the format given in the constructor) by calling the render() method. Available formats will depend on the graphviz installation
End of explanation
# Render into "example.svg" file
g1.render( filename="example")
Explanation: Render
End of explanation
# Create a graph
dot = gv.Digraph(comment='The Round Table', engine='dot')
dot.node('A', 'King Arthur', color="blue", fillcolor="lightgray", style="filled", fontcolor="red", fontname="Verdana")
dot.node('B', 'Sir Bedevere the Wise')
dot.node('L', 'Sir Lancelot the Brave', shape="rectangle")
dot.edges(['AB', 'AL'])
dot.edge('B', 'L', constraint='false', color="blue")
# Render in notebook by just outputting the graph as the result of a cell
dot
Explanation: Furthermode, the package provides a useful facility for notebooks: Graph or Digraph objects contain a _repr_svg_() method that makes them directly renderizable in a notebook. We just have to execute a cell having one object as the last line
End of explanation
src = gv.Source('digraph "countdown" { rankdir=LR; 3 -> 2 -> 1 -> "Go!" }')
# Again, the result can be rendered directly in the Notebook
src
Explanation: It is also possible to directly provide a buffer containing a graph written in dot language, by using the Source class:
End of explanation
<END_TASK>