jupyter-widgets / ipywidgets

Interactive Widgets for the Jupyter Notebook

Home Page:https://ipywidgets.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Custom widget example does not work in ipywidget 8.0.4

YZhao33 opened this issue · comments

The custom widget example, MyWidget, illustrated in https://ipywidgets.readthedocs.io/en/8.1.2/examples/Widget%20Low%20Level.html
does not work in ipywidget V8.0.4. It works at least with V7.6.5. I have another widget following this example and met the same issue. How to address this?

Setup:
Install different Anaconda versions in Windows

python code

from ipywidgets import DOMWidget
from traitlets import Unicode, Int

class MyWidget(DOMWidget):
    _view_module = Unicode('mywidget').tag(sync=True)
    _view_module_version = Unicode('0.1.0').tag(sync=True)
    _view_name = Unicode('MyWidgetView').tag(sync=True)
    count = Int().tag(sync=True)

javascript code

%%javascript
define('mywidget', ['@jupyter-widgets/base'], function(widgets) {
    var MyWidgetView = widgets.DOMWidgetView.extend({
        render: function() {
            MyWidgetView.__super__.render.apply(this, arguments);
            this._count_changed();
            this.listenTo(this.model, 'change:count', this._count_changed, this);
        },

        _count_changed: function() {
            var old_value = this.model.previous('count');
            var new_value = this.model.get('count');
            this.el.textContent = String(old_value) + ' -> ' + String(new_value);
        }
    });

    return {
        MyWidgetView: MyWidgetView
    }
});

code to test MyWidget

mywidget = MyWidget()
display(mywidget)

On V7.6, it works as expected
image

On V8.0.4, it fails with the following error

Open Browser Console for more detailed log - Double click to close this message]
Failed to create view for 'MyWidgetView' from module 'mywidget' with model 'DOMWidgetModel' from module '@jupyter-widgets/base'
TypeError: Class constructor O cannot be invoked without 'new'
at new i (http://localhost:8888/nbextensions/jupyter-js-widgets/extension.js?v=20231027001625:2:257463)
at http://localhost:8888/nbextensions/jupyter-js-widgets/extension.js?v=20231027001625:2:742192

Thanks