Skip to content

Commit

Permalink
Tweak show_graph logic a bit to simplify, allow rendering in jupyter …
Browse files Browse the repository at this point in the history
…via graphviz by default

Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com>
  • Loading branch information
timkpaine committed Jul 25, 2024
1 parent e0c990b commit 0ed0c52
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 23 deletions.
38 changes: 15 additions & 23 deletions csp/showgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def _build_graph_for_viz(graph_func, *args, **kwargs):
def _build_graphviz_graph(graph_func, *args, **kwargs):
from graphviz import Digraph

nodes, edges = _build_graph_for_viz(graph_func=graph_func, *args, **kwargs)
nodes, edges = _build_graph_for_viz(graph_func, *args, **kwargs)

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


def show_graph_graphviz(graph_func, *args, graph_filename=None, interactive=False, **kwargs):
def show_graph_graphviz(graph_func, *args, graph_filename=None, **kwargs):
# extract the format of the image
image_format = graph_filename.split(".")[-1] if graph_filename else "png"

# Generate graph with graphviz
digraph = _build_graphviz_graph(graph_func, *args, **kwargs)

# if we're in a notebook, return it directly for rendering
if interactive:
return digraph

# otherwise output to file
buffer = _graphviz_to_buffer(digraph=digraph, image_format=image_format)
with open(graph_filename, "wb") as f:
f.write(buffer.read())
if graph_filename:
# output to file
buffer = _graphviz_to_buffer(digraph=digraph, image_format=image_format)
with open(graph_filename, "wb") as f:
f.write(buffer.read())
return digraph


Expand Down Expand Up @@ -188,19 +185,14 @@ def show_graph(graph_func, *args, graph_filename=None, **kwargs):
else:
_HAVE_INTERACTIVE = False

# display graph via pillow or ipydagred3
if graph_filename in (None, "widget"):
if graph_filename == "widget" and not _HAVE_INTERACTIVE:
raise RuntimeError("Interactive graph viewer only works in Jupyter.")

if graph_filename == "widget" and not _HAVE_INTERACTIVE:
# widget only works in Jupyter for now
raise RuntimeError("Interactive graph viewer only works in Jupyter.")
elif graph_filename == "widget":
# render with ipydagred3
if graph_filename == "widget":
return show_graph_widget(graph_func, *args, **kwargs)

return show_graph_widget(graph_func, *args, **kwargs)
elif graph_filename in ("", None) and not _HAVE_INTERACTIVE:
# render with pillow
return show_graph_pil(graph_func, *args, **kwargs)

# TODO we can show graphviz in jupyter without a filename, but preserving existing behavior for now
return show_graph_graphviz(
graph_func, *args, graph_filename=graph_filename, interactive=_HAVE_INTERACTIVE, **kwargs
)
# render with graphviz
return show_graph_graphviz(graph_func, *args, graph_filename=graph_filename, **kwargs)
3 changes: 3 additions & 0 deletions examples/98_just_for_fun/e1_csp_nand_computer.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,17 @@ def my_graph(bits: int = 16):
csp.print("y", basket_to_number(y))
csp.print("x_bits", basket_to_bitstring(x))
csp.print("y_bits", basket_to_bitstring(y))

add = addInt(x, y)

csp.print("x+y", basket_to_number(add))
csp.print("x+y_bits", basket_to_bitstring(add))


def main():
# Show graph with 4-bit ints to limit size
csp.showgraph.show_graph(my_graph, 4)

csp.run(my_graph, starttime=datetime(2022, 6, 24))


Expand Down

0 comments on commit 0ed0c52

Please sign in to comment.