File: piped_output.rst

package info (click to toggle)
python-graphviz 0.20.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,188 kB
  • sloc: python: 4,098; makefile: 13
file content (44 lines) | stat: -rw-r--r-- 1,197 bytes parent folder | download
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
Piped output
------------

To directly access the raw results from the Graphviz_ ``dot``
`layout command <DOT command_>`_ as binary :class:`bytes`
or as decoded :class:`str` (for plain-text formats like SVG)
instead of writing to a file, use the :meth:`~.Graph.pipe` method
of your :class:`.Graph` or :class:`.Digraph` object:

.. doctest::

    >>> import graphviz

    >>> h = graphviz.Graph('hello', format='svg')  # doctest: +NO_EXE

    >>> h.edge('Hello', 'World')

.. doctest::

    >>> doctest_mark_exe()  # skip this line

    >>> h.pipe(format='pdf')[:4]
    b'%PDF'

    >>> print(h.pipe(encoding='utf-8'))  # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <!DOCTYPE svg
    ...
    </svg>

.. tip::

    Because :meth:`~.Graph.pipe` returns the raw ``stdout``
    from the layout subprocess by default (:class:`bytes`),
    you usually want to decode the return value
    when piping into formats like ``'svg'`` or ``'plain'``,

.. caution::

    The output for :meth:`~.Graph.pipe` is buffered in memory,
    so avoid this method if the data size is large.


.. include:: _links.rst