Skip to content

Commit b825452

Browse files
committed
Add messages for Pyodide ready and loading
1 parent 33c6acb commit b825452

File tree

6 files changed

+79
-18
lines changed

6 files changed

+79
-18
lines changed

Diff for: CNAME

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
wcs.vishnus.me
1+
woodlands.codes

Diff for: _includes/pyodide-ready-toast.html

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<div class="position-fixed bottom-0 end-0 p-3" style="z-index: 11">
2+
<div id="pyodide-ready" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
3+
<div class="toast-header">
4+
<strong class="me-auto">Woodlands Computer Science</strong>
5+
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
6+
</div>
7+
<div class="toast-body">
8+
Python interpreter ready!
9+
</div>
10+
</div>
11+
</div>
12+
13+
<div class="position-fixed bottom-0 end-0 p-3" style="z-index: 11">
14+
<div id="pyodide-loading" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
15+
<div class="toast-header">
16+
<strong class="me-auto">Woodlands Computer Science</strong>
17+
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
18+
</div>
19+
<div class="toast-body">
20+
Python interpreter loading...
21+
</div>
22+
</div>
23+
</div>

Diff for: _layouts/default.html

+6
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,9 @@
88
</div>
99

1010
</div>
11+
12+
{% if page.code_editor %}
13+
14+
{% include pyodide-ready-toast.html %}
15+
16+
{% endif %}

Diff for: assets/js/code-editor.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class CodeEditor extends HTMLDivElement {
2020

2121
this.innerHTML = `
2222
<button id="run-button-${id}" class="btn btn-primary my-3">Run</button>
23-
<button id="terminate-button-${id}" data-bs-toggle="tooltip" data-bs-placement="right" data-bs-original-title="Clicking this will restart the Python interpreter, so be careful!" class="btn btn-danger my-3 ms-2">Terminate Pyodide</button>
23+
<button id="terminate-button-${id}" data-bs-toggle="tooltip" data-bs-placement="right" data-bs-original-title="Clicking this will restart the Python interpreter, so be careful! Only do this if your program is taking too long to run." class="btn btn-danger my-3 ms-2">Restart Python</button>
2424
<div class="editor" id="ace-editor-${id}">${code}</div>
2525
<div class="mt-3 input-output-container">
2626
<div data-bs-toggle="tooltip" data-bs-placement="bottom" data-bs-original-title="Input" class="d-block position-relative input-container">
@@ -57,11 +57,24 @@ const tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
5757
})
5858

5959

60+
let loadingToast = new bootstrap.Toast(document.getElementById('pyodide-loading'));
61+
let readyToast = new bootstrap.Toast(document.getElementById('pyodide-ready'));
6062

61-
import { asyncRun, asyncTerminate } from "/assets/js/py-worker.js";
6263

64+
import { asyncRun, asyncTerminate, asyncCheckReady } from "/assets/js/py-worker.js";
6365

66+
async function checkReady() {
67+
const { ready } = await asyncCheckReady();
68+
readyToast.show()
69+
}
70+
71+
async function main() {
72+
loadingToast.show();
73+
await checkReady();
74+
loadingToast.hide();
75+
}
6476

77+
main();
6578

6679

6780
async function runPython(script, input_id, output_id) {
@@ -81,10 +94,14 @@ async function runPython(script, input_id, output_id) {
8194

8295
async function terminatePyodide() {
8396
asyncTerminate();
97+
loadingToast.show()
98+
await checkReady();
99+
loadingToast.hide();
84100
}
85101

86102

87103

104+
88105
// Uncomment the below lines to use Skulpt
89106

90107
// function outf(text) {

Diff for: assets/js/py-worker.js

+17-13
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
11
var pyodideWorker = new Worker("/assets/js/webworker.js");
22

3-
var terminated = false;
4-
53
export function run(script, stdin, onSuccess, onError) {
64
pyodideWorker.onerror = onError;
75
pyodideWorker.onmessage = (e) => onSuccess(e.data);
86
pyodideWorker.postMessage({
9-
stdin,
107
python: script,
8+
stdin: stdin,
119
});
1210
}
1311

1412
export function asyncRun(script, stdin) {
15-
16-
if (terminated) {
17-
terminated = false;
18-
pyodideWorker = new Worker("/assets/js/webworker.js");
19-
}
20-
2113
return new Promise(function (onSuccess, onError) {
2214
run(script, stdin, onSuccess, onError);
2315
});
2416
}
2517

2618
export function asyncTerminate() {
27-
if (!terminated) {
28-
pyodideWorker.terminate()
29-
terminated = true;
30-
}
19+
pyodideWorker.terminate()
20+
pyodideWorker = new Worker("/assets/js/webworker.js");
21+
}
22+
23+
24+
export function asyncCheckReady() {
25+
return new Promise(function(onSuccess) {
26+
checkReady(onSuccess);
27+
})
28+
}
29+
30+
export function checkReady(onSuccess) {
31+
pyodideWorker.onmessage = (e) => onSuccess(e.data);
32+
pyodideWorker.postMessage({
33+
onlyChecking: true,
34+
});
3135
}

Diff for: assets/js/webworker.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ let pyodideReadyPromise = loadPyodideAndPackages();
1414

1515
async function setup_function() {
1616
await pyodideReadyPromise;
17+
18+
19+
1720
let code = `
1821
import sys, io, traceback
1922
namespace = {} # use separate namespace to hide run_code, modules, etc.
@@ -42,10 +45,18 @@ let setup = setup_function()
4245

4346
self.onmessage = async (event) => {
4447
// make sure loading is done
48+
49+
const { python, stdin, onlyChecking} = event.data;
4550
await pyodideReadyPromise;
51+
52+
if (onlyChecking) {
53+
self.postMessage({ ready: true })
54+
return;
55+
}
56+
4657
await setup;
4758
// Don't bother yet with this line, suppose our API is built in such a way:
48-
const { python, stdin } = event.data;
59+
4960
// The worker copies the context in its own "memory" (an object mapping name to values)
5061
// Now is the easy part, the one that is similar to working in the main thread:
5162

@@ -56,7 +67,7 @@ self.onmessage = async (event) => {
5667
self.pyodide.globals.code_to_run = python
5768
self.pyodide.globals.inp = stdin
5869
let results = await self.pyodide.runPythonAsync('run_code(code_to_run)')
59-
self.postMessage({ results });
70+
self.postMessage({ results: results });
6071
} catch (error) {
6172
self.postMessage({ error: error.message });
6273
}

0 commit comments

Comments
 (0)