@@ -84,7 +84,7 @@ def _build_graph_for_viz(graph_func, *args, **kwargs):
84
84
def _build_graphviz_graph (graph_func , * args , ** kwargs ):
85
85
from graphviz import Digraph
86
86
87
- nodes , edges = _build_graph_for_viz (graph_func = graph_func , * args , ** kwargs )
87
+ nodes , edges = _build_graph_for_viz (graph_func , * args , ** kwargs )
88
88
89
89
digraph = Digraph (strict = True )
90
90
digraph .attr (rankdir = "LR" , size = "150,150" )
@@ -137,21 +137,18 @@ def show_graph_pil(graph_func, *args, **kwargs):
137
137
image .show ()
138
138
139
139
140
- def show_graph_graphviz (graph_func , * args , graph_filename = None , interactive = False , ** kwargs ):
140
+ def show_graph_graphviz (graph_func , * args , graph_filename = None , ** kwargs ):
141
141
# extract the format of the image
142
142
image_format = graph_filename .split ("." )[- 1 ] if graph_filename else "png"
143
143
144
144
# Generate graph with graphviz
145
145
digraph = _build_graphviz_graph (graph_func , * args , ** kwargs )
146
146
147
- # if we're in a notebook, return it directly for rendering
148
- if interactive :
149
- return digraph
150
-
151
- # otherwise output to file
152
- buffer = _graphviz_to_buffer (digraph = digraph , image_format = image_format )
153
- with open (graph_filename , "wb" ) as f :
154
- f .write (buffer .read ())
147
+ if graph_filename :
148
+ # output to file
149
+ buffer = _graphviz_to_buffer (digraph = digraph , image_format = image_format )
150
+ with open (graph_filename , "wb" ) as f :
151
+ f .write (buffer .read ())
155
152
return digraph
156
153
157
154
@@ -188,19 +185,16 @@ def show_graph(graph_func, *args, graph_filename=None, **kwargs):
188
185
else :
189
186
_HAVE_INTERACTIVE = False
190
187
191
- # display graph via pillow or ipydagred3
192
- if graph_filename in (None , "widget" ):
193
- if graph_filename == "widget" and not _HAVE_INTERACTIVE :
194
- raise RuntimeError ("Interactive graph viewer only works in Jupyter." )
195
-
188
+ if graph_filename == "widget" and not _HAVE_INTERACTIVE :
189
+ # widget only works in Jupyter for now
190
+ raise RuntimeError ("Interactive graph viewer only works in Jupyter." )
191
+ elif graph_filename == "widget" :
196
192
# render with ipydagred3
197
- if graph_filename == "widget" :
198
- return show_graph_widget (graph_func , * args , ** kwargs )
199
-
193
+ return show_graph_widget (graph_func , * args , ** kwargs )
194
+ elif graph_filename in ("" , None ) and not _HAVE_INTERACTIVE :
200
195
# render with pillow
201
196
return show_graph_pil (graph_func , * args , ** kwargs )
202
-
203
- # TODO we can show graphviz in jupyter without a filename, but preserving existing behavior for now
197
+ # render with graphviz
204
198
return show_graph_graphviz (
205
- graph_func , * args , graph_filename = graph_filename , interactive = _HAVE_INTERACTIVE , ** kwargs
199
+ graph_func , * args , graph_filename = graph_filename , ** kwargs
206
200
)
0 commit comments