|
7 | 7 | "metadata": {}, |
8 | 8 | "outputs": [], |
9 | 9 | "source": [ |
10 | | - "from ypywidgets import Widget, reactive" |
| 10 | + "from ypywidgets.comm import CommWidget\n", |
| 11 | + "from pycrdt import Map" |
11 | 12 | ] |
12 | 13 | }, |
13 | 14 | { |
14 | 15 | "cell_type": "code", |
15 | 16 | "execution_count": null, |
16 | | - "id": "18d1ca71-1943-4902-b642-06e3d812e23f", |
| 17 | + "id": "6d9538c9-4858-4c9d-b347-18965660115d", |
17 | 18 | "metadata": {}, |
18 | 19 | "outputs": [], |
19 | 20 | "source": [ |
20 | | - "class MyWidget(Widget):\n", |
21 | | - " foo = reactive(\"\")\n", |
22 | | - " bar = reactive(\"\")" |
| 21 | + "class MySlider(CommWidget):\n", |
| 22 | + " def __init__(self, value=50, min=0, max=100, step=1):\n", |
| 23 | + " super().__init__(\n", |
| 24 | + " comm_metadata={\n", |
| 25 | + " \"ymodel_name\": \"MySlider\",\n", |
| 26 | + " \"create_ydoc\": True,\n", |
| 27 | + " }\n", |
| 28 | + " )\n", |
| 29 | + "\n", |
| 30 | + " self.ydoc[\"state\"] = self._state = Map()\n", |
| 31 | + "\n", |
| 32 | + " self.min = min\n", |
| 33 | + " self.max = max\n", |
| 34 | + " self.step = step\n", |
| 35 | + " self.value = value\n", |
| 36 | + "\n", |
| 37 | + " @property\n", |
| 38 | + " def value(self):\n", |
| 39 | + " return self._state['value']\n", |
| 40 | + "\n", |
| 41 | + " @value.setter\n", |
| 42 | + " def value(self, v):\n", |
| 43 | + " self._state['value'] = v\n", |
| 44 | + "\n", |
| 45 | + " @property\n", |
| 46 | + " def min(self):\n", |
| 47 | + " return self._state['min']\n", |
| 48 | + "\n", |
| 49 | + " @min.setter\n", |
| 50 | + " def min(self, v):\n", |
| 51 | + " self._state['min'] = v\n", |
| 52 | + "\n", |
| 53 | + " @property\n", |
| 54 | + " def max(self):\n", |
| 55 | + " return self._state['max']\n", |
| 56 | + "\n", |
| 57 | + " @max.setter\n", |
| 58 | + " def max(self, v):\n", |
| 59 | + " self._state['max'] = v\n", |
| 60 | + "\n", |
| 61 | + " @property\n", |
| 62 | + " def step(self):\n", |
| 63 | + " return self._state['step']\n", |
| 64 | + "\n", |
| 65 | + " @step.setter\n", |
| 66 | + " def step(self, v):\n", |
| 67 | + " self._state['step'] = v" |
23 | 68 | ] |
24 | 69 | }, |
25 | 70 | { |
|
29 | 74 | "metadata": {}, |
30 | 75 | "outputs": [], |
31 | 76 | "source": [ |
32 | | - "w = MyWidget()\n", |
| 77 | + "w = MySlider()\n", |
33 | 78 | "w" |
34 | 79 | ] |
35 | 80 | }, |
36 | 81 | { |
37 | 82 | "cell_type": "code", |
38 | 83 | "execution_count": null, |
39 | | - "id": "72a7799e-ce21-43ed-865c-77fa1fc69eb2", |
| 84 | + "id": "9269a3f1-a061-478f-8728-7393a4aec2d0", |
40 | 85 | "metadata": {}, |
41 | 86 | "outputs": [], |
42 | 87 | "source": [ |
43 | | - "w.foo = 3" |
| 88 | + "w.value" |
44 | 89 | ] |
45 | 90 | }, |
46 | 91 | { |
47 | 92 | "cell_type": "code", |
48 | 93 | "execution_count": null, |
49 | | - "id": "d7289dc9-29c1-4422-8962-1b013bfda40a", |
| 94 | + "id": "8ac7ea5c-8315-44eb-aa62-f223f86d267d", |
50 | 95 | "metadata": {}, |
51 | 96 | "outputs": [], |
52 | 97 | "source": [ |
53 | | - "w.bar = 4" |
| 98 | + "w.value = 98" |
| 99 | + ] |
| 100 | + }, |
| 101 | + { |
| 102 | + "cell_type": "code", |
| 103 | + "execution_count": null, |
| 104 | + "id": "bd2c92fb-9653-4de5-8a20-d463205f3a9e", |
| 105 | + "metadata": {}, |
| 106 | + "outputs": [], |
| 107 | + "source": [ |
| 108 | + "w.value = 36" |
| 109 | + ] |
| 110 | + }, |
| 111 | + { |
| 112 | + "cell_type": "code", |
| 113 | + "execution_count": null, |
| 114 | + "id": "3a5680c6-77ae-4f71-a982-b3447dcb4fd0", |
| 115 | + "metadata": {}, |
| 116 | + "outputs": [], |
| 117 | + "source": [ |
| 118 | + "w.value" |
| 119 | + ] |
| 120 | + }, |
| 121 | + { |
| 122 | + "cell_type": "code", |
| 123 | + "execution_count": null, |
| 124 | + "id": "55896ccc-a128-4abf-a34d-4a50272946dd", |
| 125 | + "metadata": {}, |
| 126 | + "outputs": [], |
| 127 | + "source": [ |
| 128 | + "w2 = MySlider(max=200, step=2, value=152)\n", |
| 129 | + "w2" |
| 130 | + ] |
| 131 | + }, |
| 132 | + { |
| 133 | + "cell_type": "code", |
| 134 | + "execution_count": null, |
| 135 | + "id": "c54528b0-6eae-4601-b6f6-ab2f31630815", |
| 136 | + "metadata": {}, |
| 137 | + "outputs": [], |
| 138 | + "source": [ |
| 139 | + "w2.value" |
| 140 | + ] |
| 141 | + }, |
| 142 | + { |
| 143 | + "cell_type": "code", |
| 144 | + "execution_count": null, |
| 145 | + "id": "23380f74-83ea-4409-a990-a0b8958dde2b", |
| 146 | + "metadata": {}, |
| 147 | + "outputs": [], |
| 148 | + "source": [ |
| 149 | + "w2.value" |
54 | 150 | ] |
55 | 151 | } |
56 | 152 | ], |
|
70 | 166 | "name": "python", |
71 | 167 | "nbconvert_exporter": "python", |
72 | 168 | "pygments_lexer": "ipython3", |
73 | | - "version": "3.11.3" |
| 169 | + "version": "3.13.12" |
74 | 170 | } |
75 | 171 | }, |
76 | 172 | "nbformat": 4, |
|
0 commit comments