Skip to content

Commit

Permalink
Ensure pyodide buttons are only added once
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Sep 9, 2024
1 parent 905b9be commit 67c9a8a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
6 changes: 3 additions & 3 deletions nbsite/pyodide/ServiceHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ if ('serviceWorker' in navigator) {
reg.onupdatefound = () => {
const installingWorker = reg.installing;
installingWorker.onstatechange = () => {
if (installingWorker.state === 'installed' &&
if (installingWorker.state === 'installed' &&
navigator.serviceWorker.controller) {
// Reload page if service worker is replaced
// Reload page if service worker is replaced
location.reload();
}
}
}
}
})
Expand Down
10 changes: 5 additions & 5 deletions nbsite/pyodide/WebWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ async function loadApplication(cell_id, path) {
if (path != null) {
for (const key of Object.keys(REQUIRES)) {
if (path.replace('.html', '').endsWith(key.replace('.md', ''))) {
for (const req of REQUIRES[key]) {
packages.push(req)
}
for (const req of REQUIRES[key]) {
packages.push(req)
}
}
}
}
Expand Down Expand Up @@ -169,9 +169,9 @@ self.onmessage = async (event) => {
id: msg.id
});
try {
await self.pyodide.runPythonAsync(`await micropip.install('${pkg}', keep_going=True)`)
await self.pyodide.runPythonAsync(`await micropip.install('${pkg}', keep_going=True)`)
} catch(e) {
console.log(`Auto-detected dependency ${pkg} could not be installed.`)
console.log(`Auto-detected dependency ${pkg} could not be installed.`)
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions nbsite/pyodide/WorkerHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pyodideWorker.onmessage = async (event) => {
// Setup bi-directional syncing
pyodideWorker.documents[msg.id] = jsdoc = view.model.document
if (pyodideWorker.queues != null && pyodideWorker.queues.has(msg.id)) {
// Delete old message queue
// Delete old message queue
pyodideWorker.queues.delete(msg.id)
}
jsdoc.on_change(send_change.bind(null, jsdoc, msg.id), false)
Expand All @@ -102,9 +102,9 @@ pyodideWorker.onmessage = async (event) => {
pyodideWorker.documents[msg.id].apply_json_patch(msg.patch, msg.buffers, setter_id='py')
} finally {
if (patch_status === 0) {
patching.delete(msg.id)
patching.delete(msg.id)
} else {
patching.set(msg.id, patch_status)
patching.set(msg.id, patch_status)
}
}
}
Expand Down
18 changes: 12 additions & 6 deletions nbsite/pyodide/_static/run_cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,13 @@ const _query_params = new Proxy(new URLSearchParams(window.location.search), {
});

let ACCEPTED = false;
let ADDED = false;

const _addRunButtonToCodeCells = () => {
if (ADDED) {
return
}

// If Pyodide Worker hasn't loaded, wait a bit and try again.
if (window.pyodideWorker === undefined) {
setTimeout(addRunButtonToCodeCells, 250)
Expand Down Expand Up @@ -126,9 +131,9 @@ const _addRunButtonToCodeCells = () => {
while (true) {
let cell_id = _codeCellId(i)
let cell = document.getElementById(cell_id)
if (cell == null) {
break
}
if (cell == null) {
break
}
const output = document.getElementById(`output-${cell_id}`)
const stdout = document.getElementById(`stdout-${cell_id}`)
const stderr = document.getElementById(`stderr-${cell_id}`)
Expand All @@ -141,13 +146,13 @@ const _addRunButtonToCodeCells = () => {
stdout.innerHTML = '';
stdout.style.display = 'none';
}
if (stderr) {
if (stderr) {
stderr.innerHTML = '';
stderr.style.display = 'none';
}
executeCell(cell_id)
executeCell(cell_id)
}
i++;
i++;
}
})
})
Expand All @@ -157,6 +162,7 @@ const _addRunButtonToCodeCells = () => {
run_button.click()
run_button.click()
}
ADDED = true
}

_runWhenDOMLoaded(_addRunButtonToCodeCells)

0 comments on commit 67c9a8a

Please sign in to comment.