xflr6 / graphviz

Simple Python interface for Graphviz
https://graphviz.readthedocs.io
MIT License
1.59k stars 209 forks source link

Graphs not displaying inline in jupyter when created in a function #196

Closed davechurchill closed 1 year ago

davechurchill commented 1 year ago
dot = Digraph()
dot.edge('1', '2')
dot.edge('2', '3')
dot

Inside a jupyter notebook, the above code creates a graph and displays it inline in the notebook just fine, however when I put this exact same code inside a function and then call the function, nothing is displayed:

def graph_me():
  dot = Digraph()
  dot.edge('1', '2')
  dot.edge('2', '3')
  dot
graph_me()

Anyone know why this might be happening? Seems very odd

xflr6 commented 1 year ago

In the first example, the last bare dot line will show the vaue of that variable in the Jupyter REPL (which in this case renders the graph and displays it's SVG representation in Jupyter).

In the second example, the value displayed by the last line is the return value of the your function, which is None. Change the last line of the function to return dot.

davechurchill commented 1 year ago

Oh wow I'm dumb. Thanks for the reply :)