10000
We read every piece of feedback, and take your input very seriously.
1 parent 6a3e283 commit 136e954Copy full SHA for 136e954
pyscript.core/package-lock.json
pyscript.core/package.json
@@ -1,6 +1,6 @@
1
{
2
"name": "@pyscript/core",
3
- "version": "0.3.9",
+ "version": "0.3.10",
4
"type": "module",
5
"description": "PyScript",
6
"module": "./index.js",
pyscript.core/src/core.js
@@ -26,33 +26,9 @@ import { ErrorCode } from "./exceptions.js";
26
import { robustFetch as fetch, getText } from "./fetch.js";
27
import { hooks, main, worker, codeFor, createFunction } from "./hooks.js";
28
29
-// allows lazy element features on code evaluation
30
-let currentElement;
31
-
32
// generic helper to disambiguate between custom element and script
33
const isScript = ({ tagName }) => tagName === "SCRIPT";
34
35
-let shouldRegister = true;
36
-const registerModule = ({ XWorker: $XWorker, interpreter, io }) => {
37
- // automatically use the pyscript stderr (when/if defined)
38
- // this defaults to console.error
39
- function PyWorker(...args) {
40
- const worker = $XWorker(...args);
41
- worker.onerror = ({ error }) => io.stderr(error);
42
- return worker;
43
- }
44
45
- // enrich the Python env with some JS utility for main
46
- interpreter.registerJsModule("_pyscript", {
47
- PyWorker,
48
- get target() {
49
- return isScript(currentElement)
50
- ? currentElement.target.id
51
- : currentElement.id;
52
- },
53
- });
54
-};
55
56
// avoid multiple initialization of the same library
57
const [
58
@@ -118,6 +94,36 @@ for (const [TYPE, interpreter] of TYPES) {
118
94
return code;
119
95
};
120
96
97
+ // register once any interpreter
98
+ let alreadyRegistered = false;
99
+
100
+ // allows lazy element features on code evaluation
101
+ let currentElement;
102
103
+ const registerModule = ({ XWorker, interpreter, io }) => {
104
+ // avoid multiple registration of the same interpreter
105
+ if (alreadyRegistered) return;
106
+ alreadyRegistered = true;
107
108
+ // automatically use the pyscript stderr (when/if defined)
109
+ // this defaults to console.error
110
+ function PyWorker(...args) {
111
+ const worker = XWorker(...args);
112
+ worker.onerror = ({ error }) => io.stderr(error);
113
+ return worker;
114
+ }
115
116
+ // enrich the Python env with some JS utility for main
117
+ interpreter.registerJsModule("_pyscript", {
+ PyWorker,
+ get target() {
+ return isScript(currentElement)
121
+ ? currentElement.target.id
122
+ : currentElement.id;
123
+ },
124
+ });
125
+ };
126
127
// define the module as both `<script type="py">` and `<py-script>`
128
// but only if the config didn't throw an error
129
if (!error) {
@@ -133,10 +139,7 @@ for (const [TYPE, interpreter] of TYPES) {
133
139
main: {
134
140
...codeFor(main),
135
141
async onReady(wrap, element) {
136
- if (shouldRegister) {
137
- shouldRegister = false;
138
- registerModule(wrap);
142
+ registerModule(wrap);
143
144
// allows plugins to do whatever they want with the element
145
// before regular stuff happens in here
@@ -320,7 +323,7 @@ for (const [TYPE, interpreter] of TYPES) {
320
323
function PyWorker(file, options) {
321
324
const hooks = hooked.get("py");
322
325
// this propagates pyscript worker hooks without needing a pyscript
- // bootstrap + it passes arguments and enforces `pyodide`
326
+ // bootstrap + it passes arguments and it defaults to `pyodide`
327
// as the interpreter to use in the worker, as all hooks assume that
328
// and as `pyodide` is the only default interpreter that can deal with
329
// all the features we need to deliver pyscript out there.
pyscript.core/test/multi.html
@@ -0,0 +1,20 @@
+<!doctype html>
+<html lang="en">
+ <head>
+ <meta charset="UTF-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
+ <script type="module" src="../dist/core.js"></script>
7
+ </head>
8
+ <body>
9
+ <script type="mpy">
10
+ from pyscript import document
11
+ import sys
12
+ document.body.append(sys.version)
13
+ </script>
14
+ <script type="py">
15
16
17
18
19
+ </body>
20
+</html>