Skip to content

Commit bdc564e

Browse files
committed
Fix example switching: prefix cell IDs with slug, remove {#key}
1 parent e97075b commit bdc564e

2 files changed

Lines changed: 21 additions & 12 deletions

File tree

src/routes/[package]/[version]/examples/[slug]/+page.svelte

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import Tooltip from '$lib/components/common/Tooltip.svelte';
77
import { packageVersionsStore } from '$lib/stores/packageVersionsStore';
88
import { exampleGroupsStore } from '$lib/stores/examplesContext';
9+
import { notebookStore } from '$lib/stores/notebookStore';
910
import { groupByCategory } from '$lib/notebook/manifest';
1011
import { packages } from '$lib/config/packages';
1112
import type { PageData } from './$types';
@@ -38,12 +39,15 @@
3839
};
3940
});
4041
41-
// Reset Pyodide namespace when example changes (but don't terminate)
42-
// {#key} on Notebook handles component destruction and store cleanup via onDestroy
42+
// Reset Pyodide namespace and notebook store when example changes
4343
$effect(() => {
4444
const slug = data.meta.slug;
4545
4646
return async () => {
47+
// Cell IDs are prefixed with the slug, so old cells won't collide
48+
// with new ones. But we still need to clean up stale store entries
49+
// and reset the Python namespace.
50+
notebookStore.reset();
4751
try {
4852
const { reset } = await import('$lib/pyodide');
4953
await reset();
@@ -86,16 +90,14 @@
8690
downloadUrl={`${versionBasePath}/notebooks/${data.meta.file}`}
8791
/>
8892

89-
{#key data.meta.slug}
90-
<Notebook
91-
notebook={data.notebook}
92-
basePath={versionBasePath}
93-
precomputedOutputs={data.outputs}
94-
{figuresBasePath}
95-
showStaticOutputs={true}
96-
executable={data.meta.executable}
97-
/>
98-
{/key}
93+
<Notebook
94+
notebook={data.notebook}
95+
basePath={versionBasePath}
96+
precomputedOutputs={data.outputs}
97+
{figuresBasePath}
98+
showStaticOutputs={true}
99+
executable={data.meta.executable}
100+
/>
99101
</div>
100102

101103
<style>

src/routes/[package]/[version]/examples/[slug]/+page.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ export const load: PageLoad = async ({ params, parent, fetch }) => {
3232
// Parse the notebook
3333
const notebook = parseNotebook(JSON.stringify(rawNotebook));
3434

35+
// Prefix cell IDs with slug to prevent ID collisions when switching examples.
36+
// Many notebooks use simple numeric IDs (0, 1, 2...) which would collide,
37+
// causing Svelte to reuse components and leave stale state.
38+
for (const cell of notebook.cells) {
39+
cell.id = `${slug}:${cell.id}`;
40+
}
41+
3542
return {
3643
notebook,
3744
outputs,

0 commit comments

Comments
 (0)