jupyter-widgets / pythreejs

A Jupyter - Three.js bridge
https://pythreejs.readthedocs.io
Other
934 stars 185 forks source link

DirectionalLight on Google Colab #388

Closed francesco-ballarin closed 9 months ago

francesco-ballarin commented 1 year ago

Hi, I am trying to use pythreejs on Google Colab, and I am seeing a failure related to DirectionalLight.

A minimal notebook to reproduce has the following content:

# Cell 1: installation
!pip install pythreejs
# Cell 2: allow custom widgets
from google.colab import output
output.enable_custom_widget_manager()
# Cell 3: import pythreejs
from pythreejs import *
# Cell 4: create a simple widget
BoxGeometry(
    width=5,
    height=10,
    depth=15,
    widthSegments=5,
    heightSegments=10,
    depthSegments=15)
# Cell 5: create a light object
DirectionalLight(color='white', position=[3, 5, 1], intensity=0.5)

Cell 4 runs correctly, the box shows up and the user can interact with it. Cell 5 runs, a black background gets rendered, but immediately below the black widget there are several instances of

---------------------------------------------------------------------------
TraitError                                Traceback (most recent call last)
[/usr/local/lib/python3.7/dist-packages/ipywidgets/widgets/widget.py](https://localhost:8080/#) in _handle_msg(self, msg)
    754                 if 'buffer_paths' in data:
    755                     _put_buffers(state, data['buffer_paths'], msg['buffers'])
--> 756                 self.set_state(state)
    757 
    758         # Handle a state request.

5 frames
[/usr/local/lib/python3.7/dist-packages/ipywidgets/widgets/widget.py](https://localhost:8080/#) in set_state(self, sync_data)
    623                     from_json = self.trait_metadata(name, 'from_json',
    624                                                     self._trait_from_json)
--> 625                     self.set_trait(name, from_json(sync_data[name], self))
    626 
    627     def send(self, content, buffers=None):

[/usr/local/lib/python3.7/dist-packages/traitlets/traitlets.py](https://localhost:8080/#) in set_trait(self, name, value)
   1436                                 (cls.__name__, name))
   1437         else:
-> 1438             getattr(cls, name).set(self, value)
   1439 
   1440     @classmethod

[/usr/local/lib/python3.7/dist-packages/traitlets/traitlets.py](https://localhost:8080/#) in set(self, obj, value)
    578 
    579     def set(self, obj, value):
--> 580         new_value = self._validate(obj, value)
    581         try:
    582             old_value = obj._trait_values[self.name]

[/usr/local/lib/python3.7/dist-packages/traitlets/traitlets.py](https://localhost:8080/#) in _validate(self, obj, value)
    610             return value
    611         if hasattr(self, 'validate'):
--> 612             value = self.validate(obj, value)
    613         if obj._cross_validation_lock is False:
    614             value = self._cross_validate(obj, value)

[/usr/local/lib/python3.7/dist-packages/traitlets/traitlets.py](https://localhost:8080/#) in validate(self, obj, value)
   1985                 except TraitError:
   1986                     continue
-> 1987         self.error(obj, value)
   1988 
   1989     def __or__(self, other):

[/usr/local/lib/python3.7/dist-packages/traitlets/traitlets.py](https://localhost:8080/#) in error(self, obj, value, error, info)
    690                     e = "The '%s' trait expected %s, not %s." % (
    691                         self.name, self.info(), describe("the", value))
--> 692                 raise TraitError(e)
    693 
    694     def get_metadata(self, key, default=None):

TraitError: The 'target' trait of a DirectionalLight instance expected an Uninitialized or an Object3D, not the str 'IPY_MODEL_[object Object]'.

I suspect that some of pythreejs dependencies already pre-installed on Colab are quite old, and by running pip install --upgrade on the right dependency the error would disappear. Unfortunately, I can't pinpoint which dependency I should upgrade. Can you help me with that?

The only dependency I am confident one must not upgrade is ipywidgets: it seems that doing so would make Cell 2 ineffective, and Cell 2 is fundamental for the widget in Cell 4 to correctly show up.

The same notebook, and much more complicated ones, run correctly on my local jupyter lab installation.

cmaurini commented 1 year ago

Hi, as @francesco-ballarin, I have issue when trying to use pythreejs in colab as pyvista backend. The issue is probably related to the one reported above. Do you have any update?

hakonanes commented 1 year ago

I can reproduce the above error on my local JupyterLab installation on Ubuntu 22.04, no connection to Google Colab. I initially encountered this error using pythreejs via PyVista.

Versions (not sure which ones are most important):

francesco-ballarin commented 1 year ago

Thanks @hakonanes , so it seems that the issue may affect jupyterlab installations as well. After 3 months, it would be nice to have a comment or suggestion from pythreejs develoeprs.

zou000 commented 1 year ago

The problem is not specific to DirectionalLight, SpotLight has the same problem.

kazumamatata commented 1 year ago

I also got this error on my local macOS, after upgrading my packages.

Now, I could find a temporary fix. When downgrading traitlets from latest 5.8.0 to 4.3.3, the error disappeared.

For that I wrote the environment.yml file and created a conda environment:

conda env create --file environment.yml

environment.yml:

name: pjs
channels:
- conda-forge
dependencies:             
- python=3.9
- jupyterlab
- pythreejs
- pyvista
- traitlets=4.3.3

By removing the last line, the above mentioned error appears again.

P.S. Strangely pythreejs worked with traitlets v5.7.1 before, so I am wondering why this problem occurs

francesco-ballarin commented 1 year ago

Thanks @kazumamatata for the report and the workaround on macOS.

In terms of the original bug report (on Colab), I want to point out for pythreejs developers that

  1. neither v5.7.1 nor v5.8 of traitlets were released at the time of the original bug report (the latest released version in September 2022 was v5.4.0)
  2. upgrading/downgrading traitlets does not seem feasible on Colab, since it requires to force a kernel restart (due to being imported on startup, I guess)
vidartf commented 1 year ago

I did a bit of troubleshooting on this, and noted the following:

vidartf commented 1 year ago

It might also be an issue in the new comm package (cc @martinRenou ).

vidartf commented 1 year ago

Ah, I had ipykernel 6.19.4, which has been pulled from PyPI, but is still up on conda-forge... Downgrading to ipykernel 6.16.2 seems to have fixed the issue for me locally. I'm guessing that downgrading to traitlets 4 indirectly fixed it by constraining which version of ipykernel that you could have, since it currently depends on traitlets>=5.4.0. If someone can repro this error with ipykernel<=6.16.2 , please add some detailed repro steps, and I can look again.

francesco-ballarin commented 1 year ago

If someone can repro this error with ipykernel<=6.16.2

Colab is quite a lot behind current releases, and therefore definitely satisfies that condition. See https://github.com/googlecolab/backend-info/blob/2cec3e7ca1ae121133d9f8ee350e2aebf4518293/pip-freeze.txt#L163

vidartf commented 1 year ago

To troubleshoot colab issues, I suggest you reach out to colab :)

francesco-ballarin commented 1 year ago

Done that, https://github.com/googlecolab/colabtools/issues/3159 ;)

francesco-ballarin commented 9 months ago

My main motivation was using pythreejs within pyvista. Since the pythreejs backend was removed from pyvista in https://github.com/pyvista/pyvista/pull/4687, I don't have any further interest in this, hence I am closing this. Feel free to reopen if someone else is interested in this for pythreejs on its own, rather than within pyvista.