# https://h1ros.github.io/posts/introduction-to-graphviz-in-jupyter-notebook/
! (pip list | grep -q ^graphviz || pip install graphviz) 2>/dev/null
from graphviz import Digraph
from PIL import Image
from IPython.display import display
# https://h1ros.github.io/posts/introduction-to-graphviz-in-jupyter-notebook/
! (pip list | grep -q ^graphviz || pip install graphviz) 2>/dev/null
from graphviz import Digraph, Source
from PIL import Image
from IPython.display import display
# The revolutions over the years
# https://github.com/xflr6/graphviz/blob/master/examples/graphviz-notebook.ipynb
dot = Digraph('Technology Revolution')
# https://www.geeksforgeeks.org/how-to-visualize-a-neural-network-in-python-using-graphviz/
dot.node('p', "PC")
dot.node('m', "Mobile")
dot.node('i', "IoT")
dot.node('d', "Drone")
dot.edges(["pm", "mi", 'id'])
# got left to right and not the default top to bottom
# https://stackoverflow.com/questions/65158660/graphviz-on-colab-how-to-use-rankdir
dot.graph_attr["rankdir"] = "LR"
# https://python.hotexamples.com/examples/graphviz/Digraph/graph_attr%5B%22rankdir%22%5D/python-digraph-graph_attr%5B%22rankdir%22%5D-method-examples.html
# debug the dot output
# print(dot.source)
# You can set a default formt
dot.format = 'jpg'
# writes graph.png
dot.render('historical', format='jpg')
# https://deepnote.com/@alnashi-karam/Bachelorarbeit-xW-NTO-qQ4ykPP0ENkEU6g
# Use the python image library to render it does not work
display(Image.open('historical.jpg'))
print('The history of device revolutions')
# https://graphviz.readthedocs.io/en/stable/examples.html#hello-py
# https://nbviewer.jupyter.org/github/xflr6/graphviz/blob/master/examples/graphviz-notebook.ipynb
cycle = Digraph(name='Cycle')
cycle.attr(compound='true')
print(cycle.source)
# cycle.graph_attr["rankdir"] = "LR"
# https://python.hotexamples.com/examples/graphviz/Digraph/graph_attr%5B%22rankdir%22%5D/python-digraph-graph_attr%5B%22rankdir%22%5D-method-examples.html
# the sugraph cycle name must start with cluster_ to be treated that way
with cycle.subgraph(name='cluster_pc') as pc:
pc.graph_attr['label'] = 'PC Revolution'
pc.attr(style='filled', color='lightgrey')
pc.node('m', 'Free Compute')
pc.node('PC Software')
pc.edge('m', 'PC Software')
with cycle.subgraph(name='cluster_mobile') as mobile:
mobile.graph_attr['label'] = 'Mobile Revolution'
mobile.node('c', 'Free Communication')
mobile.node('Mobile Software')
mobile.edge('c', 'Mobile Software')
with cycle.subgraph(name='cluster_iot') as iot:
iot.graph_attr['label'] = "IoT Revolution"
iot.attr(style="filled")
iot.node('t', 'Free Sensors')
iot.node('Smart Home Software')
iot.edge('c', 'Smart Home Software')
cycle.node('h', 'Hardware Innovation')
cycle.edges(["hm", "hc", "ht"])
cycle.render("cycle", format="jpeg")
display(Image.open("cycle.jpg"))
print(cycle.source)
# https://h1ros.github.io/posts/introduction-to-graphviz-in-jupyter-notebook/
! (pip list | grep -q ^graphviz || pip install graphviz) 2>/dev/null
from graphviz import Digraph
from PIL import Image
from IPython.display import display
# demonstration of how to run this
dot = Digraph()
# https://www.geeksforgeeks.org/how-to-visualize-a-neural-network-in-python-using-graphviz/
dot.node('d', "Drone 1")
dot.node('e', "Drone 2")
dot.node('s', "Station")
dot.edges(["sd", "se"])
# see the file created
print(f"dot.source is:\n{dot.source}")
# You can set a default formt
dot.format = 'png'
# writes graph.png
dot.render('graph')
# can only have one at a time
print('graph output')
# Use the python image library to render it does not work
display(Image.open('graph.png'))
# But jpeg render works
print('second output')
dot.render('graph', format='jpg')
display(Image.open('graph.jpg'))
# render directly from Dot
raw = Source('''digraph "Technology Revolution" {
graph [rankdir=LR]
p [label=PC]
m [label=Mobile]
i [label=IoT]
d [label=Drone]
p -> m
m -> i
i -> d
}
''')
raw.render('raw', format='jpg')
display(Image.open('raw.jpg'))