Skip to content

Commit e357ffd

Browse files
committed
Fix example content
1 parent 25b0cbc commit e357ffd

4 files changed

Lines changed: 197 additions & 67 deletions

File tree

examples/simple-yjs-widget/notebooks/simple.ipynb

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,27 @@
77
"metadata": {},
88
"outputs": [],
99
"source": [
10-
"from ypywidgets import Widget, reactive"
10+
"from ypywidgets.comm import CommWidget\n",
11+
"from pycrdt import Text"
1112
]
1213
},
1314
{
1415
"cell_type": "code",
1516
"execution_count": null,
16-
"id": "18d1ca71-1943-4902-b642-06e3d812e23f",
17+
"id": "6d9538c9-4858-4c9d-b347-18965660115d",
1718
"metadata": {},
1819
"outputs": [],
1920
"source": [
20-
"class MyWidget(Widget):\n",
21-
" foo = reactive(\"\")\n",
22-
" bar = reactive(\"\")"
21+
"class MyWidget(CommWidget):\n",
22+
" def __init__(self):\n",
23+
" super().__init__(\n",
24+
" comm_metadata={\n",
25+
" \"ymodel_name\": \"MyWidget\",\n",
26+
" \"create_ydoc\": True,\n",
27+
" }\n",
28+
" )\n",
29+
"\n",
30+
" self.ydoc[\"foo\"] = self._foo = Text()"
2331
]
2432
},
2533
{
@@ -36,21 +44,11 @@
3644
{
3745
"cell_type": "code",
3846
"execution_count": null,
39-
"id": "72a7799e-ce21-43ed-865c-77fa1fc69eb2",
47+
"id": "9269a3f1-a061-478f-8728-7393a4aec2d0",
4048
"metadata": {},
4149
"outputs": [],
4250
"source": [
43-
"w.foo = 3"
44-
]
45-
},
46-
{
47-
"cell_type": "code",
48-
"execution_count": null,
49-
"id": "d7289dc9-29c1-4422-8962-1b013bfda40a",
50-
"metadata": {},
51-
"outputs": [],
52-
"source": [
53-
"w.bar = 4"
51+
"w.ydoc[\"foo\"].insert(0, 'coucou ici')"
5452
]
5553
}
5654
],
@@ -70,7 +68,7 @@
7068
"name": "python",
7169
"nbconvert_exporter": "python",
7270
"pygments_lexer": "ipython3",
73-
"version": "3.11.3"
71+
"version": "3.13.12"
7472
}
7573
},
7674
"nbformat": 4,

examples/simple-yjs-widget/package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@
8181
"jupyterlab": {
8282
"extension": true,
8383
"outputDir": "simple_yjs_widget/labextension",
84-
"webpackConfig": "./extension.webpack.config.js"
84+
"webpackConfig": "./extension.webpack.config.js",
85+
"sharedPackages": {
86+
"yjs-widgets": {
87+
"singleton": true,
88+
"bundled": false
89+
}
90+
}
8591
}
8692
}

examples/simple-yjs-widget/src/index.ts

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,41 @@
1-
import * as YjsWidgets from 'yjs-widgets';
1+
import {
2+
IJupyterYModel,
3+
JupyterYModel,
4+
IJupyterYWidgetManager
5+
} from 'yjs-widgets';
6+
7+
import * as Y from 'yjs';
28

39
import {
410
JupyterFrontEnd,
511
JupyterFrontEndPlugin
612
} from '@jupyterlab/application';
713

814
class MyWidget {
9-
constructor(yModel: YjsWidgets.IJupyterYModel, node: HTMLElement) {
15+
constructor(yModel: IJupyterYModel, node: HTMLElement) {
1016
this.yModel = yModel;
1117
this.node = node;
12-
this.yModel.sharedModel.setAttr('foo', '');
13-
this.yModel.sharedModel.setAttr('bar', '');
14-
yModel.sharedModel.attrsChanged.connect(() => {
15-
this._attrsChanged();
16-
});
17-
setInterval(() => {
18-
this._changeAttrs();
19-
}, 1000);
20-
this._changeAttrs();
21-
}
2218

23-
_changeAttrs(): void {
24-
let foo: string = this.yModel.sharedModel.getAttr('foo') as string;
25-
let bar: string = this.yModel.sharedModel.getAttr('bar') as string;
26-
foo = `#${foo}`;
27-
bar = `#${bar}`;
28-
this.yModel.sharedModel.setAttr('foo', foo);
29-
this.yModel.sharedModel.setAttr('bar', bar);
30-
this.node.innerHTML = `foo=${foo}<br>bar=${bar}`;
19+
this.foo = this.yModel.sharedModel.ydoc.getText('foo');
20+
21+
this.foo.observe(this._attrsChanged.bind(this));
3122
}
3223

3324
_attrsChanged(): void {
34-
const foo: string = this.yModel.sharedModel.getAttr('foo') as string;
35-
const bar: string = this.yModel.sharedModel.getAttr('bar') as string;
36-
this.node.innerHTML = `foo=${foo}<br>bar=${bar}`;
25+
this.node.innerHTML = `foo=${this.foo.toJSON()}`;
3726
}
3827

39-
yModel: YjsWidgets.IJupyterYModel;
28+
foo: Y.Text;
29+
yModel: IJupyterYModel;
4030
node: HTMLElement;
4131
}
4232

4333
const simple: JupyterFrontEndPlugin<void> = {
4434
id: 'example:simple',
4535
autoStart: true,
46-
requires: [YjsWidgets.IJupyterYWidgetManager],
47-
activate: (
48-
app: JupyterFrontEnd,
49-
wm: YjsWidgets.IJupyterYWidgetManager
50-
): void => {
51-
wm.registerWidget('MyWidget', YjsWidgets.JupyterYModel, MyWidget);
36+
requires: [IJupyterYWidgetManager],
37+
activate: (_: JupyterFrontEnd, wm: IJupyterYWidgetManager): void => {
38+
wm.registerWidget('MyWidget', JupyterYModel, MyWidget);
5239
}
5340
};
5441

0 commit comments

Comments
 (0)