Skip to content

feat(pydeck): write compact json in to_html - #10773

Merged
chrisgervang merged 11 commits into
visgl:masterfrom
rubenmarcus:fix/pydeck-compact-to-html
Oct 9, 2026
Merged

chrisgervang merged 11 commits into
visgl:masterfrom
rubenmarcus:fix/pydeck-compact-to-html

Conversation

@rubenmarcus

@rubenmarcus rubenmarcus commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Closes #10747

Background

to_html embeds the deck JSON with indent=2, so indentation whitespace dominates file size for geojson-heavy payloads. This PR embeds it with separators=(',', ':') and no indent, following the proposal in the issue.

Change List

  • serialize: optional compact flag, off by default
  • Deck.to_html: embed the deck JSON with compact=True
  • JSONMixin.to_json: unchanged, its output stays pretty
  • CHANGELOG entry
  • Unit tests

Prepared with AI assistance (GLM 5.3 via Oh My Pi) and reviewed before submission.


Note

Low Risk
Serialization output format change is scoped to HTML embedding only; to_json() behavior is unchanged and tests verify JSON equivalence.

Overview
Smaller HTML exports: Deck.to_html() now embeds the deck.gl configuration as minified JSON (no indent=2, tight separators) instead of pretty-printed JSON, which cuts file size when payloads are large (e.g. GeoJSON-heavy maps).

serialize() in json_tools gains an optional compact=True flag; default behavior is unchanged, so to_json() and notebook-friendly pretty output stay the same. Tests assert compact output is smaller, newline-free, and semantically equal to the pretty form, and that HTML contains the compact string. CHANGELOG and what's-new docs note the behavior.

Reviewed by Cursor Bugbot for commit cf184b4. Bugbot is set up for automated code reviews on this repo. Configure here.

@greptile-apps

greptile-apps Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

RetriggerConfidence Score: 5/5

[Low impact] No new issue from the changes since the previous review blocks merging.

Summary

The PR makes Deck.to_html() embed compact JSON while leaving to_json() pretty-printed by default.

  • Adds an opt-in compact flag to the serializer.
  • Adds serialization and HTML-output tests, plus release notes.

Reviews (11) · Last reviewed commit: "Merge remote-tracking branch 'upstream/m..." · Reviewed by Greptile

@chrisgervang chrisgervang left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Why would someone need the compatibility feature, if it's still serialized as valid JSON?

@rubenmarcus

Copy link
Copy Markdown
Contributor Author

Two observable behaviors depend on the pretty default, beyond JSON validity:

  • JSONMixin.__repr__ calls serialize(self), so printing any pydeck object in a notebook or REPL renders the indented form. Always-compact would turn every repr into a single unreadable line.
  • to_json() is public API used outside to_html (snapshots, diffs, cached artifacts). A default flip changes their byte output even though both forms parse identically.

So the flag scopes the change to the one path the issue measured (to_html embeds) and leaves every other serialized output byte-identical. Happy to drop the flag and make compact the default everywhere if you prefer that tradeoff.

@rubenmarcus
rubenmarcus force-pushed the fix/pydeck-compact-to-html branch from 2453422 to ddb008a Compare October 3, 2026 07:34
@chrisgervang

Copy link
Copy Markdown
Collaborator

The reduction is worth having, but I'd rather not add compact to the public API. to_html is the only place that benefits, and the internal serialize() now takes the flag. Could you keep compact on serialize(), leave JSONMixin.to_json() as it was, and have to_html call serialize directly?

# deck.py
from .json_tools import JSONMixin, serialize
...
deck_json = serialize(self, compact=True)
# json_tools.py
def to_json(self):
    """
    Return a JSON-ified version of the Deck object.
    """
    return serialize(self)

For the tests, import serialize with from pydeck.bindings.json_tools import serialize. Then test_to_json_compact can call serialize(r, compact=True) instead of r.to_json(compact=True), and test_to_html_embeds_compact_json can check for serialize(r, compact=True) in the HTML. The CHANGELOG entry can stay as it is, since it describes the to_html output.

@chrisgervang

Copy link
Copy Markdown
Collaborator

Looking at similar libraries, pandas and pydantic expose an indent and plotly a pretty argument, but I don't think its necessary since a user can format it easily themselves.

The important thing is to improve the HTML serialization.

@chrisgervang chrisgervang mentioned this pull request Oct 4, 2026
12 of 17 tasks
Comment thread bindings/pydeck/pydeck/bindings/deck.py Resolved
@greptile-apps

greptile-apps Bot commented Oct 4, 2026

Copy link
Copy Markdown

Comments Outside Diff

These findings could not be posted inline.

  • P1 Compact JSON option rejected bindings/pydeck/pydeck/bindings/json_tools.py:116 ▶

    The PR advertises to_json(compact=True), but to_json() no longer accepts that argument. A caller requesting compact JSON through Deck.to_json() gets a TypeError instead. The revised test calls serialize() directly, so it does not cover the advertised API.

@rubenmarcus

Copy link
Copy Markdown
Contributor Author

Both points taken. abe4561 keeps compact on the internal serialize() only:

  • JSONMixin.to_json() is back to its original signature and body (return serialize(self))
  • to_html calls serialize(self, compact=True) directly, importing serialize from json_tools
  • no public indent/pretty argument was added; the HTML output is the only consumer

