Live DOM Updates ================ .. meta:: :description: Send minimal DOM updates from Python using domonic diffdom, JSON patches, server-side rendering, WebSocket messages, and browser DOM patching. :keywords: Python live DOM, WebSocket DOM updates, DOM patch JSON, server-side DOM diff, diffDOM Python, HTML over WebSocket ``domonic.diffdom`` can compare two rendered DOM trees and produce JSON-safe patch data. That gives you a practical starting point for live HTML updates: render on the server, diff against the previous tree, send only the changes. Create a Patch -------------- .. code-block:: python import json from domonic.diffdom import DiffDOM from domonic.html import button, div, h1, p old = div(h1("Inbox"), p("0 messages")) new = div(h1("Inbox"), p("1 message"), button("Open")) dd = DiffDOM() changes = dd.diff(old, new) print(json.dumps(changes, indent=2)) # [ # {"action": "modifyTextElement", "route": [1, 0], "oldValue": "0 messages", "newValue": "1 message"}, # {"action": "addElement", "route": [2], "element": {"nodeName": "button", ...}} # ] Apply a Patch ------------- .. code-block:: python dd.apply(old, changes) assert str(old) == str(new) Undo a Patch ------------ .. code-block:: python dd.undo(old, changes) print(old) #
0 messages