Skip to content

Commit 3619b74

Browse files
committed
fetch module instead of using import directly
1 parent 7877d11 commit 3619b74

File tree

2 files changed

+22
-158
lines changed

2 files changed

+22
-158
lines changed

notebooks/introduction.ipynb

Lines changed: 14 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
},
3535
{
3636
"cell_type": "code",
37-
"execution_count": 1,
37+
"execution_count": null,
3838
"metadata": {
3939
"tags": []
4040
},
@@ -54,36 +54,11 @@
5454
},
5555
{
5656
"cell_type": "code",
57-
"execution_count": 2,
57+
"execution_count": null,
5858
"metadata": {
5959
"tags": []
6060
},
61-
"outputs": [
62-
{
63-
"data": {
64-
"text/plain": [
65-
"App(7fddbc3c7520)"
66-
]
67-
},
68-
"execution_count": 2,
69-
"metadata": {},
70-
"output_type": "execute_result"
71-
},
72-
{
73-
"data": {
74-
"application/vnd.jupyter.widget-view+json": {
75-
"model_id": "3a492cbddebd4240bee68d567ee504a1",
76-
"version_major": 2,
77-
"version_minor": 0
78-
},
79-
"text/plain": [
80-
"LayoutWidget(Layout(App(7fddbc3c7520)))"
81-
]
82-
},
83-
"metadata": {},
84-
"output_type": "display_data"
85-
}
86-
],
61+
"outputs": [],
8762
"source": [
8863
"from reactpy import component, html, run\n",
8964
"\n",
@@ -109,36 +84,11 @@
10984
},
11085
{
11186
"cell_type": "code",
112-
"execution_count": 3,
87+
"execution_count": null,
11388
"metadata": {
11489
"tags": []
11590
},
116-
"outputs": [
117-
{
118-
"data": {
119-
"text/plain": [
120-
"TodoList(7fdda0a0dea0)"
121-
]
122-
},
123-
"execution_count": 3,
124-
"metadata": {},
125-
"output_type": "execute_result"
126-
},
127-
{
128-
"data": {
129-
"application/vnd.jupyter.widget-view+json": {
130-
"model_id": "741e25bca51b400baedaf5ad683012fc",
131-
"version_major": 2,
132-
"version_minor": 0
133-
},
134-
"text/plain": [
135-
"LayoutWidget(Layout(TodoList(7fdda0a0dea0)))"
136-
]
137-
},
138-
"metadata": {},
139-
"output_type": "display_data"
140-
}
141-
],
91+
"outputs": [],
14292
"source": [
14393
"from reactpy import component, html\n",
14494
"\n",
@@ -187,36 +137,11 @@
187137
},
188138
{
189139
"cell_type": "code",
190-
"execution_count": 4,
140+
"execution_count": null,
191141
"metadata": {
192142
"tags": []
193143
},
194-
"outputs": [
195-
{
196-
"data": {
197-
"text/plain": [
198-
"Gallery(7fdda0a0e560)"
199-
]
200-
},
201-
"execution_count": 4,
202-
"metadata": {},
203-
"output_type": "execute_result"
204-
},
205-
{
206-
"data": {
207-
"application/vnd.jupyter.widget-view+json": {
208-
"model_id": "599b9afb8ca94319b7153f23e137308f",
209-
"version_major": 2,
210-
"version_minor": 0
211-
},
212-
"text/plain": [
213-
"LayoutWidget(Layout(Gallery(7fdda0a0e560)))"
214-
]
215-
},
216-
"metadata": {},
217-
"output_type": "display_data"
218-
}
219-
],
144+
"outputs": [],
220145
"source": [
221146
"import json\n",
222147
"from pathlib import Path\n",
@@ -266,7 +191,7 @@
266191
},
267192
{
268193
"cell_type": "code",
269-
"execution_count": 5,
194+
"execution_count": null,
270195
"metadata": {
271196
"tags": []
272197
},
@@ -302,27 +227,11 @@
302227
},
303228
{
304229
"cell_type": "code",
305-
"execution_count": 9,
230+
"execution_count": null,
306231
"metadata": {
307232
"tags": []
308233
},
309-
"outputs": [
310-
{
311-
"data": {
312-
"application/vnd.jupyter.widget-view+json": {
313-
"model_id": "f6fbf01c66dc42b0b53d15ecf64ddc73",
314-
"version_major": 2,
315-
"version_minor": 0
316-
},
317-
"text/plain": [
318-
"Box(children=(IntSlider(value=0, readout=False), LayoutWidget(Layout(SliderObserver(7fe2e19e7e20, slider=IntSl…"
319-
]
320-
},
321-
"execution_count": 9,
322-
"metadata": {},
323-
"output_type": "execute_result"
324-
}
325-
],
234+
"outputs": [],
326235
"source": [
327236
"from ipywidgets import Box, IntSlider\n",
328237
"from reactpy_jupyter import LayoutWidget\n",
@@ -342,27 +251,11 @@
342251
},
343252
{
344253
"cell_type": "code",
345-
"execution_count": 7,
254+
"execution_count": null,
346255
"metadata": {
347256
"tags": []
348257
},
349-
"outputs": [
350-
{
351-
"data": {
352-
"application/vnd.jupyter.widget-view+json": {
353-
"model_id": "3b018482270d4a7482aaed36e6ea2334",
354-
"version_major": 2,
355-
"version_minor": 0
356-
},
357-
"text/plain": [
358-
"Box(children=(IntSlider(value=0, readout=False), LayoutWidget(Layout(SliderObserver(7fdda0a0e6e0, slider=IntSl…"
359-
]
360-
},
361-
"execution_count": 7,
362-
"metadata": {},
363-
"output_type": "execute_result"
364-
}
365-
],
258+
"outputs": [],
366259
"source": [
367260
"from reactpy_jupyter import widgetize\n",
368261
"\n",
@@ -394,44 +287,11 @@
394287
},
395288
{
396289
"cell_type": "code",
397-
"execution_count": 8,
290+
"execution_count": null,
398291
"metadata": {
399292
"tags": []
400293
},
401-
"outputs": [
402-
{
403-
"name": "stderr",
404-
"output_type": "stream",
405-
"text": [
406-
"/tmp/ipykernel_304396/2504933715.py:4: DeprecationWarning: module_from_template() is deprecated due to instability - use the Javascript Components API instead. This function will be removed in a future release.\n",
407-
" victory = web.module_from_template(\"react\", \"victory-bar\", fallback=\"\")\n"
408-
]
409-
},
410-
{
411-
"data": {
412-
"text/plain": [
413-
"Demo(7fdda0a0fdc0)"
414-
]
415-
},
416-
"execution_count": 8,
417-
"metadata": {},
418-
"output_type": "execute_result"
419-
},
420-
{
421-
"data": {
422-
"application/vnd.jupyter.widget-view+json": {
423-
"model_id": "6d8207d9bfd844719d49d94595493d76",
424-
"version_major": 2,
425-
"version_minor": 0
426-
},
427-
"text/plain": [
428-
"LayoutWidget(Layout(Demo(7fdda0a0fdc0)))"
429-
]
430-
},
431-
"metadata": {},
432-
"output_type": "display_data"
433-
}
434-
],
294+
"outputs": [],
435295
"source": [
436296
"from reactpy import component, web\n",
437297
"\n",

src/index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,14 @@ class JupyterReactPyClient extends BaseReactPyClient {
6666
}
6767

6868
/** @param moduleName {string} */
69-
loadModule(moduleName) {
70-
return import(
71-
`${window.location.origin}${this.importSourceBaseUrl}/${moduleName}`
72-
);
69+
async loadModule(moduleName) {
70+
// Because import() does not behave directly when running via AnyWidgets. This is
71+
// because this code is itself imported and executed via a dynamic import() whose
72+
// source is a URL constructed by URL.createObjectURL. This appears to impact both
73+
// path resolution as well as CORS. By constrast the fetch() API does not appear to
74+
// be impacted by this. So we use fetch() to get the module source instead.
75+
const rsp = await fetch(`${this.importSourceBaseUrl}/${moduleName}`);
76+
return await import(URL.createObjectURL(await rsp.blob()));
7377
}
7478
}
7579

0 commit comments

Comments
 (0)