Skip to content

Commit 25df545

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 e0c990b commit 25df545

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

csp/showgraph.py

+15-21
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def _build_graph_for_viz(graph_func, *args, **kwargs):
8484
def _build_graphviz_graph(graph_func, *args, **kwargs):
8585
from graphviz import Digraph
8686

87-
nodes, edges = _build_graph_for_viz(graph_func=graph_func, *args, **kwargs)
87+
nodes, edges = _build_graph_for_viz(graph_func, *args, **kwargs)
8888

8989
digraph = Digraph(strict=True)
9090
digraph.attr(rankdir="LR", size="150,150")
@@ -137,21 +137,18 @@ def show_graph_pil(graph_func, *args, **kwargs):
137137
image.show()
138138

139139

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):
141141
# extract the format of the image
142142
image_format = graph_filename.split(".")[-1] if graph_filename else "png"
143143

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(
205-
graph_func, *args, graph_filename=graph_filename, interactive=_HAVE_INTERACTIVE, **kwargs
199+
graph_func, *args, graph_filename=graph_filename, **kwargs
206200
)

examples/98_just_for_fun/e1_csp_nand_computer.py

+3
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,17 @@ def my_graph(bits: int = 16):
9999
csp.print("y", basket_to_number(y))
100100
csp.print("x_bits", basket_to_bitstring(x))
101101
csp.print("y_bits", basket_to_bitstring(y))
102+
102103
add = addInt(x, y)
104+
103105
csp.print("x+y", basket_to_number(add))
104106
csp.print("x+y_bits", basket_to_bitstring(add))
105107

106108

107109
def main():
108110
# Show graph with 4-bit ints to limit size
109111
csp.showgraph.show_graph(my_graph, 4)
112+
110113
csp.run(my_graph, starttime=datetime(2022, 6, 24))
111114

112115

0 commit comments

Comments
 (0)