-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
78 lines (66 loc) · 2.07 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>MicroPython REPL Demo</title>
<link rel="stylesheet" href="./css/index.css">
<script type="module">
import dedent from 'https://cdn.jsdelivr.net/npm/codedent/es.js';
import init from './micro-repl.js';
const show = textContent => {
output.append(
Object.assign(
document.createElement('code'),
{ textContent }
)
);
};
connect.onclick = () => {
connect.disabled = true;
output.replaceChildren();
init({
onceClosed(error) {
connect.disabled = false;
if (error) console.warn(error);
}
}).then(async board => {
globalThis.board = board;
// there is an active state
console.info('Board active', board.active);
// once initialized, the REPL welcome message can be
// fully ignored or showed
show(await board.output);
// this works for any Python code
await board.write(dedent(`
import sys
print(sys.version)
print(sys.implementation._machine)
`));
const lines = (await board.output).split(/[\r\n]+/);
show(lines.slice(-3).join('\n'));
// each `write` wait for a result
await board.write('help()');
// where the output can be read
show(await board.output);
await board.write('print("bye bye")');
// we can close it without errors
await board.close();
// once closed it throws on write and read
// but the result would be still there
console.info('Board active', board.active);
// the final line/result remains though
const result = await board.result;
if (/^\S+?Error: /.test(result))
console.warn(result);
else
console.info('last result', result);
});
};
</script>
</head>
<body>
<button id="connect">connect</button>
<pre id="output"></pre>
</body>
</html>