You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 KiB

Tales to tell

module: graphviz

In [2]:
from graphviz import Digraph
In [3]:
dot = Digraph()
In [4]:
# Add nodes 1 and 2
dot.node('1')
dot.node('2')

# Add edge between 1 and 2
dot.edges(['12'])
In [5]:
dot
Out[5]:
1 2
In [6]:
text = """I have a tale to tell
Sometimes it gets so hard to hide it well
I was not ready for the fall
Too blind to see the writing on the wall"""
In [7]:
for word in text.split():
    print (word)
I
have
a
tale
to
tell
Sometimes
it
gets
so
hard
to
hide
it
well
I
was
not
ready
for
the
fall
Too
blind
to
see
the
writing
on
the
wall
In [8]:
from itertools import tee
def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b)
In [9]:
for w1, w2 in pairwise(text.split()):
    print (w1, w2)
I have
have a
a tale
tale to
to tell
tell Sometimes
Sometimes it
it gets
gets so
so hard
hard to
to hide
hide it
it well
well I
I was
was not
not ready
ready for
for the
the fall
fall Too
Too blind
blind to
to see
see the
the writing
writing on
on the
the wall
In [10]:
for w1, w2 in pairwise(text.split()):
    dot.edge(w1, w2)
In [11]:
dot
Out[11]:
1 2 I have was a tale to tell hide see Sometimes it gets well so hard not ready for the fall writing wall Too blind on
In [12]:
dot.render("tale")
Out[12]:
'tale.pdf'
In [13]:
dot.render?
Signature:
dot.render(
    filename=None,
    directory=None,
    view=False,
    cleanup=False,
    format=None,
    renderer=None,
    formatter=None,
    quiet=False,
    quiet_view=False,
)
Docstring:
Save the source to file and render with the Graphviz engine.

Args:
    filename: Filename for saving the source (defaults to ``name`` + ``'.gv'``)
    directory: (Sub)directory for source saving and rendering.
    view (bool): Open the rendered result with the default application.
    cleanup (bool): Delete the source file after rendering.
    format: The output format used for rendering (``'pdf'``, ``'png'``, etc.).
    renderer: The output renderer used for rendering (``'cairo'``, ``'gd'``, ...).
    formatter: The output formatter used for rendering (``'cairo'``, ``'gd'``, ...).
    quiet (bool): Suppress ``stderr`` output from the layout subprocess.
    quiet_view (bool): Suppress ``stderr`` output from the viewer process
                       (implies ``view=True``, ineffective on Windows).
Returns:
    The (possibly relative) path of the rendered file.
Raises:
    ValueError: If ``format``, ``renderer``, or ``formatter`` are not known.
    graphviz.RequiredArgumentError: If ``formatter`` is given but ``renderer`` is None.
    graphviz.ExecutableNotFound: If the Graphviz executable is not found.
    subprocess.CalledProcessError: If the exit status is non-zero.
    RuntimeError: If viewer opening is requested but not supported.

The layout command is started from the directory of ``filepath``, so that
references to external files (e.g. ``[image=...]``) can be given as paths
relative to the DOT source file.
File:      /opt/tljh/user/lib/python3.7/site-packages/graphviz/files.py
Type:      method
In [14]:
# Example of using the graph as a generator ??
In [15]:
dot.render("tale", format="png")
Out[15]:
'tale.png'

In [ ]: