GitHub - jupyter-widgets/widget-cookiecutter: A cookiecutter template for creating a custom Jupyter widget project. (original) (raw)

widget-cookiecutter (deprecated)

This cookitcutter template is now deprecated

We recommend anywidget as an alternative for those who want to learn or quickly prototype widgets. For production uses, we recommend the TypeScript cookiecutter

A cookiecutter template for creating a custom Jupyter widget project

Documentation Status Join the chat at https://gitter.im/jupyter-widgets/Lobby

A cookiecutter template for a custom Jupyter widget project.

What is widget-cookiecutter?

With widget-cookiecutter you can create a custom Jupyter interactive widget project with sensible defaults. widget-cookiecutter helps custom widget authors get started with best practices for the packaging and distribution of a custom Jupyter interactive widget library.

Who uses widget-cookiecutter

Popular widget libraries such asbqplot,pythreejs andipyleafletcan serve as advanced examples of usage of the Jupyter widget infrastructure.

Usage

Install cookiecutter:

$ pip install cookiecutter

After installing cookiecutter, use the widget-cookiecutter:

$ cookiecutter https://github.com/jupyter/widget-cookiecutter.git

As widget-cookiecutter runs, you will be asked for basic information about your custom Jupyter widget project. You will be prompted for the following information:

After this, you will have a directory containing files used for creating a custom Jupyter widget. You will now be able to easily package and distribute your custom Jupyter widget.

Files Generated by cookiecutter

Jupyter widgets are composed of two separate programs:

This cookiecutter generates example source files for both the backend and frontend programs, as well as the tooling to develop and publish the widget in the classic notebook, JupyterLab and in stand-alone HTML pages.

Running the cookiecutter generates the following file tree:

├── <python_package_name>
│   ├── __init__.py
│   ├── _version.py
│   └── example.py
├── js
│   ├── lib
│   │   ├── embed.js
│   │   ├── example.js
│   │   ├── extension.js
│   │   ├── index.js
│   │   └── labplugin.js
│   ├── package.json
│   ├── README.md
│   └── webpack.config.js
├── .gitignore
├── <npm_package_name>.json
├── MANIFEST.in
├── README.md
├── RELEASE.md
├── setup.cfg
└── setup.py
1. one that is suitable for loading the widget frontend in the classic notebook  
2. one that is suitable for loading the widget frontend in standalone HTML webpages with no backend  
3. one that is suitable for loading the widget frontend in JupyterLab  

Fortunately, you don't have to write three different versions of your widgets. There are three entrypoints that all load the widget source files. Each of the frontend program gets bundled using webpack:

1. The classic notebook frontend bundle gets created using `index.js` as the entrypoint.  
2. The standalone frontend bundle gets created using `embed.js` as the entrypoint.  
3. For JupyterLab, the frontend code gets folded into the JupyterLab frontend itself using JupyterLab's webpack. JupyterLab will include `labplugin.js` as the entrypoint to the widgets.  
- **`lib directory`**\  

Directory containing source files
Webpack entry-point for generating the code bundle that is loaded when widgets are used in a stand-alone HTML page. Learn more about embedding widgets into a stand-alone HTML page here. When you add new source files to your project that declare widgets, you will need to export them here.
Widget frontend source code. Contains widget model and view class definitions. Widget model is derived from DOMWidgetModel which provides functionality to communicate with the model instance on kernel. Widget view is derived from DOMWidgetView which has a model attribute referencing to widget model it is presenting.
Entry-point that is loaded by the classic notebook to load your widget. You should not need to modify this. Note that this does not get included in the webpack bundle.
This is the entrypoint to your library. When you add new widgets, or change widget names, add these here. When bundling for the classic notebook, webpack will create a single bundle based on this file. This bundle is then loaded by extension.js. For JupyterLab, labplugin.js loads this file.
JupyterLab extension definition for JupyterLab integration. When JupyterLab loads, it activates the plugin defined in this file. Widget registry service provided by @jupyter-widgets/jupyterlab-manager is used during activation to register the widget with JupyterLab. You should not need to modify this file.
npm package definiton file for widget frontend. You should not need to modify this file, apart from to add new JavaScript dependencies.
Auto-generated documentation file for frontend npm package, with installation instructions.

- **`webpack.config.js`**\  

Webpack bundler configuration that defines bundler entry points and ouputs along with other configuration

Local Dev Installation for Classic Notebook

To develop this package against the classic notebook, run:

Now make some changes to your source code. Then:

Local Dev Installation for JupyterLab

To develop this package against JupyterLab, run:

Now make some changes to your source code. Then:

Publishing Widget

Follow the steps in (RELEASE.md) to publish widget python package and npm packages.

Widget Installation Process for Notebook

When installing published package in non-development mode

Widget Load Process for Notebook

More information