File: subgraphs_and_clusters.rst

package info (click to toggle)
python-graphviz 0.21-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,184 kB
  • sloc: python: 4,063; makefile: 13
file content (80 lines) | stat: -rw-r--r-- 2,341 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
Subgraphs & clusters
--------------------

:class:`.Graph` and :class:`.Digraph` objects have a
:meth:`~.Graph.subgraph` method for adding a subgraph to the instance.

There are two ways to use it: Either with a ready-made instance
of the same kind as the only argument (whose content is added as a subgraph)
or omitting the ``graph`` argument
(returning a context manager for defining the subgraph content more elegantly
within a ``with``-block).

First option, with ``graph`` as the only argument:

.. doctest::

    >>> import graphviz  # doctest: +NO_EXE

    >>> p = graphviz.Graph(name='parent')
    >>> p.edge('spam', 'eggs')

    >>> c = graphviz.Graph(name='child', node_attr={'shape': 'box'})
    >>> c.edge('foo', 'bar')

    >>> p.subgraph(c)

Second usage, with a ``with``-block (omitting the ``graph`` argument):

.. doctest::

    >>> p = graphviz.Graph('parent')  # doctest: +NO_EXE
    >>> p.edge('spam', 'eggs')

    >>> with p.subgraph(name='child', node_attr={'shape': 'box'}) as c:
    ...    c.edge('foo', 'bar')

Both produce the same result:

.. doctest::

    >>> print(p.source)  # doctest: +NORMALIZE_WHITESPACE +NO_EXE
    graph parent {
        spam -- eggs
        subgraph child {
            node [shape=box]
            foo -- bar
        }
    }

.. tip::

    If the ``name`` of a subgraph begins with ``'cluster'`` (all lowercase),
    the layout engine treats it as a special **cluster** subgraph
    (:ref:`example <cluster.py>` ).
    See the `Subgraphs and Clusters` section in `DOT language <DOT_>`_.

When :meth:`~.Graph.subgraph` is used as a context manager,
the new graph instance  is created with ``strict=None``
copying the **parent graph values** for ``directory``,
``engine``, ``format``, ``renderer``, ``formatter``,
and ``encoding``:

.. doctest::

    >>> doctest_mark_exe()  # skip this line

    >>> p = graphviz.Graph('parent', directory='doctest-output')

    >>> with p.subgraph(name='child') as c:
    ...    c.edge('bacon', 'eggs')
    ...    c.render().replace('\\', '/')
    'doctest-output/child.gv.pdf'

.. note::

    These copied attributes are only relevant for rendering the subgraph
    **independently** (i.e. as a stand-alone graph) from within the ``with``-block.


.. include:: _links.rst