This example implements the ipywidgets widget protocol version 2, as well as a few of its widgets. It also comes with a little toy language to try out the widgets. It serves as an example how to setup the widgets and communicate to the frontend.
Almost all the models defined in ipywidgets. These models define the messages sent between frontend and backend.
Currently only the following widgets are supported:
- FloatSliderModel
- IntSliderModel
- ButtonModel
- CheckboxModel
You can easily extend the set by registering them in the WidgetFactory
defined in the app.d
This example also contains a small repl to construct the widgets, and comes with autocomplete support. See the Example.ipynb
for more information.
-
Changes to a widgets state must be synchronized manually by calling
onRequestState()
on the widget. -
The toy language only supports int, doubles and strings.
-
The example has not implemented support for binary data.
You can read up on the version 2 of the ipywidgets widget messaging protocol here.
Briefly, it works as follows:
- upon creation of a Widget in the backend, it sends a
commOpenMessage
(for each model), with acomm_id
referencing this widget - to send updates to the frontend, it sends the
commMessage
with the modified state - to display the widget, it send the
displayDataMessage
- the frontend in turn sends
commMessage
's for each change
Most likely you will have your own interpreter in your backend, but you can reuse much of the following code:
models.d
is a freestanding file and defines most of the ipywidgets models.widgets.d
depends onmodels.d
and implements the state synchronisation with the frontend.app.d
contains thecommOpen
/commClose
/commMessage
functions that you will need to implement as well.
There are plenty of open source widgets available such as bqplot, pythreejs and ipyleaflet
If you already have the ipywidgets in your backend, then you will only need to implement the models that correspond with the models of those widgets.
With D's support for WebAssembly it is possible to write a custom widget completely in D. See the skoppe/spasm-jupyter-widget-example for an example.
To install locally, copy kernel.json
from this directory to
~/.local/share/jupyter/kernels/<name>/kernel.json
(linux) or
~/Library/Jupyter/kernels/<name>/kernel.json
(mac). The directory
name can be anything. The widgets_example_kernel
executable build by dub
will have to be in the PATH
, otherwise edit kernel.json
to point
to the path where it is located.
To install globally, use /usr/share/jupyter/kernels
(linux and mac) instead of
~/.local/share/jupyter/kernels
.
And that's it. When you run jupyter notebook
you'll see jupyter-wire-widgets-example
as one of the available kernels. The example here contains a toy programming language with widgets.
See the Example.ipynb
notebook.
Make sure to install ipywidgets 7.0 first.