Skip to content
This repository was archived by the owner on Sep 21, 2023. It is now read-only.

Commit 0ba4ca8

Browse files
committed
Minor tidy-ups to help with upcoming refactoring.
1 parent e11161b commit 0ba4ca8

File tree

2 files changed

+68
-21
lines changed

2 files changed

+68
-21
lines changed

customtags.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"use strict";
2+
/******************************************************************************
3+
An example of how to do a custom "third party" plugin for PyScript.
4+
5+
Authors:
6+
- Nicholas H.Tollervey (ntollervey@anaconda.org)
7+
8+
Copyright (c) 2022 Anaconda Inc.
9+
10+
Licensed under the Apache License, Version 2.0 (the "License");
11+
you may not use this file except in compliance with the License.
12+
You may obtain a copy of the License at
13+
14+
http://www.apache.org/licenses/LICENSE-2.0
15+
16+
Unless required by applicable law or agreed to in writing, software
17+
distributed under the License is distributed on an "AS IS" BASIS,
18+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19+
See the License for the specific language governing permissions and
20+
limitations under the License.
21+
******************************************************************************/
22+
23+
class PyREPLTag extends Plugin {
24+
/*
25+
Adds a REPL to the DOM. The REPL session is only initialised when the
26+
runtime is ready. The content of the REPL is inserted in the following
27+
arrangement of tags:
28+
29+
<pre class="pyscriptREPL"><code>
30+
</code></pre>
31+
32+
No styling is provided by this plugin (the browser defaults for this
33+
arrangement of tags will make it look like a TTY session). Bespoke CSS
34+
should use the pyscriptREPL class to attach styling.
35+
*/
36+
start(config) {
37+
// Define the py-repl element.
38+
class PyREPL extends HTMLElement {
39+
connectedCallback() {
40+
/*
41+
Create a shadow DOM with the expected child elements and event
42+
handlers defined in it.
43+
*/
44+
const shadow = this.attachShadow({ mode: "open" });
45+
const pre = document.createElement("pre");
46+
pre.setAttribute("class", "pyscriptREPL");
47+
const code = document.createElement("code");
48+
pre.appendChild(code);
49+
shadow.appendChild(pre);
50+
}
51+
}
52+
customElements.define("py-repl", PyREPL);
53+
}
54+
}

pyscript.js

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ limitations under the License.
2929
/******************************************************************************
3030
Base classes and constants.
3131
******************************************************************************/
32-
class Plugin {
32+
class PyScriptPlugin {
3333
/*
3434
Defines a "plugin" in PyScript.
3535
*/
@@ -91,7 +91,7 @@ class Runtime {
9191
Dispatch the py-runtime-ready event (for when the runtime has
9292
eventually started and is ready to evaluate code).
9393
*/
94-
const pyRuntimeReady = new CustomEvent("py-runtime-ready");
94+
const pyRuntimeReady = new CustomEvent("py-runtime-ready", this);
9595
document.dispatchEvent(pyRuntimeReady);
9696
}
9797

@@ -124,14 +124,15 @@ class Runtime {
124124

125125

126126
// The innerHTML of the default splash screen to show while PyScript is
127-
// starting up. Currently a simple SVG animation above the word "PyScript".
127+
// starting up. Currently the page is greyed out and the words
128+
// "Loading PyScript...".
128129
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:99999;"><div style="position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);-ms-transform:translate(-50%,-50%);color:white;font-family:monospace;font-size:10px;">Loading PyScript...</div></div>';
129130

130131

131132
/******************************************************************************
132133
Built-in plugins and runtimes.
133134
******************************************************************************/
134-
class PyScriptTag extends Plugin {
135+
class PyScriptTag extends PyScriptPlugin {
135136
start(config) {
136137
// Define the PyScript element.
137138
class PyScript extends HTMLElement {
@@ -268,13 +269,11 @@ class PyodideRuntime extends Runtime {
268269
The core PyScript app definition.
269270
******************************************************************************/
270271
const main = function() {
271-
// Used to measure start-up times.
272-
const start = new Date();
273272
// Really simple logging. Emoji 🐍 highlights PyScript app logs. ;-)
274273
const logger = function() {
275274
return Function.prototype.bind.call(console.log, console, "🐍 ", ...arguments);
276275
}();
277-
logger("Starting PyScript. 👋", start);
276+
logger("Starting PyScript. 👋");
278277

279278
// Default configuration settings for PyScript. These may be overridden by
280279
// the app.loadConfig function.
@@ -301,8 +300,6 @@ const main = function() {
301300
"cpython": CPythonRuntime,
302301
"pyodide": PyodideRuntime
303302
}
304-
// Default to smallest/fastest runtime.
305-
runtimes["default"] = runtimes["micropython"]
306303

307304
// Eventually references an instance of the Runtime class, representing the
308305
// started runtime.
@@ -377,28 +374,25 @@ const main = function() {
377374
378375
TL;DR - a new script tag with the correct src is added to the head.
379376
*/
380-
const runtimeName = config.runtime ? config.runtime : "default";
381-
if(!runtimes.hasOwnProperty(runtimeName)) {
382-
throw `💥 Unknown runtime: "${runtimeName}" (known runtimes: ${Object.keys(runtimes)})`;
377+
if(!runtimes.hasOwnProperty(config.runtime)) {
378+
throw `💥 Unknown runtime: "${config.runtime}" (known runtimes: ${Object.keys(runtimes)})`;
383379
}
384380
const runtimeElement = document.createElement("script");
385-
runtimeElement.src = runtimes[runtimeName.toLowerCase()].url;
381+
runtimeElement.src = runtimes[config.runtime.toLowerCase()].url;
386382
runtimeElement.onload = function(e) {
387-
let duration = new Date() - start;
388-
logger(`Runtime "${runtimeName}" loaded (${duration}ms). 👍`);
389-
const pyRuntimeLoaded = new CustomEvent("py-runtime-loaded", {detail: runtimeName});
383+
logger(`Runtime "${config.runtime}" loaded. 👍`);
384+
const pyRuntimeLoaded = new CustomEvent("py-runtime-loaded", {detail: config.runtime});
390385
document.dispatchEvent(pyRuntimeLoaded);
391386
};
392387
var head = document.getElementsByTagName('head')[0];
393-
logger(`Loading runtime "${runtimeName}". 🚀`)
388+
logger(`Loading runtime "${config.runtime}". 🚀`)
394389
head.appendChild(runtimeElement);
395390
},
396391
startRuntime: function() {
397392
/*
398393
Congigure and start the Python runtime.
399394
*/
400-
const runtimeName = config.runtime ? config.runtime : "default";
401-
runtime = new runtimes[runtimeName.toLowerCase()]();
395+
runtime = new runtimes[config.runtime.toLowerCase()]();
402396
runtime.start(config);
403397
},
404398
runtimeStarted: function() {
@@ -407,8 +401,7 @@ const main = function() {
407401
through each registered plugin's onRuntimeReady method, and begin
408402
evaluating any code in the pendingScripts queue.
409403
*/
410-
let duration = new Date() - start;
411-
logger(`Runtime started (${duration}ms). 🎬`);
404+
logger(`Runtime started. 🎬`);
412405
runtimeReady = true;
413406
plugins.forEach(function(plugin) {
414407
plugin.onRuntimeReady(config, runtime);

0 commit comments

Comments
 (0)