1[![Build Status](https://www.travis-ci.com/pydot/pydot.svg?branch=master)](https://www.travis-ci.com/pydot/pydot)
2[![PyPI](https://img.shields.io/pypi/v/pydot.svg)](https://pypi.org/project/pydot/)
3
4
5About
6=====
7
8`pydot`:
9
10  - is an interface to [Graphviz][1]
11  - can parse and dump into the [DOT language][2] used by GraphViz,
12  - is written in pure Python,
13
14and [`networkx`][3] can convert its graphs to `pydot`.
15
16Development occurs at [GitHub][11], where you can report issues and
17contribute code.
18
19
20Examples
21========
22
23The examples here will show you the most common input, editing and
24output methods.
25
26Input
27-----
28
29No matter what you want to do with `pydot`, it will need some input to
30start with. Here are 3 common options:
31
321. Import a graph from an existing DOT-file.
33
34    Use this method if you already have a DOT-file describing a graph,
35    for example as output of another program. Let's say you already
36    have this `example.dot` (based on an [example from Wikipedia][12]):
37
38    ```dot
39    graph my_graph {
40       bgcolor="yellow";
41       a [label="Foo"];
42       b [shape=circle];
43       a -- b -- c [color=blue];
44    }
45    ```
46
47    Just read the graph from the DOT-file:
48
49    ```python
50    import pydot
51
52    graphs = pydot.graph_from_dot_file('example.dot')
53    graph = graphs[0]
54    ```
55
562. or: Parse a graph from an existing DOT-string.
57
58    Use this method if you already have a DOT-string describing a
59    graph in a Python variable:
60
61    ```python
62    import pydot
63
64    dot_string = """graph my_graph {
65        bgcolor="yellow";
66        a [label="Foo"];
67        b [shape=circle];
68        a -- b -- c [color=blue];
69    }"""
70
71    graphs = pydot.graph_from_dot_data(dot_string)
72    graph = graphs[0]
73    ```
74
753. or: Create a graph from scratch using pydot objects.
76
77    Now this is where the cool stuff starts. Use this method if you
78    want to build new graphs from Python.
79
80    ```python
81    import pydot
82
83    graph = pydot.Dot('my_graph', graph_type='graph', bgcolor='yellow')
84
85    # Add nodes
86    my_node = pydot.Node('a', label='Foo')
87    graph.add_node(my_node)
88    # Or, without using an intermediate variable:
89    graph.add_node(pydot.Node('b', shape='circle'))
90
91    # Add edges
92    my_edge = pydot.Edge('a', 'b', color='blue')
93    graph.add_edge(my_edge)
94    # Or, without using an intermediate variable:
95    graph.add_edge(pydot.Edge('b', 'c', color='blue'))
96    ```
97
98    Imagine using these basic building blocks from your Python program
99    to dynamically generate a graph. For example, start out with a
100    basic `pydot.Dot` graph object, then loop through your data while
101    adding nodes and edges. Use values from your data as labels, to
102    determine shapes, edges and so forth. This way, you can easily
103    build visualizations of thousands of interconnected items.
104
1054. or: Convert a NetworkX graph to a pydot graph.
106
107    NetworkX has conversion methods for pydot graphs:
108
109    ```python
110    import networkx
111    import pydot
112
113    # See NetworkX documentation on how to build a NetworkX graph.
114
115    graph = networkx.drawing.nx_pydot.to_pydot(my_networkx_graph)
116    ```
117
118Edit
119----
120
121You can now further manipulate your graph using pydot methods:
122
123- Add further nodes and edges:
124
125  ```python
126  graph.add_edge(pydot.Edge('b', 'd', style='dotted'))
127  ```
128
129- Edit attributes of graph, nodes and edges:
130
131  ```python
132  graph.set_bgcolor('lightyellow')
133  graph.get_node('b')[0].set_shape('box')
134  ```
135
136Output
137------
138
139Here are 3 different output options:
140
1411. Generate an image.
142
143    To generate an image of the graph, use one of the `create_*()` or
144    `write_*()` methods.
145
146    - If you need to further process the output in Python, the
147      `create_*` methods will get you a Python bytes object:
148
149      ```python
150      output_graphviz_svg = graph.create_svg()
151      ```
152
153    - If instead you just want to save the image to a file, use one of
154      the `write_*` methods:
155
156      ```python
157      graph.write_png('output.png')
158      ```
159
1602. Retrieve the DOT string.
161
162    There are two different DOT strings you can retrieve:
163
164    - The "raw" pydot DOT: This is generated the fastest and will
165      usually still look quite similar to the DOT you put in. It is
166      generated by pydot itself, without calling Graphviz.
167
168      ```python
169      # As a string:
170      output_raw_dot = graph.to_string()
171      # Or, save it as a DOT-file:
172      graph.write_raw('output_raw.dot')
173      ```
174
175    - The Graphviz DOT: You can use it to check how Graphviz lays out
176      the graph before it produces an image. It is generated by
177      Graphviz.
178
179      ```python
180      # As a bytes literal:
181      output_graphviz_dot = graph.create_dot()
182      # Or, save it as a DOT-file:
183      graph.write_dot('output_graphviz.dot')
184      ```
185
1863. Convert to a NetworkX graph.
187
188    Here as well, NetworkX has a conversion method for pydot graphs:
189
190    ```python
191    my_networkx_graph = networkx.drawing.nx_pydot.from_pydot(graph)
192    ```
193
194More help
195---------
196
197For more help, see the docstrings of the various pydot objects and
198methods. For example, `help(pydot)`, `help(pydot.Graph)` and
199`help(pydot.Dot.write)`.
200
201More [documentation contributions welcome][13].
202
203
204Installation
205============
206
207From [PyPI][4] using [`pip`][5]:
208
209`pip install pydot`
210
211From source:
212
213`python setup.py install`
214
215
216Dependencies
217============
218
219- [`pyparsing`][6]: used only for *loading* DOT files,
220  installed automatically during `pydot` installation.
221
222- GraphViz: used to render graphs as PDF, PNG, SVG, etc.
223  Should be installed separately, using your system's
224  [package manager][7], something similar (e.g., [MacPorts][8]),
225  or from [its source][9].
226
227
228License
229=======
230
231Distributed under an [MIT license][10].
232
233
234Contacts
235========
236
237Maintainers:
238- Sebastian Kalinowski <sebastian@kalinowski.eu> (GitHub: @prmtl)
239- Peter Nowee <peter@peternowee.com> (GitHub: @peternowee)
240
241Original author: Ero Carrera <ero.carrera@gmail.com>
242
243
244[1]: https://www.graphviz.org
245[2]: https://en.wikipedia.org/wiki/DOT_%28graph_description_language%29
246[3]: https://github.com/networkx/networkx
247[4]: https://pypi.python.org/pypi
248[5]: https://github.com/pypa/pip
249[6]: https://github.com/pyparsing/pyparsing
250[7]: https://en.wikipedia.org/wiki/Package_manager
251[8]: https://www.macports.org
252[9]: https://gitlab.com/graphviz/graphviz
253[10]: https://github.com/pydot/pydot/blob/master/LICENSE
254[11]: https://github.com/pydot/pydot
255[12]: https://en.wikipedia.org/w/index.php?title=DOT_(graph_description_language)&oldid=1003001464#Attributes
256[13]: https://github.com/pydot/pydot/issues/130
257