Skip to content

Commit 47f6dfc

Browse files
committed
WebguiScene.DownloadScreenshot()
To use it, you must pass an 'id' value to Draw(). This is because the scenes for screenshots need to be stored globally and are not cleaned up automatically, so we don't want to store them all.
1 parent 504927d commit 47f6dfc

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

python/webgui.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,16 @@ def GetData(self, set_minmax=True):
289289
)
290290
d["clipping_" + name] = val
291291

292-
if "js_code" in kwargs:
293-
d["on_init"] = kwargs["js_code"]
292+
id_ = kwargs.get("id", None)
293+
js_code = kwargs.get("js_code", "")
294+
if id_ is not None:
295+
js_code += f"""
296+
if(window._webgui_scenes === undefined)
297+
window._webgui_scenes = {{}};
298+
window._webgui_scenes['{id_}'] = scene;
299+
"""
300+
if js_code:
301+
d["on_init"] = js_code
294302

295303
if "min" in kwargs:
296304
d["funcmin"] = kwargs["min"]
@@ -351,6 +359,34 @@ def GetData(self, set_minmax=True):
351359

352360
return d
353361

362+
def DownloadScreenshot(self, filename="image.jpg"):
363+
from IPython.display import Javascript, display
364+
365+
if "id" not in self.kwargs:
366+
raise Exception(
367+
"To make a screenshot, please provide an id='some_unique_id' argument to Draw()"
368+
)
369+
format = filename.split(".")[-1].lower()
370+
display(
371+
Javascript(
372+
f"""
373+
{{
374+
var scene = window._webgui_scenes['{self.kwargs['id']}'];
375+
console.log("screenshot of scene", scene);
376+
const toimage = () => {{
377+
var link = document.createElement('a');
378+
link.href = scene.renderer.domElement.toDataURL('image/{format}');
379+
link.download = '{filename}';
380+
link.click();
381+
scene.event_handlers['afterrender'].pop(toimage);
382+
}};
383+
scene.on('afterrender', toimage);
384+
scene.render();
385+
}}
386+
"""
387+
)
388+
)
389+
354390

355391
bezier_trig_trafos = {} # cache trafos for different orders
356392

0 commit comments

Comments
 (0)