Debian Codegraph @

docs/colored_view.rst

raw
1 .. _colored_view_label:
2
3 Colored View
4 ============
5
6 The `ColoredView` feature in `deepdiff` provides a human-readable, color-coded JSON output of the
7 differences between two objects. This feature is particularly useful for visualizing changes in a
8 clear and intuitive manner.
9
10 - **Color-Coded Differences:**
11
12 - **Added Elements:** Shown in green.
13 - **Removed Elements:** Shown in red.
14 - **Changed Elements:** The old value is shown in red, and the new value is shown in green.
15
16 Usage
17 -----
18
19 To use the `ColoredView`, simply pass the `COLORED_VIEW` option to the `DeepDiff` function:
20
21 .. code-block:: python
22
23 from deepdiff import DeepDiff
24 from deepdiff.helper import COLORED_VIEW
25
26 t1 = {"name": "John", "age": 30, "scores": [1, 2, 3], "address": {"city": "New York", "zip": "10001"}}
27 t2 = {"name": "John", "age": 31, "scores": [1, 2, 4], "address": {"city": "Boston", "zip": "10001"}, "new": "value"}
28
29 diff = DeepDiff(t1, t2, view=COLORED_VIEW)
30 print(diff)
31
32 Or from command line:
33
34 .. code-block:: bash
35
36 deep diff --view colored t1.json t2.json
37
38 The output will look something like this:
39
40 .. raw:: html
41
42 <pre style="background-color: #f8f8f8; padding: 1em; border-radius: 4px;">
43 {
44 "name": "John",
45 "age": <span style="color: #ff0000">30</span> -> <span style="color: #00aa00">31</span>,
46 "scores": [
47 1,
48 2,
49 <span style="color: #ff0000">3</span> -> <span style="color: #00aa00">4</span>
50 ],
51 "address": {
52 "city": <span style="color: #ff0000">"New York"</span> -> <span style="color: #00aa00">"Boston"</span>,
53 "zip": "10001"
54 },
55 <span style="color: #00aa00">"new": "value"</span>
56 }
57 </pre>
58
59 Colored Compact View
60 --------------------
61
62 For a more concise output, especially with deeply nested objects where many parts are unchanged,
63 the `ColoredView` with the compact option can be used. This view is similar but collapses
64 unchanged nested dictionaries to `{...}` and unchanged lists/tuples to `[...]`. To use the compact
65 option do:
66
67 .. code-block:: python
68
69 from deepdiff import DeepDiff
70 from deepdiff.helper import COLORED_COMPACT_VIEW
71
72 t1 = {"name": "John", "age": 30, "scores": [1, 2, 3], "address": {"city": "New York", "zip": "10001"}}
73 t2 = {"name": "John", "age": 31, "scores": [1, 2, 4], "address": {"city": "New York", "zip": "10001"}, "new": "value"}
74
75 diff = DeepDiff(t1, t2, view=COLORED_COMPACT_VIEW)
76 print(diff)
77
78 Or from command line:
79
80 .. code-block:: bash
81
82 deep diff --view colored_compact t1.json t2.json
83
84
85 The output will look something like this:
86
87 .. raw:: html
88
89 <pre style="background-color: #f8f8f8; padding: 1em; border-radius: 4px;">
90 {
91 "name": "John",
92 "age": <span style="color: #ff0000">30</span> -> <span style="color: #00aa00">31</span>,
93 "scores": [
94 1,
95 2,
96 <span style="color: #ff0000">3</span> -> <span style="color: #00aa00">4</span>
97 ],
98 "address": {...},
99 <span style="color: #00aa00">"new": "value"</span>
100 }
101 </pre>