pythongraphvizgoogle-colaboratorypygraphviz

Graphviz not printing output graph on Colab


I am trying Graphviz examples on Google Colab.

This is the code I am trying:

from graphviz import Graph

g = Graph('G', filename='process.gv', engine='sfdp')

g.edge('run', 'intr')
g.edge('intr', 'runbl')
g.edge('runbl', 'run')
g.edge('run', 'kernel')
g.edge('kernel', 'zombie')
g.edge('kernel', 'sleep')
g.edge('kernel', 'runmem')
g.edge('sleep', 'swap')
g.edge('swap', 'runswap')
g.edge('runswap', 'new')
g.edge('runswap', 'runmem')
g.edge('new', 'runmem')
g.edge('sleep', 'runmem')

g.view()

from https://graphviz.readthedocs.io/en/stable/examples.html#hello-py

I am expecting a node & edge graph as an output. However, I only get 'hello.gv.pdf'and it doesn't draw any graph.

How can I fix this problem? I tried re-installing Graphviz on Colab but it still doesn't work

Thank you


Solution

  • Add this:

    with open("process.gv") as f:
        dot_graph = f.read()
    graphviz.Source(dot_graph)
    

    Then you will be able to observe in the google colab.

    result

    Or instead of g.view() just write g