'Concatenate `difflib.HtmlDiff` instances

Objective & Question

I have a file with 'database' entries, each with a number of items. I want to show an entry-by-entry comparison (instead of a global comparison where the comparison might get avoidably lost).

Loosely speaking my question is: How do I concatenate difflib.HtmlDiff instances?.

Example & Current workaround

Consider this example, with my current workaround of just concatenating multiple HTML pages:

import difflib

a0 = """
Foo2022:
    title: Foo
    journal: Journal of Foo
"""

a1 = """
Foo2022:
    title: Foo
    journal: J. of Foo
"""

b0 = """
Bar2022:
    title: Bar
    journal: Journal of Bar
"""

b1 = """
Bar2021:
    title: Description of bar
    journal: J. of Bar
"""

diff_a = difflib.HtmlDiff(wrapcolumn=100).make_file(
    a0.splitlines(keepends=True),
    a1.splitlines(keepends=True),
)

diff_b = difflib.HtmlDiff(wrapcolumn=100).make_file(
    b0.splitlines(keepends=True),
    b1.splitlines(keepends=True),
)

with open("t.html", "w") as file:
    file.write(diff_a + diff_b)

This, however, gives multiple legends (and incorrect line-numbers, but that I could live with). I could, of course, strip things from the HTML files, but I would like to avoid this as it reduces maintainability and readability.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source