Skip to content

Commit b4b4cb0

Browse files
committed
Tweak show_graph logic a bit to simplify, allow rendering in jupyter via graphviz by default
Signed-off-by: Tim Paine <[email protected]>
1 parent 57942a2 commit b4b4cb0

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

Diff for: csp/showgraph.py

+12-18
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,11 @@ def show_graph_graphviz(graph_func, *args, graph_filename=None, interactive=Fals
144144
# Generate graph with graphviz
145145
digraph = _build_graphviz_graph(graph_func, *args, **kwargs)
146146

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())
155152
return digraph
156153

157154

@@ -188,19 +185,16 @@ def show_graph(graph_func, *args, graph_filename=None, **kwargs):
188185
else:
189186
_HAVE_INTERACTIVE = False
190187

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":
196192
# 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:
200195
# render with pillow
201196
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
204198
return show_graph_graphviz(
205199
graph_func, *args, graph_filename=graph_filename, interactive=_HAVE_INTERACTIVE, **kwargs
206200
)

0 commit comments

Comments
 (0)