Friday 11 December 2020

Can an ipywidget source its JavaScript from a URL, rather than local disk?

I'm building an ipywidget to be shared with some coworkers on a private repo. I'd like to have it source JavaScript from a server, rather than requiring my coworkers to build the JS bundle locally, and I'm wondering if this is possible.

Here's my setup. I've created a directory (test-extension) and enabled it for development with Jupyter:

jupyter nbextension install test-extension --sys-prefix --symlink
jupyter nbextension enable test-extension/main --sys-prefix

Following the Custom Widget Tutorial, I can create a custom widget with some Python:

from traitlets import Unicode, Bool, validate, TraitError
from ipywidgets import DOMWidget, register

@register
class Email(DOMWidget):
    _view_name = Unicode('EmailView').tag(sync=True)
    _view_module = Unicode('email_widget').tag(sync=True)
    _view_module_version = Unicode('0.1.0').tag(sync=True)

    value = Unicode('example@example.com', help="The email value.").tag(sync=True)

and some JavaScript, in test-extension/main.js:

define('email_widget', ["@jupyter-widgets/base"], function(widgets) {
    var EmailView = widgets.DOMWidgetView.extend({

        // Render the view.
        render: function() {
            this.email_input = document.createElement('input');
            this.email_input.type = 'email';
            this.email_input.value = this.model.get('value');
            this.el.appendChild(this.email_input);
        },
    });

    return {
        EmailView: EmailView
    };
});

In my real application, this main.js file is generated by webpack and is 10-20MB. I'd rather not require my coworkers to rebuild the widget JS whenever anything changes. But since it's so large, I'd prefer not to commit it to GitHub. It's for a private repo, so I don't want to publish it on PyPI. One idea is to host the JS on an internal server and have the widget source it from there.

Is this possible with ipywidgets? Can a widget source the JS for its _view_module from a URL, rather than from local disk? ipywidgets uses RequireJS as a module loader, so a solution involving that would work, too. Or is there a better solution that I've overlooked?



from Can an ipywidget source its JavaScript from a URL, rather than local disk?

No comments:

Post a Comment