Optional dependency for JupyterLab extension


I’m developing an extension that adds new functionality through toolbar buttons and widgets.

In an earlier release, I made ipywidgets a dependency because of the widgets. Now, I want to make ipywidgets an optional dependency, so I removed it from the pyproject.toml.

When testing the package, I realized that removing ipywidgets broke it, and upon debugging, I noticed that the error came because the extension could not find the @jupyter-widgets/base JS package. After some debugging, I noticed that my package.json has a jupyterlab section and a sharedPackages subsection (I created this project from the template).

It seems like since I no longer have ipywidgets as a dependency, @jupyter-widgets/base is never installed and the package breaks, because it’s configured to share the @jupyter-widgets/base package.

What is the best way to configure my extension so the widgets feature is optional?

For now, I eliminated the sharedPackages section. This fixed the problem, but I’m unsure if this could create other issues and there’s a better way do achieve this.

1 Like

This might be a good idea to add optional field in sharedPackages objects. I would suggest opening an issue on Issues · jupyterlab/jupyterlab · GitHub to discuss.

One workaround would be making two extensions (two package.json) and bundling them in a single PyPI package. This way one can request sharedPackages and the other does not need to.

thanks @krassowski! I opened an issue: Add `optional` field to `sharedPackages` in `package.json` -> `jupyterlab` · Issue #15151 · jupyterlab/jupyterlab · GitHub

1 Like