altair
altair copied to clipboard
Same HTML output when plot is remade
Fixes #2616.
Instead of dynamically changing the chart's id depending on the other charts, keep a history of the charts made and assign each one their respective id.
The id is a function of a "template" (in this case, "altair-viz"), the md5 hash of its respective spec (the "signature"), and the number of times a chart has been created.
Caveat: Since the counts
object only registers the number of times a chart is created, if the same cell is run twice, then the id will change. In order to fix this just restart the notebook and run all cells again at the end of your workflow. Personally, I don't think this is much of a problem.
This caveat is due to the fact that two identical charts are, well... identical, so there is no way to differentiate between the two apart from having a counter, or a random uuid, or adding the time of creation, etcetera. The counter, however, maintains states between kernel sessions.
Thanks for looking into this - unfortunately I don't think this is something we can merge. When using Jupyter Notebook, this will break in very strange and confusing ways if someone displays the same chart twice in the same notebook: the second time you attempt to display the same chart, it will update the first output cell where the chart is displayed. In JupyterLab it's even weirder: because all open notebooks exist in the same HTML browser window, if you attempt to display a chart that already exists in any notebook tab open in your jupyterlab session, the updated chart will display in that distant output cell.
I understand there are issues with the current model, but a solution that causes that kind of strange action-at-a-distance is not one we can merge. Unfortunately, given the frontend architectures of Jupyter and JupyterLab, I can't think of any way to solve the problems mentioned in the linked issue.
I don't think I follow. I tried replicating the problems you've mentioned but everything seems to work fine.
With respect to the first problem: the second time I tried to display the chart, the only thing that changes is the chart's id, but apparently Jupyter does not seem to have a problem with it. In order to solve the issue of changing id's using this PR, the user would have to restart the kernel and rerun everything, then do git add
.
With respect to the second problem: The chart can be displayed in both notebooks without any problem. However --and this is a problem I didn't notice before-- both charts have the same id. It seems that JL does not have any problem with this, but it still feels sketchy.
I understand if both of these caveats (having to restart the kernel and shared charts within a jl session having the same id) make it impossible for this PR to be merged.
Given the problems users have reported in the past, I think we need to avoid at all costs the possibility of charts being created with matching output div IDs. Div id collisions can be really confusing and difficult to debug. It's also quite difficult to comprehensively test, because the problems manifest differently depending on what browser & version is being used to view the notebook. All told, I don't think the problems fixed by this approach outweigh the potential problems it creates.
Clear review from @jakevdp. Closing for now.