@@ -376,7 +376,7 @@ def _compute_sequential_method(self, named_inputs, outputs):
376376 return {k : cache [k ] for k in iter (cache ) if k in outputs }
377377
378378
379- def plot (self , filename = None , show = False ,
379+ def plot (self , filename = None , show = False , jupyter = None ,
380380 inputs = None , outputs = None , solution = None ):
381381 """
382382 Plot a *Graphviz* graph and return it, if no other argument provided.
@@ -385,8 +385,12 @@ def plot(self, filename=None, show=False,
385385 Write diagram into a file.
386386 Common extensions are ``.png .dot .jpg .jpeg .pdf .svg``
387387 call :func:`network.supported_plot_formats()` for more.
388- :param boolean show:
388+ :param show:
389389 If it evaluates to true, opens the diagram in a matplotlib window.
390+ If it equals `-1``, it plots but does not open the Window.
391+ :param jupyter:
392+ If it evaluates to true, return an SVG suitable to render
393+ in *jupyter notebook cells* (`ipython` must be installed).
390394 :param inputs:
391395 an optional name list, any nodes in there are plotted
392396 as a "house"
@@ -402,8 +406,8 @@ def plot(self, filename=None, show=False,
402406
403407 See :func:`network.plot_graph` for the plot legend and example code.
404408 """
405- return plot_graph (self .graph , filename , show , self . steps ,
406- inputs , outputs , solution )
409+ return plot_graph (self .graph , filename , show , jupyter ,
410+ self . steps , inputs , outputs , solution )
407411
408412
409413def ready_to_schedule_operation (op , has_executed , graph ):
@@ -461,8 +465,8 @@ def supported_plot_formats():
461465 return [".%s" % f for f in pydot .Dot ().formats ]
462466
463467
464- def plot_graph (graph , filename = None , show = False , steps = None ,
465- inputs = None , outputs = None , solution = None ):
468+ def plot_graph (graph , filename = None , show = False , jupyter = False ,
469+ steps = None , inputs = None , outputs = None , solution = None ):
466470 """
467471 Plot a *Graphviz* graph/steps and return it, if no other argument provided.
468472
@@ -494,9 +498,12 @@ def plot_graph(graph, filename=None, show=False, steps=None,
494498 Write diagram into a file.
495499 Common extensions are ``.png .dot .jpg .jpeg .pdf .svg``
496500 call :func:`network.supported_plot_formats()` for more.
497- :param boolean show:
501+ :param show:
498502 If it evaluates to true, opens the diagram in a matplotlib window.
499- If it equals ``-1``, it plots but does not open the Window.
503+ If it equals `-1``, it plots but does not open the Window.
504+ :param jupyter:
505+ If it evaluates to true, return an SVG suitable to render
506+ in *jupyter notebook cells* (`ipython` must be installed).
500507 :param steps:
501508 a list of nodes & instructions to overlay on the diagram
502509 :param inputs:
@@ -514,15 +521,18 @@ def plot_graph(graph, filename=None, show=False, steps=None,
514521
515522 **Example:**
516523
517- >>> netop = compose(name="netop")(
524+ >>> from graphkit import compose, operation
525+ >>> from graphkit.modifiers import optional
526+
527+ >>> pipeline = compose(name="pipeline")(
518528 ... operation(name="add", needs=["a", "b1"], provides=["ab1"])(add),
519529 ... operation(name="sub", needs=["a", optional("b2")], provides=["ab2"])(lambda a, b=1: a-b),
520530 ... operation(name="abb", needs=["ab1", "ab2"], provides=["asked"])(add),
521531 ... )
522532
523533 >>> inputs = {'a': 1, 'b1': 2}
524- >>> solution=netop (inputs)
525- >>> netop .plot('plot.svg', inputs=inputs, solution=solution, outputs=['asked', 'b1']);
534+ >>> solution=pipeline (inputs)
535+ >>> pipeline .plot('plot.svg', inputs=inputs, solution=solution, outputs=['asked', 'b1']);
526536
527537 """
528538 import pydot
@@ -596,7 +606,8 @@ def get_node_name(a):
596606 penwidth = 3 , arrowhead = "vee" )
597607 g .add_edge (edge )
598608
599- # save plot
609+ # Save plot
610+ #
600611 if filename :
601612 formats = supported_plot_formats ()
602613 _basename , ext = os .path .splitext (filename )
@@ -608,7 +619,14 @@ def get_node_name(a):
608619
609620 g .write (filename , format = ext .lower ()[1 :])
610621
611- # display graph via matplotlib
622+ ## Return an SVG renderable in jupyter.
623+ #
624+ if jupyter :
625+ from IPython .display import SVG
626+ g = SVG (data = g .create_svg ())
627+
628+ ## Display graph via matplotlib
629+ #
612630 if show :
613631 import matplotlib .pyplot as plt
614632 import matplotlib .image as mpimg
0 commit comments