jupyter-widgets / tutorial

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

`ipycanvas` example error in more-widget-libraries folder #195

Closed ibdafna closed 2 years ago

ibdafna commented 2 years ago

I'm seeing the following error when running the custom scatter plot and game of life examples in the ipycanvas notebook. The Perlin noise and rough canvas examples do not throw an error but no output is displayed.

image

This is with the latest conda environment spec.

Full trace:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [6], in <cell line: 121>()
    118 sizes = np.random.randint(2, 8, n_points)
    119 colors = np.random.rand(n_points) * 10 - 2
--> 121 plot = ScatterPlot(x, y, sizes, colors, branca.colormap.linear.viridis, stroke_color='white')
    122 plot

Input In [6], in ScatterPlot.__init__(self, x, y, size, color, scheme, stroke_color)
     16 self.stroke_color = stroke_color
     18 self.n_marks = min(x.shape[0], y.shape[0], size.shape[0], color.shape[0])
---> 20 self.draw()

Input In [6], in ScatterPlot.draw(self)
    108     plot_layer.fill_circle(mark_x, mark_y, mark_size)
    109     plot_layer.stroke_circle(mark_x, mark_y, mark_size)
--> 111 plot_layer.restore()

File ~/micromamba/envs/widgets-tutorial-2022/lib/python3.9/contextlib.py:126, in _GeneratorContextManager.__exit__(self, typ, value, traceback)
    124 if typ is None:
    125     try:
--> 126         next(self.gen)
    127     except StopIteration:
    128         return False

File ~/micromamba/envs/widgets-tutorial-2022/lib/python3.9/site-packages/ipycanvas/canvas.py:1792, in hold_canvas(canvas)
   1790 _CANVAS_MANAGER._caching = True
   1791 yield
-> 1792 _CANVAS_MANAGER.flush()
   1794 if not orig_caching:
   1795     _CANVAS_MANAGER._caching = False

File ~/micromamba/envs/widgets-tutorial-2022/lib/python3.9/site-packages/ipycanvas/canvas.py:250, in _CanvasManager.flush(self)
    247 if not self._caching or not len(self._commands_cache):
    248     return
--> 250 self._send_custom(self._commands_cache, self._buffers_cache)
    252 self._commands_cache = []
    253 self._buffers_cache = []

File ~/micromamba/envs/widgets-tutorial-2022/lib/python3.9/site-packages/ipycanvas/canvas.py:256, in _CanvasManager._send_custom(self, command, buffers)
    255 def _send_custom(self, command, buffers=[]):
--> 256     metadata, command_buffer = commands_to_buffer(command)
    257     self.send(metadata, buffers=[command_buffer] + buffers)

File ~/micromamba/envs/widgets-tutorial-2022/lib/python3.9/site-packages/ipycanvas/utils.py:74, in commands_to_buffer(commands)
     66     return array_to_binary(
     67         np.frombuffer(
     68             bytes(orjson.dumps(commands, option=orjson.OPT_SERIALIZE_NUMPY)),
     69             dtype=np.uint8,
     70         )
     71     )
     72 else:
     73     return array_to_binary(
---> 74         np.frombuffer(bytes(json.dumps(commands), encoding="utf8"), dtype=np.uint8)
     75     )

File ~/micromamba/envs/widgets-tutorial-2022/lib/python3.9/json/__init__.py:231, in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw)
    226 # cached encoder
    227 if (not skipkeys and ensure_ascii and
    228     check_circular and allow_nan and
    229     cls is None and indent is None and separators is None and
    230     default is None and not sort_keys and not kw):
--> 231     return _default_encoder.encode(obj)
    232 if cls is None:
    233     cls = JSONEncoder

File ~/micromamba/envs/widgets-tutorial-2022/lib/python3.9/json/encoder.py:199, in JSONEncoder.encode(self, o)
    195         return encode_basestring(o)
    196 # This doesn't pass the iterator directly to ''.join() because the
    197 # exceptions aren't as detailed.  The list call should be roughly
    198 # equivalent to the PySequence_Fast that ''.join() would do.
--> 199 chunks = self.iterencode(o, _one_shot=True)
    200 if not isinstance(chunks, (list, tuple)):
    201     chunks = list(chunks)

File ~/micromamba/envs/widgets-tutorial-2022/lib/python3.9/json/encoder.py:257, in JSONEncoder.iterencode(self, o, _one_shot)
    252 else:
    253     _iterencode = _make_iterencode(
    254         markers, self.default, _encoder, self.indent, floatstr,
    255         self.key_separator, self.item_separator, self.sort_keys,
    256         self.skipkeys, _one_shot)
--> 257 return _iterencode(o, 0)

File ~/micromamba/envs/widgets-tutorial-2022/lib/python3.9/json/encoder.py:179, in JSONEncoder.default(self, o)
    160 def default(self, o):
    161     """Implement this method in a subclass such that it returns
    162     a serializable object for ``o``, or calls the base implementation
    163     (to raise a ``TypeError``).
   (...)
    177 
    178     """
--> 179     raise TypeError(f'Object of type {o.__class__.__name__} '
    180                     f'is not JSON serializable')

TypeError: Object of type int64 is not JSON serializable

Attn @marimeireles cc @mwcraig

marimeireles commented 2 years ago

Hey @ibdafna this was already fixed by another issue thank you! :)