This repository has been archived by the owner on Sep 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
pyscript.js
876 lines (786 loc) · 32.4 KB
/
pyscript.js
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
"use strict";
/******************************************************************************
MicroPyScript. 🐍
A small, simple, single file kernel of getting scripting languages into the
browser, made for testing purposes and technical exploration.
See the README for more details, design decisions, and an explanation of how
things work.
Authors:
- Nicholas H.Tollervey (ntollervey@anaconda.org)
Copyright (c) 2022 Anaconda Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
******************************************************************************/
const main = function() {
/**************************************************************************
The core MicroPyScript app definition.
Its main concern is to:
* Keep the bare minimum of state.
* Load and process any configuration from the page.
* Provide a mechanism for plugins to be registered, configured and then
started.
* Load and start the scripting language interpreter.
* Dispatch the following events to signal various changes in state or the
completion of tasks (such as starting the interpreter).
- "py-configured", when configuration is processed.
- "py-plugin-registered", when a plugin is registered.
- "py-plugin-started", when a plugin is started.
- "py-interpreter-loaded", when the interpreter has been downloaded.
- "py-interpreter-ready", when the interpreter is ready to process
Python for configuration reasons (such as pip installing modules).
- "py-file-fetched", when a file, to be added to the interpreter's
filesystem, has been fetched from the network.
- "py-files-loaded", when all the files have been copied onto the
interpreter's filesystem.
- "py-finished-setup", when the interpreter and filesystem are both
ready. At this point, PyScript is ready to evaluate user's code.
* Define, configure and start built-in MicroPyScript plugins (e.g. the
<py-script> tag).
**************************************************************************/
const logger = function() {
/*
Really simple logging. Emoji 🐍 highlights MicroPyScript app logs. ;-)
*/
return Function.prototype.bind.call(console.log, console, "🐍 ", ...arguments);
}();
logger("Starting MicroPyScript. 👋🐍");
class Interpreter {
/*
Defines and encapsulates a interpreter used by MicroPyScript to evaluate
code or run an interactive REPL with a scripting language compiled to
WASM.
*/
static get url() {
/*
The URL pointing to where to download the interpreter.
*/
return "";
}
static ready() {
/*
Dispatch the py-interpreter-ready event (for when the interpreter has
eventually started and is ready to evaluate code).
*/
const pyInterpreterReady = new CustomEvent("py-interpreter-ready", this);
document.dispatchEvent(pyInterpreterReady);
}
static print(output) {
/*
Dispatch the py-print event (for when output is printed to stdout).
*/
const pyPrint = new CustomEvent("py-print", {detail: output})
document.dispatchEvent(pyPrint);
}
start(config) {
/*
Instantiate, setup, configure and do whatever else is needed to
start the interpreter. This is called once the interpreter is loaded into
the browser.
*/
}
eval(script) {
/*
Use the interpreter to evaluate the script.code.
*/
}
addFile(path, content) {
/*
Copy a file with the referenced path, and content, onto the local
filesystem available to the interpreter.
*/
}
startREPL() {
/*
Start an interactive REPL session with the interpreter.
*/
}
stdin(input) {
/*
Pass the input into the interpreter's stdin.
*/
}
}
// The innerHTML of the default splash screen to show while MicroPyScript is
// starting up. Currently the page is greyed out and the words
// "Loading MicroPyScript...".
const defaultSplash= '<div style="position:fixed;width:100%;height:100%;top:0;left:0;right:0;bottom:0;background-color:rgba(0,0,0,0.5);z-index:9999;"><div style="position:absolute;top:50%;left:40%;color:white;">Loading MicroPyScript... 🐍</div></div>';
/**************************************************************************
Built-in plugins and interpreters.
**************************************************************************/
const pyScriptTag = function(){
// Contains Python scripts found on the page.
const scripts = [];
// Contains Python scripts whose source code is available, and pending
// evaluation by the interpreter.
const pendingScripts = [];
// Eventually references the available interpreter, once ready.
let availableInterpreter = null;
// Eventually references the first <py-script> tag into which all
// stdout will be piped.
let stdoutTag = null;
function registerScript(e) {
/*
Add a Python script to the scripts array. If required load the code
by fetching it from the URL found in the script's src attribute.
*/
const script = e.detail;
// Ignore code that is just whitespace.
script.code = script.code.trim() ? script.code : "";
logger("Registered script. 📄", script);
scripts.push(script);
if (script.code) {
// The script's code was inline.
const pyLoadedScript = new CustomEvent("py-script-loaded", {detail: script});
document.dispatchEvent(pyLoadedScript);
} else if (script.src) {
// Handle asynchronous loading of the script's code from the
// URL in src.
fetch(script.src).then(function(response) {
logger(`Fetched script from "${script.src}". 📡`, response);
if (response.ok) {
response.text().then((data) => {
script.code = data;
logger("Updated script code. 📄", script);
const pyLoadedScript = new CustomEvent("py-script-loaded", {detail: script});
document.dispatchEvent(pyLoadedScript);
})
} else {
// Abort.
throw `💥 Cannot load script from "${script.src}"`;
}
});
} else {
// Warn that a script has no source code either inline or via
// the src attribute.
logger("Script has no source code. ⁉️😕", script);
}
}
function scriptLoaded(e) {
/*
The given script is ready to be evaluated.
Either queue it for later evaluation if the interpreter isn't ready
yet, or dispatch the py-eval-script event to signal to the interpreter
it should evaluate the script.
*/
if (availableInterpreter) {
// Interpreter is ready, so evaluate the code.
const pyEvalScript = new CustomEvent("py-eval-script", {detail: e.detail});
document.dispatchEvent(pyEvalScript);
} else {
// No interpreter, so add to pendingScripts queue, to be evaluated
// once the interpreter is ready.
pendingScripts.push(e.detail);
}
}
function evaluateScript(e) {
/*
Given the interpreter is ready AND the script is loaded,
evaluate the script with the interpreter.
*/
logger("Evaluating code. 🤖\n" + e.detail.code);
availableInterpreter.eval(e.detail);
}
function onPrint(e) {
/*
Handle print to stdout events.
*/
if (stdoutTag === null) {
const firstPyScriptTag = document.querySelector("py-script");
const preTag = document.createElement("pre");
firstPyScriptTag.appendChild(preTag);
stdoutTag = document.createElement("code");
preTag.appendChild(stdoutTag);
}
stdoutTag.innerText = stdoutTag.innerText + e.detail;
}
document.addEventListener("py-script-registered", registerScript);
document.addEventListener("py-script-loaded", scriptLoaded);
document.addEventListener("py-eval-script", evaluateScript);
// The object to contain the various functions needed to handle the
// life cycle of this plugin, returned to the MicroPyScript environment.
const plugin = {
name: "py-script",
start: function(config) {
// Define the PyScript element.
class PyScript extends HTMLElement {
connectedCallback() {
/*
All code is dispatched as a py-script-registered event
for later processing.
Additional metadata if available:
- the src value for remote source file
- this element as target
*/
const code = this.textContent;
this.textContent = "";
const script = {
code: code.trim() ? code : "",
src: this.attributes.src ? this.attributes.src.value : "",
target: this
};
const pyScriptRegistered = new CustomEvent("py-script-registered", {"detail": script});
document.dispatchEvent(pyScriptRegistered);
document.addEventListener("py-print", onPrint);
}
}
// Register it (thus extracting the code from the page).
customElements.define('py-script', PyScript);
},
onInterpreterReady: function(config, interpreter) {
availableInterpreter = interpreter;
// Evaluate any pending scripts.
pendingScripts.forEach(function(script) {
const pyEvalScript = new CustomEvent("py-eval-script", {detail: script});
document.dispatchEvent(pyEvalScript);
})
// Empty pendingScripts.
pendingScripts.splice(0, pendingScripts.length);
}
}
return plugin;
}();
class MicroPythonInterpreter extends Interpreter {
/*
MicroPython (https://micropython.org) is a lean and efficient
implementation of the Python 3 programming language that includes a
small subset of the Python standard library and is optimised to run on
microcontrollers and in constrained environments.
*/
static get url() {
return "mpbuild/micropython.js";
}
start(config) {
let mp_memory = 1024 * 1024; // 1Mb
if(config.mp_memory) {
mp_memory = config.mp_memory;
}
document.addEventListener('micropython-print', function(e) {
Interpreter.print(e.data);
}, false);
let mp_js_startup = Module['onRuntimeInitialized'];
Module["onRuntimeInitialized"] = async function() {
mp_js_startup();
mp_js_init(mp_memory);
Interpreter.ready();
}
}
eval(script) {
mp_js_do_str(script.code);
}
addFile(path, content) {
Module.FS.writeFile(path, content);
}
startREPL() {
mp_js_init_repl();
}
stdin(input) {
const bytes = Uint8Array.from(input.split("").map(x => x.charCodeAt()));
bytes.forEach(function(b) {
mp_js_process_char(b);
});
}
}
class CPythonInterpreter extends Interpreter {
/*
The standard CPython version of Python compiled to WASM. For more
information, see:
https://github.com/python/cpython/blob/main/Tools/wasm/README.md
TODO: Finish this.
*/
static get url() {
return "pybuild/python.js";
}
}
class PyodideInterpreter extends Interpreter {
/*
Pyodide is a Python distribution for the browser, compiled to WASM. For
more information, see:
https://pyodide.org/en/stable/
TODO: Finish this. It's a hack!
*/
constructor() {
super();
// Read and emptied when Pyodide calls the stdin_func to read user
// input. (This feels wrong, but that's just how Pyodide works.)
this.stdInBuffer = [];
this.repr_shorten = null;
this.banner = null;
this.await_fut = null;
this.pyconsole = null;
this.clear_console = null;
this.term = null;
}
static get url() {
return "https://cdn.jsdelivr.net/pyodide/v0.21.3/full/pyodide.js";
}
print(output) {
let processed_output = output.replaceAll("\n", "\n\r");
Interpreter.print(processed_output);
}
start(config) {
const stdout_func = function(output) {
if (output === "Python initialization complete") {
return;
}
this.print(output);
};
const stdin_func = function() {
return null;
};
async function main() {
let pyodide = await loadPyodide({
stdout: stdout_func,
stdin: stdin_func
});
return pyodide;
}
let pyodideReadyPromise = main();
const myself = this;
pyodideReadyPromise.then(result => {
myself.pyodide = result;
Interpreter.ready()
});
}
eval(script) {
this.pyodide.runPython(script.code);
}
addFile(path, content) {
this.pyodide.FS.writeFile(path, content);
}
startREPL(term) {
term.prompt = () => {
term.write("\r\n>> ");
}
logger("Starting Pyodide REPL. ⌨️");
let namespace = this.pyodide.globals.get("dict")();
this.pyodide.runPython(
`
import sys
from pyodide.ffi import to_js
from pyodide.console import PyodideConsole, repr_shorten, BANNER
import __main__
pyconsole = PyodideConsole(__main__.__dict__)
import builtins
async def await_fut(fut):
res = await fut
if res is not None:
builtins._ = res
return to_js([res], depth=1)
def clear_console():
pyconsole.buffer = []
`,
{ globals: namespace },
);
this.repr_shorten = namespace.get("repr_shorten");
this.banner = namespace.get("BANNER");
this.await_fut = namespace.get("await_fut");
this.pyconsole = namespace.get("pyconsole");
this.clear_console = namespace.get("clear_console");
namespace.destroy();
this.print(this.banner);
this.print("\r\n>>> ")
this.pyconsole.stdout_callback = (output) => {
this.print(output);
}
this.term = term;
}
stdin(input) {
// Push the input to the stdInBuffer, which is read and cleared by
// Pyodide at some point in the future.
if (input === "\u007F") {
// Delete
if (this.stdInBuffer.length > 0) {
this.print("\b \b");
}
this.stdInBuffer = this.stdInBuffer.slice(0, -1);
} else if (input === "\r" && this.pyconsole) {
const code = this.stdInBuffer.join("").trimEnd();
this.stdInBuffer = [];
let fut = this.pyconsole.push(code);
switch (fut.syntax_check) {
case "syntax-error":
this.print(fut.formatted_error.trimEnd());
break;
case "incomplete":
this.print("\r\n... ");
break;
case "complete":
this.print("\r\n>>> ");
break;
default:
throw new Error(`Unexpected type ${fut.syntax_check}`);
}
let wrapped = this.await_fut(fut);
wrapped.then(value => {
if (value !== undefined) {
const output = this.repr_shorten.callKwargs(value, {
separator: "\n<long output truncated>\n",
limit: 99999
}).trimEnd();
if (output) {
this.term.write("\x1b[2K\r"); // clear line
this.print(output + "\r\n>>> ");
}
}
if (this.pyodide.isPyProxy(value)) {
value.destroy();
}
}).catch(e => {
if (e.constructor.name === "PythonError") {
this.term.write("\x1b[2K\r"); // clear line
const message = fut.formatted_error || e.message;
this.print(message.trimEnd().replace(/\n/g, "\r\n") + "\r\n>>> ");
} else {
throw e;
}
}).finally(() => {
fut.destroy();
wrapped.destroy();
});
} else {
this.stdInBuffer.push(input);
this.print(input);
}
}
}
/**************************************************************************
Utility functions for MicroPyScript.
**************************************************************************/
function toJSON(node) {
/*
Takes a node in the DOM and serialises it into JSON.
*/
}
function toDOM(obj) {
/*
Takes a JSON object and returns a node to mutate into the DOM.
*/
}
/**************************************************************************
Variables and functions needed for the life-cycle of MicroPyScript.
**************************************************************************/
// Default configuration settings for MicroPyScript. These may be overridden
// by the app.loadConfig function.
// The "files" object should look like this:
// "files": {
// "myfile.py": "https://domain.com/myfile.py",
// "myotherfile.txt": "otherfile.txt"
// }
// Key: filename on WASM filesystem.
// Value: url to download content of file.
const config = {
"interpreter": "micropython", // Numpty default.
"splash": defaultSplash, // loading message in grey overlay.
"files": {} // No files by default.
}
// Contains plugins to the MicroPyScript context.
const plugins = [];
// Details of interpreters.
// Key: lowercase interpreter name.
// Value: the class wrapping that version of the interpreter.
const interpreters = {
"micropython": MicroPythonInterpreter,
"cpython": CPythonInterpreter,
"pyodide": PyodideInterpreter
}
// Files to be loaded to the filesystem once the interpreter is loaded (but
// perhaps not yet ready).
const filesToLoad = [];
// Eventually references an instance of the Interpreter class, representing the
// started interpreter.
let interpreter = null;
// Flag to indicate that all the files to be copied into the filesystem
// (defined in config) have been downloaded and copied over.
let filesLoaded = false;
// Flag to indicate the interpreter is ready to evaluate scripts.
let interpreterReady = false;
// To hold a reference to the div containing the start-up splash screen
// displayed while MicroPyScript starts up.
let splashElement = null;
function loadConfig() {
/*
Loads configuration for running MicroPyScript from JSON contained in the
py-config element. Updates the default config object. Dispatches a
py-configured event when done.
*/
let userConf = {};
const element = document.querySelector('py-config');
if (element) {
userConf = JSON.parse(element.textContent);
element.textContent = "";
}
Object.keys(userConf).forEach((key) => {
config[key] = userConf[key];
});
logger("Loaded configuration. ✅", config);
const pyConfigured = new CustomEvent("py-configured", {detail: config});
document.dispatchEvent(pyConfigured);
}
function splashOn() {
/*
Display the splash screen for when MicroPyScript is starting.
*/
splashElement = document.createElement("div");
splashElement.innerHTML = config.splash;
const body = document.getElementsByTagName('body')[0];
body.appendChild(splashElement);
}
function splashOff() {
/*
Remove the splash screen, once MicroPyScript is finished starting.
*/
splashElement.parentNode.removeChild(splashElement);
}
function registerPlugin(plugin) {
/*
Add a plugin to the MicroPyScript context, after calling its configure
method.
*/
logger(`Registering plugin "${plugin.name}". 🔌`);
plugin.configure?.(config);
plugins.push(plugin);
if (interpreterReady) {
startPlugin(plugin)
plugin.onInterpreterReady?.(config, interpreter);
}
const pyPluginRegistered = new CustomEvent("py-plugin-registered", {detail: { config: config, plugin: plugin}});
document.dispatchEvent(pyPluginRegistered);
}
function startPlugins() {
/*
Start all registered plugins.
*/
plugins.forEach(function(plugin) {
startPlugin(plugin);
})
}
function startPlugin(plugin) {
/*
Start an individual plugin.
*/
logger(`Starting plugin "${plugin.name}". ⚡`);
plugin.start?.(config);
const pyPluginStarted = new CustomEvent("py-plugin-started", {detail: { config: config, plugin: plugin}});
document.dispatchEvent(pyPluginStarted);
}
function loadFiles() {
/*
Download and add the config.files into the local filesystem accessible
by the interpreter.
*/
// Holds the promises used to fetch the content of the files.
const pendingDownloads = [];
if (config.files) {
// Iterate the path and associated url (pointing at the content).
for (let path in config.files) {
let url = config.files[path];
logger(`Fetching file "${path}" from: ${url} 📡`);
// Create a new promise representing the fetch call.
const filePromise = fetch(url);
// Ensure the response is handled in the right way.
filePromise.then(response => {
if (response.ok) {
response.text().then(content => {
if (interpreterReady) {
// The interpreter exists, so just add the file to
// its filesystem.
const pyFileFetched = new CustomEvent("py-file-fetched", {detail: { path: path, content: content}});
document.dispatchEvent(pyFileFetched);
} else {
// No interpreter (yet), so push onto the
// filesToLoad queue so they're copied over
// once the interpreter becomes available.
filesToLoad.push({
path: path,
content: content
});
}
})
} else {
// Abort.
throw `💥 Cannot load file from "${url}"`;
}
});
// Add the promise to the pendingDownloads.
pendingDownloads.push(filePromise);
}
}
// A meta-promise that resolves when all the fetch promises have
// successfully resolved, then sets the filesLoaded flag, dispatches
// the "py-files-loaded" event and checks if MicroPyScript has finished
// setup.
Promise.all(pendingDownloads).then((values) => {
filesLoaded = true;
if (values.length > 0) {
logger(`${values.length} file[s] downloaded, copying to filesystem. 📥`);
} else {
logger(`No files to download, nothing to do on filesystem. 📥`);
}
const pyFilesLoaded = new CustomEvent("py-files-loaded");
document.dispatchEvent(pyFilesLoaded);
finished();
})
}
function onFileFetched(e) {
/*
Save the file's content to the path on the interpreter's local filesystem.
*/
interpreter.addFile(e.detail.path, e.detail.content);
logger(`Saved file "${e.detail.path}" to file system. 💾`);
}
function loadInterpreter() {
/*
Given a configuration state, load the interpreter specified therein and
dispatch a py-interpreter-loaded event when done.
TL;DR - a new script tag with the correct src is added to the head.
*/
if(!interpreters.hasOwnProperty(config.interpreter)) {
throw `💥 Unknown interpreter: "${config.interpreter}" (known interpreters: ${Object.keys(interpreters)})`;
}
const interpreterElement = document.createElement("script");
interpreterElement.src = interpreters[config.interpreter.toLowerCase()].url;
interpreterElement.onload = function(e) {
logger(`Interpreter "${config.interpreter}" loaded. 👍`);
const pyInterpreterLoaded = new CustomEvent("py-interpreter-loaded", {detail: config.interpreter});
document.dispatchEvent(pyInterpreterLoaded);
};
var head = document.getElementsByTagName('head')[0];
logger(`Loading interpreter "${config.interpreter}". 🚀`)
head.appendChild(interpreterElement);
}
function startInterpreter() {
/*
Configure and start the Python interpreter. Now that there is a interpreter,
use it to add any filesToLoad to the filesystem.
*/
interpreter = new interpreters[config.interpreter.toLowerCase()]();
interpreter.start(config);
}
function interpreterStarted() {
/*
The interpreter is ready to go, so flip the interpreterReady flag, step
through each registered plugin's onInterpreterReady method. Then check if
setup is finished.
*/
logger(`Interpreter started. 🎬`);
interpreterReady = true;
filesToLoad.forEach(function(file) {
const pyFileFetched = new CustomEvent("py-file-fetched", {detail: { path: file.path, content: file.content}});
document.dispatchEvent(pyFileFetched);
});
plugins.forEach(function(plugin) {
plugin.onInterpreterReady?.(config, interpreter);
});
finished();
}
function finished() {
/*
If both the interpreter and filesystem are in a ready state for evaluating
a user's code.
- Dispatch the "py-finished-setup" event to signal everything is
done.
*/
if (interpreterReady && filesLoaded) {
logger(`MicroPyScript finished setup. 🏁`);
const pyFinishedSetup = new CustomEvent("py-finished-setup", {detail: { interpreter: interpreter}});
document.dispatchEvent(pyFinishedSetup);
}
}
// The following functions coordinate the unfolding of MicroPyScript as
// various events are dispatched and state evolves to trigger the next
// steps.
//
// These functions are defined in the order they're roughly expected to
// be called through the life-cycle of the page, although this cannot be
// guaranteed for some of the functions.
function onPyConfigured(e) {
/*
Once MicroPyScript has loaded its configuration:
- Register the default plugins (currently only pyScriptTag), so
they can modify the config if required.
- Download any files that need to be copied onto the interpreter's
filesystem.
- Load the Python interpreter into the browser.
- Display the splash screen.
*/
registerPlugin(pyScriptTag);
loadFiles();
loadInterpreter();
splashOn();
}
function onInterpreterLoaded(e) {
/*
The interpreter has loaded over the network.
- Start the interpreter in this MicroPyScript context.
- Start the plugins to kick off plugin related aspects of the page.
*/
startInterpreter();
startPlugins();
}
function onInterpreterReady(e) {
/*
The interpreter is ready to evaluate scripts.
- Kick off all the pending things needed now it's all started up.
*/
interpreterStarted();
}
function onFinished(e) {
/*
Remove the splash screen.
*/
splashOff();
}
// Finally, return a function to start MicroPyScript.
return function() {
// An object to represent the MicroPyScript platform in the browser.
// What is eventually returned from the main() function.
const pyScript = {
get config() {
return config;
},
get plugins() {
return plugins;
},
get availableInterpreters() {
return interpreters;
},
get interpreter() {
return interpreter;
},
get isInterpreterReady() {
return interpreterReady;
},
registerPlugin: function(plugin) {
registerPlugin(plugin);
},
runPython: function(code) {
if (interpreterReady) {
interpreter.eval(code);
}
},
start: function() {
document.addEventListener("py-configured", onPyConfigured);
document.addEventListener("py-interpreter-loaded", onInterpreterLoaded);
document.addEventListener("py-file-fetched", onFileFetched);
document.addEventListener("py-interpreter-ready", onInterpreterReady);
document.addEventListener("py-finished-setup", onFinished);
loadConfig();
}
};
return pyScript;
}
}();
/******************************************************************************
Start MicroPyScript.
******************************************************************************/
window.pyScript = main();
if (!window.pyscriptTest) {
window.pyScript.start();
}