K3D-tools / K3D-jupyter

K3D lets you create 3D plots backed by WebGL with high-level API (surfaces, isosurfaces, voxels, mesh, cloud points, vtk objects, volume renderer, colormaps, etc). The primary aim of K3D-jupyter is to be easy for use as stand alone package like matplotlib, but also to allow interoperation with existing libraries as VTK.
MIT License
921 stars 123 forks source link

Embedding using ipywidgets.embed not working properly #320

Closed jpomoell closed 2 years ago

jpomoell commented 2 years ago

I am using k3d in a setting where the user can interact with the k3d.plot via an interface built using ipywidgets. While everything works great in the notebook, I would also like to embed snapshots in html pages, which is causing issues. As an example, in the following

import k3d
import k3d.platonic
import ipywidgets as widgets
import ipywidgets.embed as embed

mesh = k3d.platonic.Dodecahedron().mesh
plot = k3d.plot(menu_visibility=False)
plot += mesh

checkbox = widgets.Checkbox(value=True, description="Visible")
widgets.jslink((checkbox, 'value'), (mesh, 'visible'))
widget = widgets.VBox([checkbox, plot])

embed.embed_minimal_html("k3d_embedded.html", views=[widget])

the generated html file shows the plot and the checkbox works. However, the usual mouse interaction does not work, e.g. rotating the view does not work. When trying to rotate the view, the browser console reports

embed-amd.js:69 Uncaught Syncing error: no comm channel defined

Creating a snapshot of only the plot using plot.get_snapshot() works, but I am unsure how to get both the widgets and the plot embedded at the same time. Any help would be very much appreciated!

artur-trzesiok commented 2 years ago

Thanks for a feedback. I can reproduce this error. It is in save here:

        this.cameraChangeId = this.K3DInstance.on(this.K3DInstance.events.CAMERA_CHANGE, (control) => {
            if ((new Date()).getTime() - self.model.lastCameraSync > 200) {
                self.model.lastCameraSync = (new Date()).getTime();
                self.model.save('camera', control, { patch: true });
            }
        });

I don't know why I cannot save this model. For sure we can wrap it in K3D with try/catch to avoid stoping whole process. I will check that and return with outcome!

ps. or even: or even: if (self.model._comm_live) {

artur-trzesiok commented 2 years ago

Yup! That solves the problem :)

marscher commented 2 years ago

Sorry, I should have studied the documentation more carefully.