-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpyscript.js
670 lines (599 loc) Β· 24.6 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
"use strict";
/******************************************************************************
PyScript. π¬ππ
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 PyScript 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 runtime.
* Dispatch the following events to signal various changes in state or the
completion of tasks (such as starting the runtime).
- "py-configured", when configuration is processed.
- "py-plugin-registered", when a plugin is registered.
- "py-plugin-started", when a plugin is started.
- "py-runtime-loaded", when the runtime has been downloaded.
- "py-runtime-ready", when the runtime is ready to process Python.
- "py-file-fetched", when a file, to be added to the runtime's
filesystem, has been fetched from the network.
- "py-files-loaded", when all the files have been copied onto the
runtime's filesystem.
* Define, configure and start built-in PyScript plugins (e.g. the
<py-script> tag).
**************************************************************************/
const logger = function() {
/*
Really simple logging. Emoji π¬ππ highlights PyScript app logs. ;-)
*/
return Function.prototype.bind.call(console.log, console, "π¬ππ ", ...arguments);
}();
logger("Starting PyScript. ππ¬ππ");
class Runtime {
/*
Defines and encapsulates a runtime used by PyScript 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 runtime.
*/
return "";
}
static ready() {
/*
Dispatch the py-runtime-ready event (for when the runtime has
eventually started and is ready to evaluate code).
*/
const pyRuntimeReady = new CustomEvent("py-runtime-ready", this);
document.dispatchEvent(pyRuntimeReady);
}
static print(output) {
/*
Dispatch the polly-print event (for when output is printed).
*/
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 runtime. This is called once the runtime is loaded into
the browser.
*/
}
eval(script) {
/*
Use the runtime to evaluate the script.code.
*/
}
addFile(path, content) {
/*
Copy a file with the referenced path, and content, onto the local
filesystem available to the runtime.
*/
}
startREPL() {
/*
Start an interactive REPL session with the runtime.
*/
}
stdin(input) {
/*
Pass the input into the runtime's stdin.
*/
}
}
// The innerHTML of the default splash screen to show while PyScript is
// starting up. Currently the page is greyed out and the words
// "Loading PyScript...".
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:50%;color:white;">Loading PyScript... π¬π</div></div>';
/**************************************************************************
Built-in plugins and runtimes.
**************************************************************************/
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 runtime.
const pendingScripts = [];
// Eventually references the available runtime, once ready.
let availableRuntime = 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 runtime isn't ready
yet, or dispatch the py-eval-script event to signal to the runtime
it should evaluate the script.
*/
if (availableRuntime) {
// Runtime is ready, so evaluate the code.
const pyEvalScript = new CustomEvent("py-eval-script", {detail: e.detail});
document.dispatchEvent(pyEvalScript);
} else {
// No runtime, so add to pendingScripts queue, to be evaluated
// once the runtime is ready.
pendingScripts.push(e.detail);
}
}
function evaluateScript(e) {
/*
Given the runtime is ready AND the script is loaded,
evaluate the script with the runtime.
*/
logger("Evaluating code. π€\n" + e.detail.code);
availableRuntime.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 PyScript 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);
},
onRuntimeReady: function(config, runtime) {
availableRuntime = runtime;
// 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 MicroPythonRuntime extends Runtime {
/*
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 "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) {
Runtime.print(e.data);
}, false);
let mp_js_startup = Module['onRuntimeInitialized'];
Module["onRuntimeInitialized"] = async function() {
mp_js_startup();
mp_js_init(mp_memory);
Runtime.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);
});
}
}
// Default configuration settings for PyScript. 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 = {
"runtime": "micropython", // Numpty default.
"splash": defaultSplash, // loading message in grey overlay.
"files": {} // No files by default.
}
// Contains plugins to the PyScript context.
const plugins = [];
// Details of runtimes.
// Key: lowercase runtime name.
// Value: the class wrapping that version of the runtime.
const runtimes = {
"micropython": MicroPythonRuntime
}
// Files to be loaded to the filesystem once the runtime is loaded (but
// perhaps not yet ready).
const filesToLoad = [];
// Eventually references an instance of the Runtime class, representing the
// started runtime.
let runtime = 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 runtime is ready to evaluate scripts.
let runtimeReady = false;
// To hold a reference to the div containing the start-up splash screen
// displayed while PyScript starts up.
let splashElement = null;
function loadConfig() {
/*
Loads configuration for running PyScript 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 PyScript 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 PyScript is finished starting.
*/
splashElement.parentNode.removeChild(splashElement);
}
function registerPlugin(plugin) {
/*
Add a plugin to the PyScript context, after calling its configure
method.
*/
logger(`Registering plugin "${plugin.name}". π`);
plugin.configure?.(config);
plugins.push(plugin);
if (runtimeReady) {
startPlugin(plugin)
plugin.onRuntimeReady?.(config, runtime);
}
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 runtime.
*/
// 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 (runtime) {
// The runtime 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 runtime (yet), so push onto the
// filesToLoad queue so they're copied over
// once the runtime 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 PyScript has finished
// setup.
Promise.all(pendingDownloads).then((values) => {
filesLoaded = true;
if (values) {
logger(`All files downloaded, copying to 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 runtime's local filesystem.
*/
logger(`Saving file "${e.detail.path}" to file system. πΎ`);
runtime.addFile(e.detail.path, e.detail.content);
}
function loadRuntime() {
/*
Given a configuration state, load the runtime specified therein and
dispatch a py-runtime-loaded event when done.
TL;DR - a new script tag with the correct src is added to the head.
*/
if(!runtimes.hasOwnProperty(config.runtime)) {
throw `π₯ Unknown runtime: "${config.runtime}" (known runtimes: ${Object.keys(runtimes)})`;
}
const runtimeElement = document.createElement("script");
runtimeElement.src = runtimes[config.runtime.toLowerCase()].url;
runtimeElement.onload = function(e) {
logger(`Runtime "${config.runtime}" loaded. π`);
const pyRuntimeLoaded = new CustomEvent("py-runtime-loaded", {detail: config.runtime});
document.dispatchEvent(pyRuntimeLoaded);
};
var head = document.getElementsByTagName('head')[0];
logger(`Loading runtime "${config.runtime}". π`)
head.appendChild(runtimeElement);
}
function startRuntime() {
/*
Configure and start the Python runtime. Now that there is a runtime,
use it to add any filesToLoad to the filesystem.
*/
runtime = new runtimes[config.runtime.toLowerCase()]();
runtime.start(config);
filesToLoad.forEach(function(file) {
const pyFileFetched = new CustomEvent("py-file-fetched", {detail: { path: file.path, content: file.content}});
document.dispatchEvent(pyFileFetched);
});
}
function runtimeStarted() {
/*
The runtime is ready to go, so flip the runtimeReady flag, step
through each registered plugin's onRuntimeReady method. Then check if
setup is finished.
*/
logger(`Runtime started. π¬`);
runtimeReady = true;
plugins.forEach(function(plugin) {
plugin.onRuntimeReady?.(config, runtime);
});
finished();
}
function finished() {
/*
If both the runtime 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 (runtimeReady && filesLoaded) {
logger(`PyScript finished setup. π`);
const pyFinishedSetup = new CustomEvent("py-finished-setup", {detail: { runtime: runtime}});
document.dispatchEvent(pyFinishedSetup);
}
}
// The following functions coordinate the unfolding of PyScript 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 PyScript has loaded its configuration:
- Register the default plugins (currently only pyScriptTag), so
they can modify the config if required.
- Load the Python runtime into the browser.
- Display the splash screen.
*/
registerPlugin(pyScriptTag);
loadFiles();
loadRuntime();
splashOn();
}
function onRuntimeLoaded(e) {
/*
The runtime has loaded over the network.
- Start the runtime in this PyScript context.
- Start the plugins to kick off plugin related aspects of the page.
*/
startRuntime();
startPlugins();
}
function onRuntimeReady(e) {
/*
The runtime is ready to evaluate scripts.
- Kick off all the pending things needed now it's all started up.
*/
runtimeStarted();
}
function onFinished(e) {
/*
Remove the splash screen.
*/
splashOff();
}
// Finally, return a function to start PyScript.
return function() {
// Check to bypass loadConfig, for testing purposes.
document.addEventListener("py-configured", onPyConfigured);
document.addEventListener("py-runtime-loaded", onRuntimeLoaded);
document.addEventListener("py-file-fetched", onFileFetched);
document.addEventListener("py-runtime-ready", onRuntimeReady);
document.addEventListener("py-finished-setup", onFinished);
// An object to represent the PyScript platform in the browser. What
// is eventually returned from the main() function.
const pyScript = {
get config() {
return config;
},
get plugins() {
return plugins;
},
get availableRuntimes() {
return runtimes;
},
get runtime() {
return runtime;
},
get isRuntimeReady() {
return runtimeReady;
},
registerPlugin: function(plugin) {
registerPlugin(plugin);
},
runPython: function(code) {
if (runtimeReady) {
runtime.eval(code);
}
},
start: function() {
loadConfig();
}
};
return pyScript;
}
}();
/******************************************************************************
Start PyScript.
******************************************************************************/
window.pyScript = main();
if (!window.pyscriptTest) {
window.pyScript.start();
}