Skip to content

Commit d2b6d7e

Browse files
Show nice-looking error if pyodide fails to load
1 parent 073f409 commit d2b6d7e

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

src/lib/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ async function pyodideInit(onStatusUpdate: (status: string) => void) {
1515
const micropip = pyodide.pyimport("micropip");
1616
onStatusUpdate("micropip install pyhtml-enhanced");
1717
await micropip.install('pyhtml-enhanced');
18+
onStatusUpdate("Almost there...");
1819
return pyodide;
1920
}
2021

src/routes/+page.svelte

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ p.html(
2121
2222
let htmlCode = $state('');
2323
24+
// TODO: Display errors nicely if the user messes up
2425
async function renderHtml() {
2526
htmlCode = await evalPyHTML(pyhtmlCode);
2627
}
2728
2829
let pyodideReady = $state(false);
30+
let pyodideError: string | undefined = $state(undefined);
2931
3032
let pyodideStatus = $state('Please wait...');
3133
@@ -34,10 +36,14 @@ p.html(
3436
}
3537
3638
onMount(async () => {
37-
await getPyodide(pyodideStatusUpdate);
38-
console.log('Pyodide is ready');
39-
renderHtml();
40-
pyodideReady = true;
39+
try {
40+
await getPyodide(pyodideStatusUpdate);
41+
renderHtml();
42+
pyodideReady = true;
43+
} catch (e) {
44+
console.error(e);
45+
pyodideError = `${e}`;
46+
}
4147
});
4248
</script>
4349

@@ -63,13 +69,17 @@ p.html(
6369
<pre><code>{htmlCode}</code></pre>
6470
</div>
6571
</main>
72+
{:else if pyodideError}
73+
<h2>Oh no!</h2>
74+
<p>Pyodide failed to load!</p>
75+
<p>{pyodideError}</p>
6676
{:else}
67-
<div class="status-outer">
68-
<div class="status-inner">
69-
<i class="las la-sync spin"></i>
70-
<p class="status-text">{pyodideStatus}</p>
77+
<div class="status-outer">
78+
<div class="status-inner">
79+
<i class="las la-sync spin"></i>
80+
<p class="status-text">{pyodideStatus}</p>
81+
</div>
7182
</div>
72-
</div>
7383
{/if}
7484

7585
<style>

vite.config.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import { fileURLToPath } from 'url';
99
const PYODIDE_EXCLUDE = [
1010
'!**/*.{md,html}',
1111
'!**/*.d.ts',
12-
// Need to include .whl files or micropip won't load correctly
13-
// '!**/*.whl',
12+
'!**/*.whl',
1413
'!**/node_modules',
1514
];
1615

0 commit comments

Comments
 (0)