Debian Codegraph @

docs/howto/graphics.md

raw
1 # Render a document as a graph (PNG/SVG/PDF)
2
3 `prov.dot` turns a document into a [pydot](https://pypi.org/project/pydot/) graph, which
4 can then be written out in any format Graphviz supports.
5
6 ```{important}
7 Rendering needs a local **Graphviz** installation (the `dot` executable) in addition to the
8 `pydot` Python package (which is a core dependency of `prov`, always installed). Installing
9 `pydot` alone is not enough — this is the most common source of confusion:
10
11 - **macOS**: `brew install graphviz`
12 - **Debian/Ubuntu**: `apt install graphviz`
13 - **Windows**: download and run the installer from <https://graphviz.org/download/>
14
15 Without it, `dot.write_*()` calls below fail (typically with a `FileNotFoundError` for the
16 `dot` executable, surfaced by `pydot` as an assertion/`Exception` depending on version) —
17 verify Graphviz is on `PATH` with `dot -V` before debugging your own code.
18 ```
19
20 ## Convert a document to a `pydot.Dot`
21
22 ```python
23 import prov.model as pm
24 from prov.dot import prov_to_dot
25
26 document = pm.ProvDocument()
27 document.set_default_namespace("http://example.org/")
28 e = document.entity("e1")
29 a = document.activity("a1")
30 document.wasGeneratedBy(e, a)
31
32 dot = prov_to_dot(document)
33 ```
34
35 ## Write PNG, SVG, or PDF
36
37 `pydot.Dot` has a `write_<format>` method for most Graphviz output formats:
38
39 ```python
40 dot.write_png("document.png")
41 dot.write_svg("document.svg")
42 dot.write_pdf("document.pdf")
43 ```
44
45 ## Layout direction
46
47 `direction` controls the rank direction Graphviz lays the graph out in — `"BT"`
48 (bottom-to-top, the default), `"TB"`, `"LR"`, or `"RL"`:
49
50 ```python
51 dot_lr = prov_to_dot(document, direction="LR")
52 dot_lr.write_svg("document-lr.svg")
53 ```
54
55 ## Hide attribute annotations
56
57 By default every element and relation gets an attached note node listing its non-formal
58 attributes. Turn either off to declutter dense graphs:
59
60 ```python
61 dot_plain = prov_to_dot(
62 document,
63 show_element_attributes=False,
64 show_relation_attributes=False,
65 )
66 ```
67
68 ## Use labels instead of identifiers
69
70 Pass `use_labels=True` to show each element's `prov:label` (falling back to its
71 identifier) as the node text instead of always showing the identifier:
72
73 ```python
74 document.entity("e1", {pm.PROV_LABEL: "Crime report"})
75 dot_labelled = prov_to_dot(document, use_labels=True)
76 ```
77
78 ## Hide n-ary relation elements
79
80 Relations with more than two formal attributes (e.g. a `wasDerivedFrom` recording an
81 activity and usage/generation) render every element by default. Set `show_nary=False` to
82 draw only the first two:
83
84 ```python
85 dot_binary = prov_to_dot(document, show_nary=False)
86 ```
87
88 ## All together
89
90 ```python
91 dot = prov_to_dot(
92 document,
93 show_nary=True,
94 use_labels=False,
95 direction="BT",
96 show_element_attributes=True,
97 show_relation_attributes=True,
98 )
99 ```
100
101 See {py:func}`prov.dot.prov_to_dot` for the full parameter reference.