File: colored_view.rst

package info (click to toggle)
deepdiff 8.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,976 kB
  • sloc: python: 16,739; makefile: 167
file content (101 lines) | stat: -rw-r--r-- 3,062 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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
.. _colored_view_label:

Colored View
============

The `ColoredView` feature in `deepdiff` provides a human-readable, color-coded JSON output of the
differences between two objects. This feature is particularly useful for visualizing changes in a
clear and intuitive manner.

- **Color-Coded Differences:**

  - **Added Elements:** Shown in green.
  - **Removed Elements:** Shown in red.
  - **Changed Elements:** The old value is shown in red, and the new value is shown in green.

Usage
-----

To use the `ColoredView`, simply pass the `COLORED_VIEW` option to the `DeepDiff` function:

.. code-block:: python

    from deepdiff import DeepDiff
    from deepdiff.helper import COLORED_VIEW

    t1 = {"name": "John", "age": 30, "scores": [1, 2, 3], "address": {"city": "New York", "zip": "10001"}}
    t2 = {"name": "John", "age": 31, "scores": [1, 2, 4], "address": {"city": "Boston", "zip": "10001"}, "new": "value"}

    diff = DeepDiff(t1, t2, view=COLORED_VIEW)
    print(diff)

Or from command line:

.. code-block:: bash

    deep diff --view colored t1.json t2.json

The output will look something like this:

.. raw:: html

    <pre style="background-color: #f8f8f8; padding: 1em; border-radius: 4px;">
    {
      "name": "John",
      "age": <span style="color: #ff0000">30</span> -> <span style="color: #00aa00">31</span>,
      "scores": [
        1,
        2,
        <span style="color: #ff0000">3</span> -> <span style="color: #00aa00">4</span>
      ],
      "address": {
        "city": <span style="color: #ff0000">"New York"</span> -> <span style="color: #00aa00">"Boston"</span>,
        "zip": "10001"
      },
      <span style="color: #00aa00">"new": "value"</span>
    }
    </pre>

Colored Compact View
--------------------

For a more concise output, especially with deeply nested objects where many parts are unchanged,
the `ColoredView` with the compact option can be used. This view is similar but collapses
unchanged nested dictionaries to `{...}` and unchanged lists/tuples to `[...]`. To use the compact
option do:

.. code-block:: python

    from deepdiff import DeepDiff
    from deepdiff.helper import COLORED_COMPACT_VIEW

    t1 = {"name": "John", "age": 30, "scores": [1, 2, 3], "address": {"city": "New York", "zip": "10001"}}
    t2 = {"name": "John", "age": 31, "scores": [1, 2, 4], "address": {"city": "New York", "zip": "10001"}, "new": "value"}

    diff = DeepDiff(t1, t2, view=COLORED_COMPACT_VIEW)
    print(diff)

Or from command line:

.. code-block:: bash

    deep diff --view colored_compact t1.json t2.json


The output will look something like this:

.. raw:: html

    <pre style="background-color: #f8f8f8; padding: 1em; border-radius: 4px;">
    {
      "name": "John",
      "age": <span style="color: #ff0000">30</span> -> <span style="color: #00aa00">31</span>,
      "scores": [
        1,
        2,
        <span style="color: #ff0000">3</span> -> <span style="color: #00aa00">4</span>
      ],
      "address": {...},
      <span style="color: #00aa00">"new": "value"</span>
    }
    </pre>