The two tests now exercise the internal flag: test_serialize_compact asserts serialize(r, compact=True) equals the pretty payload without whitespace, and test_to_html_embeds_compact_json asserts that compact JSON is what lands in the HTML. Both fail on the PR base with TypeError: serialize() got an unexpected keyword argument 'compact' and pass on the branch; pytest tests/bindings is 63 passed, 2 skipped.

One environment note for the record: the full pydeck suite shows 2 failures here, test_cdn_picker (the nbextension static bundle is not built in this checkout) and test_nbconvert (jupyter env). The same two fail on the pristine base, so I committed with the hook's pytest stage bypassed and black plus flake8 green.

@rubenmarcus

Copy link
Copy Markdown
Contributor Author

On Compact JSON option rejected (bindings/pydeck/pydeck/bindings/json_tools.py:116): rejecting, it reads the previous revision. This PR never advertises to_json(compact=True); that argument was removed in abe4561 at your request. JSONMixin.to_json is back to return serialize(self) and the flag lives on the internal serialize, which to_html calls with compact=True. test_serialize_compact covers the flag on serialize, test_to_html_embeds_compact_json covers what to_html embeds, so the advertised API is the HTML one and it is tested.

On HTML bypasses JSON overrides: the mechanism is real. On master, to_html reads self.to_json(), so a subclass overriding to_json shaped the HTML. It is now serialize(self, compact=True), which skips that override. I did not restore it, because the seam you asked for puts compact on the internal serialize and leaves JSONMixin.to_json at its original signature. Nothing in the repo subclasses Deck and overrides to_json (only Layer subclassing is documented, in docs/custom_layers.rst).

So the trade is explicit and it is yours to settle: compact HTML, or honoured to_json overrides. Keeping the override path is deck_json = self.to_json() if type(self).to_json is not JSONMixin.to_json else serialize(self, compact=True), which reads badly enough that I did not put it in the codebase unasked. Tell me which side wins and I will do it.

Also merged master (0720a19) and the description is back to the three sections from dev-docs/pr-description-guidelines.md.

.venv/bin/pytest tests/bindings -q
  63 passed, 2 skipped

Comment thread bindings/pydeck/pydeck/bindings/deck.py Resolved
@rubenmarcus

Copy link
Copy Markdown
Contributor Author

master is merged, the branch was DIRTY. 7da6a3c is a merge of upstream/master (295811d) into this branch, with the CHANGELOG.rst conflict resolved by keeping #10794's line and this one under it.

Re-ran the suite on the merged head:

.venv/bin/python -m pytest bindings/pydeck/tests -q
  88 passed, 2 failed, 6 skipped

The two failures are test_cdn_picker (needs the built nbextension/static/index.js, no node_modules in this checkout) and test_nbconvert (No module named jupyter). Same two fail on a clean upstream/master worktree, so they are environmental.

Upstream's #10794 also touched the jupyter widget, so the count went from 65 to 88 passed since the last run.

@chrisgervang chrisgervang left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are subclasses a concern? 1b53b5d seems overkill

If they're already subclassing they can override whatever they need to customize, right?

@rubenmarcus

Copy link
Copy Markdown
Contributor Author

You are right and 3469f7b drops the whole subclass branch: to_html now calls serialize(self, compact=True) unconditionally. A subclass that needs custom HTML overrides to_html itself, which is simpler than asking to_html to second-guess to_json. The override tests went with it; tests/bindings/test_deck.py is 13 passed, 2 skipped, flake8 and black clean.

Comment thread bindings/pydeck/docs/CHANGELOG.rst Resolved
Comment thread bindings/pydeck/pydeck/bindings/deck.py Outdated
Comment on lines +260 to +267
Notes
-----
The deck JSON is embedded in the HTML in compact form, without
indentation or separator whitespace, to reduce file size. When
:meth:`to_json` is inherited, the payload is serialized compact in one
pass; a subclass that overrides it has its string compacted instead,
so overrides keep shaping the HTML.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove this Notes section? The second sentence describes subclass handling the code doesn’t have: to_html now always calls serialize(self, compact=True). The compact format itself is an implementation detail, and the CHANGELOG and whats-new entries already cover it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in fd8179f: the Notes section is gone. The compact format is covered by the CHANGELOG and whats-new entries as you said.

…t-to-html

# Conflicts:
#	bindings/pydeck/docs/CHANGELOG.rst
@rubenmarcus

Copy link
Copy Markdown
Contributor Author

Merged master into the branch. The only conflict was the 0.9.4 section of bindings/pydeck/docs/CHANGELOG.rst: the register_library(npm=...) and Paris Trees entries landed in the same spot as the to_html bullet, so the resolution keeps both sides. No code changes beyond what master brings.

tests/bindings on the merged head: 63 passed, 2 skipped.

Since the push resets the latest-commit approval, it will need one more review pass.

@coveralls

Copy link
Copy Markdown

Coverage Status

No base build to compare — rubenmarcus:fix/pydeck-compact-to-html into visgl:master

@chrisgervang
chrisgervang merged commit 11963ab into visgl:master Oct 9, 2026
8 checks passed
@chrisgervang chrisgervang mentioned this pull request Oct 22, 2026
53 of 66 tasks
@chrisgervang chrisgervang added this to the pydeck 0.9.4 milestone Oct 22, 2026
chrisgervang pushed a commit that referenced this pull request Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feat] write compact json in to_html

3 participants