Skip to content

Commit 70fde68

Browse files
authored
Jupyter Widgets (#24)
* Jupyter Widgets * lint * fixes * consolidate * refactor * ported widget table
1 parent 4cca90b commit 70fde68

27 files changed

Lines changed: 3334 additions & 55 deletions

README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,75 @@ Limits of automatic cleanup:
105105
- It will not automatically remove all event listeners or timers.
106106
- It cannot safely revert all stateful third-party module internals.
107107

108+
## Jupyter Widgets
109+
110+
The kernel provides built-in support for [Jupyter Widgets](https://ipywidgets.readthedocs.io/) (`ipywidgets`-compatible). Widget classes and helpers are available under `Jupyter.widgets`; destructure the ones you need before using them:
111+
112+
```javascript
113+
const { IntSlider, IntProgress, jslink } = Jupyter.widgets;
114+
115+
const slider = new IntSlider({
116+
value: 50,
117+
min: 0,
118+
max: 100,
119+
description: 'My Slider'
120+
});
121+
const progress = new IntProgress({
122+
value: 50,
123+
min: 0,
124+
max: 100,
125+
description: 'Mirror'
126+
});
127+
display(slider);
128+
display(progress);
129+
130+
slider.observe(({ new: value }) => {
131+
console.log('Slider value:', value);
132+
}, 'value');
133+
134+
jslink([slider, 'value'], [progress, 'value']);
135+
```
136+
137+
Widgets auto-display when they are the last expression in a cell. Use the global `display()` function to display a widget explicitly, for example when assigning to a variable.
138+
139+
### Available widgets
140+
141+
- **Numeric**: `IntSlider`, `FloatSlider`, `FloatLogSlider`, `IntRangeSlider`, `FloatRangeSlider`, `Play`, `IntProgress`, `FloatProgress`, `IntText`, `FloatText`, `BoundedIntText`, `BoundedFloatText`
142+
- **Boolean**: `Checkbox`, `ToggleButton`, `Valid`
143+
- **Selection**: `Dropdown`, `RadioButtons`, `Select`, `SelectMultiple`, `ToggleButtons`, `SelectionSlider`, `SelectionRangeSlider`
144+
- **String**: `Text`, `Textarea`, `Password`, `Combobox`
145+
- **Display**: `Label`, `HTML`, `HTMLMath`, `Output`
146+
- **Button**: `Button` (with `.onClick()` handler)
147+
- **Color**: `ColorPicker`
148+
- **Layout / Style**: `Layout`, `DescriptionStyle`, `SliderStyle`, `ProgressStyle`, `ButtonStyle`, `CheckboxStyle`, `ToggleButtonStyle`, `ToggleButtonsStyle`, `TextStyle`, `HTMLStyle`, `HTMLMathStyle`, `LabelStyle`
149+
- **Containers**: `Box`, `HBox`, `VBox`, `GridBox`, `Accordion`, `Tab`, `Stack`
150+
- **Helpers**: `jslink`, `jsdlink`
151+
152+
### Ported widget modules
153+
154+
The widget runtime is split into files that roughly follow the upstream `ipywidgets` package structure so it is easier to track what has been ported.
155+
156+
| Upstream `ipywidgets` file | Local file | Status | Notes |
157+
| ---------------------------------------------------- | --------------------------------------------------------------------- | ------- | -------------------------------------------------------------------- |
158+
| `packages/base/src/widget.ts` | `packages/javascript-kernel/src/widgets/widget.ts` | Ported | Kernel-side `Widget` and `DOMWidget` equivalents |
159+
| `packages/base/src/widget_layout.ts` | `packages/javascript-kernel/src/widgets/widget_layout.ts` | Ported | Layout models |
160+
| `packages/base/src/widget_style.ts` | `packages/javascript-kernel/src/widgets/widget_style.ts` | Ported | Shared style models, plus control-specific styles gathered here |
161+
| `packages/controls/src/widget_int.ts` | `packages/javascript-kernel/src/widgets/widget_int.ts` | Ported | Integer widgets, play, progress, and text inputs |
162+
| `packages/controls/src/widget_float.ts` | `packages/javascript-kernel/src/widgets/widget_float.ts` | Ported | Float widgets |
163+
| `packages/controls/src/widget_bool.ts` | `packages/javascript-kernel/src/widgets/widget_bool.ts` | Ported | Boolean widgets; related styles live in `widget_style.ts` |
164+
| `packages/controls/src/widget_selection.ts` | `packages/javascript-kernel/src/widgets/widget_selection.ts` | Partial | Selection semantics still differ from `ipywidgets` in some cases |
165+
| `packages/controls/src/widget_string.ts` | `packages/javascript-kernel/src/widgets/widget_string.ts` | Ported | String and display widgets; related styles live in `widget_style.ts` |
166+
| `packages/output/src/output.ts` | `packages/javascript-kernel/src/widgets/widget_output.ts` | Partial | Output capture is supported but not feature-complete |
167+
| `packages/controls/src/widget_button.ts` | `packages/javascript-kernel/src/widgets/widget_button.ts` | Partial | Button widget is present, but callback behavior differs slightly |
168+
| `packages/controls/src/widget_color.ts` | `packages/javascript-kernel/src/widgets/widget_color.ts` | Ported | Color picker |
169+
| `packages/controls/src/widget_box.ts` | `packages/javascript-kernel/src/widgets/widget_box.ts` | Ported | Box, HBox, VBox, GridBox |
170+
| `packages/controls/src/widget_selectioncontainer.ts` | `packages/javascript-kernel/src/widgets/widget_selectioncontainer.ts` | Ported | Accordion, Tab, Stack |
171+
| `packages/controls/src/widget_link.ts` | `packages/javascript-kernel/src/widgets/widget_link.ts` | Ported | `jslink`, `jsdlink`, `Link`, and `DirectionalLink` |
172+
173+
> **Note:** `jupyterlab-widgets`, `@jupyter-widgets/controls`, and `@jupyter-widgets/output` must be available in the JupyterLite deployment for the full widget set to render.
174+
175+
See the [example notebook](examples/widgets.ipynb) for more usage examples.
176+
108177
### Enable or disable specific modes
109178

110179
The two runtime modes are registered by separate plugins:

0 commit comments

Comments
 (0)