vega / altair

Declarative statistical visualization library for Python
https://altair-viz.github.io/
BSD 3-Clause "New" or "Revised" License
9.23k stars 784 forks source link

does HTML rendering need to depend on `outputDiv.id` #2616

Open jbloom opened 2 years ago

jbloom commented 2 years ago

I don't really understand how the HTML rendering of altair plots work, but one thing that is annoying at least for my workflows is that even if I make an identical plot twice, it has different HTML output. This makes git tracking annoying.

The responsible line is due to the outputDiv.id as shown below. I don't understand what this is doing, but am just curious if there is a way that it could not need to depend on this ID which changes each time an identical plot is remade:

if (outputDiv.id !== "altair-viz-24d195b1a15a4248b89adf5c26955a13") {
   outputDiv = document.getElementById("altair-viz-24d195b1a15a4248b89adf5c26955a13");
}

Please follow these steps to make it more efficient to respond to your feature request.

joelostblom commented 2 years ago

Based on this comment https://github.com/altair-viz/altair/issues/2520#issuecomment-984703085, I believe there needs to be unique output div UUID in order to place charts on unique locations on the page. Currently the output div is randomly generated, but I think it would be technically possible to change that to instead depend on the spec (e.g. a MD5 hash, maybe together with uuid3. However, this would require checking the document for identical existing UUIDs and append/increment a suffix if the same plot is created twice.

Having that said, I don't know if this placement strategy is needed for a Jupyter Notebook where there are other mechanisms in play to distribute content correctly on the page. It is possible that the output div uuid could be disabled altogether when working in a notebook without any side-effects, so if you want to play around with that and it turns out to be successful I would happily review a PR.