Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON serialization of OrbitControls fails with jupyter_client 7.0.3 #366

Closed
boehmc opened this issue Sep 22, 2021 · 11 comments · Fixed by jupyter/jupyter_client#708 or #378
Closed

Comments

@boehmc
Copy link

boehmc commented Sep 22, 2021

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
Copy link
Member

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

@martinRenou
Copy link
Member

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:

  • if (value === Infinity) { return "float('inf')"; }
    if (value === -Infinity) { return "-float('inf')"; }
  • convertFloatModelToThree: function(v) {
    if (typeof v === 'string' || v instanceof String) {
    v = v.toLowerCase();
    if (v === 'inf') {
    return Infinity;
    } else if (v === '-inf') {
    return -Infinity;
    } else if (v === 'nan') {
    return NaN;
    }
    }
    return v;
    },
    convertFloatThreeToModel: function(v) {
    if (isFinite(v)) { // Most common first
    return v;
    } else if (isNaN(v)) {
    return 'nan';
    } else if (v === Infinity) {
    return 'inf';
    } else if (v === -Infinity) {
    return '-inf';
    }
    return v;
    },

@boehmc
Copy link
Author

boehmc commented Sep 22, 2021

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
Copy link
Member

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 ipython/ipykernel#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
Copy link
Author

boehmc commented Sep 29, 2021

Awesome, thanks a lot!

@martinRenou
Copy link
Member

martinRenou commented Sep 30, 2021

This will be fixed by jupyter/jupyter_client#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
Copy link
Member

vidartf commented Oct 4, 2021

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

@bastianlb
Copy link

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

@vidartf vidartf mentioned this issue May 26, 2022
4 tasks
@vidartf
Copy link
Member

vidartf commented May 26, 2022

Fix proposed in #378 (this commit: f294530)

@LtAirman
Copy link

LtAirman commented Jul 2, 2022

Hello vidartf,

Please pardon me for not following proper protocol. I just posted the following message to you at #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
Copy link

LtAirman commented Jul 3, 2022

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 #378.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants