jupyter-widgets / tutorial

A tutorial for widgets
BSD 3-Clause "New" or "Revised" License
394 stars 164 forks source link

Getting np array is not serializable error in several cells of ipycanvas #185

Closed marimeireles closed 2 years ago

marimeireles commented 2 years ago

I'm getting

TypeError: Object of type int64 is not JSON serializable

In line

plot = ScatterPlot(x, y, sizes, colors, branca.colormap.linear.viridis, stroke_color='white')

From the ipycanvas example.

I've tried converting all the np arrays to a python type using:

import numpy as np

colors = np.random.rand(n_points) * 10 - 2
colors = colors.astype(np.float32)

But getting the exact same error. Really no clue what's going on here.

Doesn't seem like it's a regression from branca the plotting tool, as I've tested with earlier versions. Is this a known issue with a new ipywidgets version?

mwcraig commented 2 years ago

Odd -- I see this too, but I didn't a few weeks ago (at least I don't think I did).

mwcraig commented 2 years ago

@martinRenou any suggestions?

FYI, I think all of the failures are due to the Custom scatter p[lot cell -- if I skip that then everything subsequent to that works.

Testing with ipywidgets 7.7 and ipycanvas 0.12.0.

ianhi commented 2 years ago

This may be a bug in ipycanvas. But looks like there's an easy workaround - that plot works for me after pip install orjson.

The function breaking has two branches https://github.com/martinRenou/ipycanvas/blob/7c6fc1f63205566e287ebb9552a2b2be0b7b05cc/ipycanvas/utils.py#L63-L75 and I guess orjson is more robust.

marimeireles commented 2 years ago

Will test later tonight. Thank you guys! :D

mwcraig commented 2 years ago

Please ping me if it works -- I'll add orjson to the install instructions if it works.

marimeireles commented 2 years ago

Thanks @ianhi! That did the trick @mwcraig if you can, please add it to the instructions =) And we can close this one.