Is it possible to create the “thing” to download only when the button is clicked, with a callback of some sort?
- It would be nicer to the CPU not to compute the rasterized version with every frame.
- it would help to avoid downloading the initial state of the animation in the case of a SVG that continuously updates its content (but is not recreated)
See https://beta.observablehq.com/d/fc1129681e026d0e for two problematic examples. (please feel free to fork and fix it if there is a simple solution.)
Here you go:
The reason we don’t do this with DOM.download is that it requires two separate clicks: there does not appear to be a way to initiate a download when you don’t yet know what the URL will be. But possibly we could still build this into DOM.download if the specified object is an async function.
Cool! I had to adapt a bit in the second case to avoid the button being recreated at each frame, and it works (more or less… sometimes I get “network errors” and empty files… but not every time!)
I just published some improvements. The value function can now be synchronous, and if so, you can download the value with a single click rather than needing two clicks, such as when serializing SVG.
I noticed in @Fil’s notebook that if I decided to adjust the globe after the first click on the “Download MAP1 as PNG” button, I had to click and then cancel the download and then click the download button again to save the updated globe.
Here’s a minor edit of the “Lazy Download” notebook which allows for “resetting” the button by dispatching an event:
This gives one hacky approach to fix the issue I described above: