You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"- `count` is a stateful property for that both the client JavaScript and Python have access to.\n",
99
+
" Shared state is defined via [traitlets](https://traitlets.readthedocs.io/en/stable/) with the `sync=True`\n",
100
+
" keyword argument.\n",
101
+
"\n",
102
+
"- `_esm` specifies a <u>**required**</u> [ECMAScript module](https://nodejs.org/api/esm.html) for the widget.\n",
103
+
" It defines and exports `render`, a function for rendering and initializes dynamic updates for the custom widget.\n",
104
+
"\n",
105
+
"- `_css` specifies an <u>**optional**</u> CSS stylesheet to load for the widget. It can be a full URL or plain text. Styles are loaded\n",
106
+
" in the global scope if using this feature, so take care to avoid naming conflicts.\n",
107
+
"\n",
108
+
" Feel free to modify some of the code above and re-execute the cells to see the changes 🪄"
109
+
]
110
+
},
111
+
{
112
+
"cell_type": "markdown",
113
+
"metadata": {},
114
+
"source": [
115
+
"## Progressive Development\n",
116
+
"\n",
117
+
"As your widgets grow in complexity, it is recommended to separate your\n",
118
+
"front-end code from your Python code. Just move the `_esm` and `_css`\n",
119
+
"definitions to separate files and reference them via path."
120
+
]
41
121
},
42
122
{
43
123
"cell_type": "code",
44
-
"source": "",
124
+
"execution_count": null,
45
125
"metadata": {
46
126
"trusted": true
47
127
},
48
128
"outputs": [],
49
-
"execution_count": null
129
+
"source": [
130
+
"from pathlib import Path\n",
131
+
"\n",
132
+
"class CounterWidget(anywidget.AnyWidget):\n",
133
+
" _esm = Path('/drive/index.js')\n",
134
+
" _css= Path('/drive/index.css')\n",
135
+
" count = traitlets.Int(0).tag(sync=True)\n",
136
+
"\n",
137
+
"counter = CounterWidget()\n",
138
+
"counter.count = 42\n",
139
+
"counter"
140
+
]
141
+
},
142
+
{
143
+
"cell_type": "markdown",
144
+
"metadata": {},
145
+
"source": [
146
+
"**Note**: since this particular notebook is meant to be used in JupyterLite, we specify `/drive` as the prefix for finding the `.js` and `.css` files. `/drive` is the location of the underlying (in-browser) file system where JupyterLite expects to find the files, so they can be displayed in the file browser.\n",
147
+
"\n",
148
+
"You can now open `index.js` and `index.css` in JupyterLab and edit the files directly. After making changes, you will have to recreate the widget so they are applied."
0 commit comments