jupyter-widgets / pythreejs

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

JSON serialization of OrbitControls fails with jupyter_client 7.0.3 #366

Closed boehmc closed 2 years ago

boehmc commented 3 years ago

I believe a recent change in the json serialization within jupyter_client leads to the following error when trying to use OrbitControls in a jupyter notebook.

ValueError: Out of range float values are not JSON compliant

The issue is caused by default values of +/- Inf for some parameters in OrbitControls

This can also be seen in the second cell of the built-in example.

Here is a minimal (non)-working example:

import pythreejs
camera = pythreejs.PerspectiveCamera(
    position=[0.0, 0.0, 6.0],
    fov=20,
    aspect=1.35,
    near=0.001
)
controls = pythreejs.OrbitControls(
    controlling=camera, 
#     minAzimuthAngle=-1.0,
#     maxAzimuthAngle=1.0,
#     maxDistance=1.0,
#     maxZoom=1
)

Removing the commented lines will insert finite values for all parameters that would default to +/- Inf otherwise, after which the serialization works again.

System info:

python --version
Python 3.7.10

python -c "import pythreejs; print(pythreejs.version_info)"
(2, 3, 0, 'final')

 jupyter --version
Selected Jupyter core packages...
IPython          : 7.27.0
ipykernel        : 6.4.0
ipywidgets       : 7.6.5
jupyter_client   : 7.0.3
jupyter_core     : 4.8.1
jupyter_server   : not installed
jupyterlab       : not installed
nbclient         : 0.5.4
nbconvert        : 6.1.0
nbformat         : 5.1.3
notebook         : 6.4.4
qtconsole        : 5.1.1
traitlets        : 5.1.0
martinRenou commented 3 years ago

Thanks for reporting. I believe this should be fixed in jupyter_client. I'll see what I can do.

martinRenou commented 3 years ago

Actually I believe this should be fixed here in pythreejs. Python's math.inf is now properly serialized to Infinity (valid JSON) instead of 'inf'. So we can remove all the workarounds:

boehmc commented 3 years ago

Awesome! Thanks so much for your swift reply and for looking into it!

Let me know if I can help and work on a pull request.

martinRenou commented 3 years ago

is now properly serialized to Infinity (valid JSON)

I was wrong. Infinity and NaN are not part of the JSON specification, even though it's valid JavaScript and JavaScript decoders supports it.

The issue comes from https://github.com/ipython/ipykernel/pull/708 specifically, as Infinity and NaN are not being "cleaned" anymore.

For now I can only suggest either downgrading ipykernel to <6.4 or jupyter_client to <7.

I am trying to push a fix to jupyter_client directly ASAP

boehmc commented 3 years ago

Awesome, thanks a lot!

martinRenou commented 3 years ago

This will be fixed by https://github.com/jupyter/jupyter_client/pull/708 (at least, we will only see a Warning but the widget will work again)

Though we will need to update pythreejs at some point, having a custom serializer to support Infinity/NaN, as it's not valid JSON, similar to what ipydatagrid does: https://github.com/bloomberg/ipydatagrid/blob/main/ipydatagrid/datagrid.py#L172-L178

vidartf commented 2 years ago

Yeah, we should make some serializers for "extended floats"..

bastianlb commented 2 years ago

any update on this? I'm having to downgrade my jupyter_client version in the meantime

vidartf commented 2 years ago

Fix proposed in #378 (this commit: https://github.com/jupyter-widgets/pythreejs/pull/378/commits/f294530d7b7d5e6e7469360f34fd0770a7485a31)

LtAirman commented 2 years ago

Hello vidartf,

Please pardon me for not following proper protocol. I just posted the following message to you at https://github.com/jupyter-widgets/pythreejs/pull/378

I’m a newbie programmer working with Jupyter Notebook and pythreejs.

All 3 of my project files at https://github.com/LtAirman/ChargeFieldTopics stopped working this morning. They no longer display the pythreejs rendered scene.

Over the last few months the cell setting up the rendered scene displayed a UserWarning, due to non JSON compliant infinite values. I believe you identified the problem, involving the pythreejs orbital camera, in issues 366 and 378 which I’ve been following.

It was my impression this non-finite number problem would be corrected with the upcoming ipywidget 8 release, seeing no rendered output at all is an awful surprise. What do I do now?

I’d appreciate any guidance or information you might provide in this matter.

LtAirman commented 2 years ago

Update, happy to report my projects' pythreejs rendered outputs are now working properly. Along with the UserWarning that is. I double posted this message at https://github.com/jupyter-widgets/pythreejs/pull/378.