DirectionalLight on Google Colab
francesco-ballarin opened this issue ยท 13 comments
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.
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?
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):
- python 3.10.8
- pythreejs 2.4.1
- ipywidgets 7.7.0 (not newer to avoid pyvista/pyvista#3274)
- jupyterlab 3.5.1
- traitlets 5.7.0
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.
The problem is not specific to DirectionalLight
, SpotLight
has the same problem.
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
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
- 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) - 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)
I did a bit of troubleshooting on this, and noted the following:
- This happens also if I simply install
pythreejs
with recent version of everything. No other packages / envs are important in order to repro the current error. - I install jupyterlab-kernelspy to inspect the comm messages, and found this error occurs when the default "target" for a
DirectionalLight
is created from the kernel side:Exception opening comm with target: jupyter.widget Traceback (most recent call last): File "<env root>lib\site-packages\comm\base_comm.py", line 237, in comm_open f(comm, msg) File "<env root>lib\site-packages\ipywidgets\widgets\widget.py", line 427, in handle_comm_opened widget = widget_class(comm=comm) File "<env root>lib\site-packages\pythreejs\core\Object3D_autogen.py", line 26, in __init__ super(Object3D, self).__init__(**kwargs) File "<env root>lib\site-packages\pythreejs\_base\Three.py", line 17, in __init__ super(ThreeWidget, self).__init__(**kwargs) File "<env root>lib\site-packages\ipywidgets\widgets\widget.py", line 502, in __init__ super().__init__(**kwargs) File "<env root>lib\site-packages\traitlets\traitlets.py", line 1346, in __init__ setattr(self, key, value) File "<env root>lib\site-packages\traitlets\traitlets.py", line 732, in __set__ self.set(obj, value) File "<env root>lib\site-packages\traitlets\traitlets.py", line 706, in set new_value = self._validate(obj, value) File "<env root>lib\site-packages\traitlets\traitlets.py", line 738, in _validate value = self.validate(obj, value) File "<env root>lib\site-packages\traitlets\traitlets.py", line 2150, in validate self.error(obj, value) File "<env root>lib\site-packages\traitlets\traitlets.py", line 844, in error raise TraitError(e) traitlets.traitlets.TraitError: The 'comm' trait of an Object3D instance expected a Comm or None, not the BaseComm at '0x13ea2217df0'.
- The creation of the
DirectionalLight
fails after that, since it is referencing a widget that failed to create. - I note that the error during the client side initiated
comm_open
message is not shown in the log viewer in lab. This should ideally be added in Lab. - The comm issue is likely to be an ipywidgets bug (cc @jasongrout for visibility)
It might also be an issue in the new comm
package (cc @martinRenou ).
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.
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
To troubleshoot colab issues, I suggest you reach out to colab :)
Done that, googlecolab/colabtools#3159 ;)
My main motivation was using pythreejs
within pyvista
. Since the pythreejs
backend was removed from pyvista
in pyvista/pyvista#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
.