Skip to main content

Tools for converting HTMLs to dicts & calculating diff between them

Project description

diff4html

Tools for converting HTMLs to dicts & calculating diff between them.

🔎 What for?

Well, if you need to calculate the diff between HTML pages without inplace markup (with <ins> & <del> tags), or maybe you experience issues with matching these tags later, or even find the htmldiff's behavior not entirely accurate, then diff4html may be a great fit for you.

Unlike htmldiff, the result here is a difference object. This object is a list containing information about each change. Each change, in turn, is described by the start and end indices of the modification in the original HTML page, along with the subject of the change.

Application of this is fully handled by diff4html and can be triggered via simple Python API, so you don't have to restore snapshots on your own.

🛠️ Installation

pip install diff4html

⚡️ Usage

Let's start with running IPython or Jupyter and making two HtmlDicts for our pages. The one for an example.org page and the other for slightly modified version of it:

HtmlDict here is simply a representation of HTML tree built on native Python dict & list data types.

In [1]: import requests
        from diff4html import HtmlDict

        page_1 = HtmlDict(requests.get("https://example.org").text)
        page_2 = HtmlDict("""
            <html>
                <head>
                    <title>Example Domain</title>
                    <!-- NOTICE: missing meta & styles here -->
                </head>
                <body>
                    <div>
                        <h1>Example Domain modified</hi> <!-- NOTICE: changed text-->
                        <!-- NOTICE: missing rest -->
                    </div>
                </body>
            </html>
        """)
        page_2
Out[1]: <HtmlDict({...})>

Let's then calculate diff between them. For example: I don't want to store the whole page 2 source code and want only delta to remain.

In [2]: diff = page_2 - page_1
        diff
Out[2]: <HtmlDiff([...])>

What this code does is it determines which parts of page 1 were deleted, modified, and added in page 2, and returns information about all these changes in the form of an HtmlDiff object.

HtmlDiff, in the context of page 2 - page 1, is a list of changes in the form of (s, e, t), where s and e are the start and end indices pointing to the differing element in page 1, and t is the content of the corresponding differing element in page 2.

If one day I want to restore the entire source code of the page 2 I can do the following. We can check their equality right away:

In [3]: page_2_restored = page_1 + diff # you can think of it as: 
        # page_1 + (page_2 - page_1) => page_1 + page_2 - page_1 => page_2
        page_2_restored == page_2
Out[3]: True

BTW: there is a hash mechanism under the hood that protects the delta to be applied to any random html:

In [4]: diff + page_2 # diff can be applied to page_1 only
Out[4]: ValueError: wrong snapshot used for applying diff

And if I want to use lxml after all here's a pretty straight workaround for it:

In [5]: page_2_restored.to_lxml()
Out[5]: <Element div at 0x000000000>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

diff4html-0.1.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

diff4html-0.1-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file diff4html-0.1.tar.gz.

File metadata

  • Download URL: diff4html-0.1.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for diff4html-0.1.tar.gz
Algorithm Hash digest
SHA256 18ecba68e179bde834b3450aa07a799c48b5497de4f5490b0d1ad955ec5f9048
MD5 749b2da7f021cb9119ae9d2162c38d2f
BLAKE2b-256 70b8fe4ff65be829b0b6bc8999b5077521b14470cd686ca8d5d6fda3688690bc

See more details on using hashes here.

Provenance

The following attestation bundles were made for diff4html-0.1.tar.gz:

Publisher: publish.yml on dsp-shp/diff4html

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file diff4html-0.1-py3-none-any.whl.

File metadata

  • Download URL: diff4html-0.1-py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for diff4html-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a36f6735dbaf4ae73498d1efc00b2a9856fa4461d2ddbe6c4a8a37de8733a46d
MD5 a8aaaba53dc42a1433a855696b5f5552
BLAKE2b-256 e0e12c977c9076d6e8614dff3d31e404ff90845d38fee55bfb01a8d49814e2b6

See more details on using hashes here.

Provenance

The following attestation bundles were made for diff4html-0.1-py3-none-any.whl:

Publisher: publish.yml on dsp-shp/diff4html

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page