@pyscript/core 0.1.22 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/dist/core.css +1 -1
  2. package/dist/core.js +2 -2
  3. package/dist/core.js.map +1 -1
  4. package/dist/error-e4fe78fd.js +2 -0
  5. package/dist/error-e4fe78fd.js.map +1 -0
  6. package/package.json +2 -2
  7. package/src/core.css +3 -1
  8. package/src/core.js +170 -161
  9. package/src/plugins/error.js +2 -2
  10. package/src/stdlib/pyscript/__init__.py +10 -1
  11. package/src/stdlib/pyscript/display.py +7 -0
  12. package/src/stdlib/pyscript/util.py +3 -4
  13. package/src/stdlib/pyscript.js +3 -3
  14. package/dist/error-87e0706c.js +0 -2
  15. package/dist/error-87e0706c.js.map +0 -1
  16. package/tests/integration/__init__.py +0 -0
  17. package/tests/integration/conftest.py +0 -184
  18. package/tests/integration/support.py +0 -1038
  19. package/tests/integration/test_00_support.py +0 -495
  20. package/tests/integration/test_01_basic.py +0 -353
  21. package/tests/integration/test_02_display.py +0 -452
  22. package/tests/integration/test_03_element.py +0 -303
  23. package/tests/integration/test_assets/line_plot.png +0 -0
  24. package/tests/integration/test_assets/tripcolor.png +0 -0
  25. package/tests/integration/test_async.py +0 -197
  26. package/tests/integration/test_event_handling.py +0 -193
  27. package/tests/integration/test_importmap.py +0 -66
  28. package/tests/integration/test_interpreter.py +0 -98
  29. package/tests/integration/test_plugins.py +0 -419
  30. package/tests/integration/test_py_config.py +0 -294
  31. package/tests/integration/test_py_repl.py +0 -663
  32. package/tests/integration/test_py_terminal.py +0 -270
  33. package/tests/integration/test_runtime_attributes.py +0 -64
  34. package/tests/integration/test_script_type.py +0 -121
  35. package/tests/integration/test_shadow_root.py +0 -33
  36. package/tests/integration/test_splashscreen.py +0 -124
  37. package/tests/integration/test_stdio_handling.py +0 -370
  38. package/tests/integration/test_style.py +0 -47
  39. package/tests/integration/test_warnings_and_banners.py +0 -32
  40. package/tests/integration/test_zz_examples.py +0 -419
  41. package/tests/integration/test_zzz_docs_snippets.py +0 -305
@@ -0,0 +1,2 @@
1
+ import{hooks as e}from"./core.js";function r(e){const r=document.createElement("div");r.className="py-error",r.textContent=e,r.style.cssText="\n border: 1px solid red;\n background: #ffdddd;\n color: black;\n font-family: courier, monospace;\n white-space: pre;\n overflow-x: auto;\n padding: 8px;\n margin-top: 8px;\n ",document.body.append(r)}e.onInterpreterReady.add((function n(t){e.onInterpreterReady.delete(n);const{stderr:o}=t.io;t.io.stderr=(e,...n)=>(r(e.message||e),o(e,...n)),addEventListener("error",(({message:e})=>{e.startsWith("Uncaught PythonError")&&r(e)}))}));export{r as notify};
2
+ //# sourceMappingURL=error-e4fe78fd.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error-e4fe78fd.js","sources":["../src/plugins/error.js"],"sourcesContent":["// PyScript Error Plugin\nimport { hooks } from \"../core.js\";\n\nhooks.onInterpreterReady.add(function override(pyScript) {\n // be sure this override happens only once\n hooks.onInterpreterReady.delete(override);\n\n // trap generic `stderr` to propagate to it regardless\n const { stderr } = pyScript.io;\n\n // override it with our own logic\n pyScript.io.stderr = (error, ...rest) => {\n notify(error.message || error);\n // let other plugins or stderr hook, if any, do the rest\n return stderr(error, ...rest);\n };\n\n // be sure uncaught Python errors are also visible\n addEventListener(\"error\", ({ message }) => {\n if (message.startsWith(\"Uncaught PythonError\")) notify(message);\n });\n});\n\n// Error hook utilities\n\n// Custom function to show notifications\nexport function notify(message) {\n const div = document.createElement(\"div\");\n div.className = \"py-error\";\n div.textContent = message;\n div.style.cssText = `\n border: 1px solid red;\n background: #ffdddd;\n color: black;\n font-family: courier, monospace;\n white-space: pre;\n overflow-x: auto;\n padding: 8px;\n margin-top: 8px;\n `;\n document.body.append(div);\n}\n"],"names":["notify","message","div","document","createElement","className","textContent","style","cssText","body","append","hooks","onInterpreterReady","add","override","pyScript","delete","stderr","io","error","rest","addEventListener","startsWith"],"mappings":"kCA0BO,SAASA,EAAOC,GACnB,MAAMC,EAAMC,SAASC,cAAc,OACnCF,EAAIG,UAAY,WAChBH,EAAII,YAAcL,EAClBC,EAAIK,MAAMC,QAAU,6MAUpBL,SAASM,KAAKC,OAAOR,EACzB,CAtCAS,EAAMC,mBAAmBC,KAAI,SAASC,EAASC,GAE3CJ,EAAMC,mBAAmBI,OAAOF,GAGhC,MAAMG,OAAEA,GAAWF,EAASG,GAG5BH,EAASG,GAAGD,OAAS,CAACE,KAAUC,KAC5BpB,EAAOmB,EAAMlB,SAAWkB,GAEjBF,EAAOE,KAAUC,IAI5BC,iBAAiB,SAAS,EAAGpB,cACrBA,EAAQqB,WAAW,yBAAyBtB,EAAOC,EAAQ,GAEvE"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pyscript/core",
3
- "version": "0.1.22",
3
+ "version": "0.2.1",
4
4
  "type": "module",
5
5
  "description": "PyScript",
6
6
  "module": "./index.js",
@@ -33,7 +33,7 @@
33
33
  "dependencies": {
34
34
  "@ungap/with-resolvers": "^0.1.0",
35
35
  "basic-devtools": "^0.1.6",
36
- "polyscript": "^0.3.10"
36
+ "polyscript": "^0.4.2"
37
37
  },
38
38
  "devDependencies": {
39
39
  "@rollup/plugin-node-resolve": "^15.2.1",
package/src/core.css CHANGED
@@ -1,4 +1,6 @@
1
1
  py-script,
2
- py-config {
2
+ py-config,
3
+ mpy-script,
4
+ mpy-config {
3
5
  display: none;
4
6
  }
package/src/core.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /*! (c) PyScript Development Team */
2
2
 
3
3
  import "@ungap/with-resolvers";
4
- import { define, XWorker } from "polyscript";
4
+ import { INVALID_CONTENT, define, XWorker } from "polyscript";
5
5
 
6
6
  // TODO: this is not strictly polyscript related but handy ... not sure
7
7
  // we should factor this utility out a part but this works anyway.
@@ -15,16 +15,15 @@ import stdlib from "./stdlib.js";
15
15
  import { config, plugins, error } from "./config.js";
16
16
  import { robustFetch as fetch, getText } from "./fetch.js";
17
17
 
18
- const { assign, defineProperty, entries } = Object;
19
-
20
- const TYPE = "py";
18
+ const { assign, defineProperty } = Object;
21
19
 
22
20
  // allows lazy element features on code evaluation
23
21
  let currentElement;
24
22
 
25
- // create a unique identifier when/if needed
26
- let id = 0;
27
- const getID = (prefix = TYPE) => `${prefix}-${id++}`;
23
+ const TYPES = new Map([
24
+ ["py", "pyodide"],
25
+ ["mpy", "micropython"],
26
+ ]);
28
27
 
29
28
  // generic helper to disambiguate between custom element and script
30
29
  const isScript = ({ tagName }) => tagName === "SCRIPT";
@@ -41,65 +40,11 @@ const after = () => {
41
40
  delete document.currentScript;
42
41
  };
43
42
 
44
- /**
45
- * Some content that might contain Python/JS comments only.
46
- * @param {string} text some content to evaluate
47
- * @returns {boolean}
48
- */
49
- const hasCommentsOnly = (text) =>
50
- !text
51
- .replace(/\/\*[\s\S]*?\*\//g, "")
52
- .replace(/^\s*(?:\/\/|#).*/gm, "")
53
- .trim();
54
-
55
- /**
56
- *
57
- * @param {Element} scriptOrPyScript the element with possible `src` or `worker` content
58
- * @returns {boolean}
59
- */
60
- const hasAmbiguousContent = (
61
- io,
62
- { localName, textContent, attributes: { src, worker } },
63
- ) => {
64
- // any `src` or a non-empty `worker` attribute + not just comments
65
- if ((src || worker?.value) && !hasCommentsOnly(textContent)) {
66
- io.stderr(
67
- `(${ErrorCode.CONFLICTING_CODE}) a ${localName} tag has content shadowed by attributes`,
68
- );
69
- return true;
70
- }
71
- return false;
72
- };
73
-
74
- /**
75
- * Given a generic DOM Element, tries to fetch the 'src' attribute, if present.
76
- * It either throws an error if the 'src' can't be fetched or it returns a fallback
77
- * content as source.
78
- */
79
- const fetchSource = async (tag, io, asText) => {
80
- if (tag.hasAttribute("src")) {
81
- try {
82
- return await fetch(tag.getAttribute("src")).then(getText);
83
- } catch (error) {
84
- io.stderr(error);
85
- }
86
- }
87
-
88
- if (asText) return dedent(tag.textContent);
89
-
90
- console.warn(
91
- `Deprecated: use <script type="${TYPE}"> for an always safe content parsing:\n`,
92
- tag.innerHTML,
93
- );
94
-
95
- return dedent(tag.innerHTML);
96
- };
97
-
98
43
  // common life-cycle handlers for any node
99
- const bootstrapNodeAndPlugins = (pyodide, element, callback, hook) => {
44
+ const bootstrapNodeAndPlugins = (wrap, element, callback, hook) => {
100
45
  // make it possible to reach the current target node via Python
101
46
  callback(element);
102
- for (const fn of hooks[hook]) fn(pyodide, element);
47
+ for (const fn of hooks[hook]) fn(wrap, element);
103
48
  };
104
49
 
105
50
  let shouldRegister = true;
@@ -158,116 +103,180 @@ const workerHooks = {
158
103
  [...hooks.codeAfterRunWorkerAsync].map(dedent).join("\n"),
159
104
  };
160
105
 
161
- // define the module as both `<script type="py">` and `<py-script>`
162
- // but only if the config didn't throw an error
163
- error ||
164
- define(TYPE, {
165
- config,
166
- env: `${TYPE}-script`,
167
- interpreter: "pyodide",
168
- ...workerHooks,
169
- onWorkerReady(_, xworker) {
170
- assign(xworker.sync, sync);
171
- },
172
- onBeforeRun(pyodide, element) {
173
- currentElement = element;
174
- bootstrapNodeAndPlugins(pyodide, element, before, "onBeforeRun");
175
- },
176
- onBeforeRunAsync(pyodide, element) {
177
- currentElement = element;
178
- bootstrapNodeAndPlugins(
179
- pyodide,
180
- element,
181
- before,
182
- "onBeforeRunAsync",
183
- );
184
- },
185
- onAfterRun(pyodide, element) {
186
- bootstrapNodeAndPlugins(pyodide, element, after, "onAfterRun");
187
- },
188
- onAfterRunAsync(pyodide, element) {
189
- bootstrapNodeAndPlugins(pyodide, element, after, "onAfterRunAsync");
190
- },
191
- async onInterpreterReady(pyodide, element) {
192
- if (shouldRegister) {
193
- shouldRegister = false;
194
- registerModule(pyodide);
106
+ for (const [TYPE, interpreter] of TYPES) {
107
+ // create a unique identifier when/if needed
108
+ let id = 0;
109
+ const getID = (prefix = TYPE) => `${prefix}-${id++}`;
110
+
111
+ /**
112
+ * Given a generic DOM Element, tries to fetch the 'src' attribute, if present.
113
+ * It either throws an error if the 'src' can't be fetched or it returns a fallback
114
+ * content as source.
115
+ */
116
+ const fetchSource = async (tag, io, asText) => {
117
+ if (tag.hasAttribute("src")) {
118
+ try {
119
+ return await fetch(tag.getAttribute("src")).then(getText);
120
+ } catch (error) {
121
+ io.stderr(error);
195
122
  }
123
+ }
196
124
 
197
- // ensure plugins are bootstrapped already
198
- if (plugins) await plugins;
199
-
200
- // allows plugins to do whatever they want with the element
201
- // before regular stuff happens in here
202
- for (const callback of hooks.onInterpreterReady)
203
- callback(pyodide, element);
204
-
205
- if (isScript(element)) {
206
- if (hasAmbiguousContent(pyodide.io, element)) return;
207
- const {
208
- attributes: { async: isAsync, target },
209
- } = element;
210
- const hasTarget = !!target?.value;
211
- const show = hasTarget
212
- ? queryTarget(target.value)
213
- : document.createElement("script-py");
214
-
215
- if (!hasTarget) {
216
- const { head, body } = document;
217
- if (head.contains(element)) body.append(show);
218
- else element.after(show);
219
- }
220
- if (!show.id) show.id = getID();
125
+ if (asText) return dedent(tag.textContent);
221
126
 
222
- // allows the code to retrieve the target element via
223
- // document.currentScript.target if needed
224
- defineProperty(element, "target", { value: show });
127
+ console.warn(
128
+ `Deprecated: use <script type="${TYPE}"> for an always safe content parsing:\n`,
129
+ tag.innerHTML,
130
+ );
225
131
 
226
- // notify before the code runs
227
- dispatch(element, TYPE);
228
- pyodide[`run${isAsync ? "Async" : ""}`](
229
- await fetchSource(element, pyodide.io, true),
132
+ return dedent(tag.innerHTML);
133
+ };
134
+
135
+ // define the module as both `<script type="py">` and `<py-script>`
136
+ // but only if the config didn't throw an error
137
+ if (!error) {
138
+ // possible early errors sent by polyscript
139
+ const errors = new Map();
140
+
141
+ define(TYPE, {
142
+ config,
143
+ interpreter,
144
+ env: `${TYPE}-script`,
145
+ onerror(error, element) {
146
+ errors.set(element, error);
147
+ },
148
+ ...workerHooks,
149
+ onWorkerReady(_, xworker) {
150
+ assign(xworker.sync, sync);
151
+ },
152
+ onBeforeRun(wrap, element) {
153
+ currentElement = element;
154
+ bootstrapNodeAndPlugins(wrap, element, before, "onBeforeRun");
155
+ },
156
+ onBeforeRunAsync(wrap, element) {
157
+ currentElement = element;
158
+ bootstrapNodeAndPlugins(
159
+ wrap,
160
+ element,
161
+ before,
162
+ "onBeforeRunAsync",
230
163
  );
231
- } else {
232
- // resolve PyScriptElement to allow connectedCallback
233
- element._pyodide.resolve(pyodide);
234
- }
235
- console.debug("[pyscript/main] PyScript Ready");
236
- },
237
- });
164
+ },
165
+ onAfterRun(wrap, element) {
166
+ bootstrapNodeAndPlugins(wrap, element, after, "onAfterRun");
167
+ },
168
+ onAfterRunAsync(wrap, element) {
169
+ bootstrapNodeAndPlugins(
170
+ wrap,
171
+ element,
172
+ after,
173
+ "onAfterRunAsync",
174
+ );
175
+ },
176
+ async onInterpreterReady(wrap, element) {
177
+ if (shouldRegister) {
178
+ shouldRegister = false;
179
+ registerModule(wrap);
180
+ }
238
181
 
239
- class PyScriptElement extends HTMLElement {
240
- constructor() {
241
- assign(super(), {
242
- _pyodide: Promise.withResolvers(),
243
- srcCode: "",
244
- executed: false,
182
+ // ensure plugins are bootstrapped already
183
+ if (plugins) await plugins;
184
+
185
+ // allows plugins to do whatever they want with the element
186
+ // before regular stuff happens in here
187
+ for (const callback of hooks.onInterpreterReady)
188
+ callback(wrap, element);
189
+
190
+ // now that all possible plugins are configured,
191
+ // bail out if polyscript encountered an error
192
+ if (errors.has(element)) {
193
+ let { message } = errors.get(element);
194
+ errors.delete(element);
195
+ const clone = message === INVALID_CONTENT;
196
+ message = `(${ErrorCode.CONFLICTING_CODE}) ${message} for `;
197
+ message += element.cloneNode(clone).outerHTML;
198
+ wrap.io.stderr(message);
199
+ return;
200
+ }
201
+
202
+ if (isScript(element)) {
203
+ const {
204
+ attributes: { async: isAsync, target },
205
+ } = element;
206
+ const hasTarget = !!target?.value;
207
+ const show = hasTarget
208
+ ? queryTarget(target.value)
209
+ : document.createElement("script-py");
210
+
211
+ if (!hasTarget) {
212
+ const { head, body } = document;
213
+ if (head.contains(element)) body.append(show);
214
+ else element.after(show);
215
+ }
216
+ if (!show.id) show.id = getID();
217
+
218
+ // allows the code to retrieve the target element via
219
+ // document.currentScript.target if needed
220
+ defineProperty(element, "target", { value: show });
221
+
222
+ // notify before the code runs
223
+ dispatch(element, TYPE);
224
+ wrap[`run${isAsync ? "Async" : ""}`](
225
+ await fetchSource(element, wrap.io, true),
226
+ );
227
+ } else {
228
+ // resolve PyScriptElement to allow connectedCallback
229
+ element._wrap.resolve(wrap);
230
+ }
231
+ console.debug("[pyscript/main] PyScript Ready");
232
+ },
245
233
  });
246
234
  }
247
- get id() {
248
- return super.id || (super.id = getID());
249
- }
250
- set id(value) {
251
- super.id = value;
252
- }
253
- async connectedCallback() {
254
- if (!this.executed) {
255
- this.executed = true;
256
- const { io, run, runAsync } = await this._pyodide.promise;
257
- if (hasAmbiguousContent(io, this)) return;
258
- const runner = this.hasAttribute("async") ? runAsync : run;
259
- this.srcCode = await fetchSource(this, io, !this.childElementCount);
260
- this.replaceChildren();
261
- // notify before the code runs
262
- dispatch(this, TYPE);
263
- runner(this.srcCode);
264
- this.style.display = "block";
235
+
236
+ class PyScriptElement extends HTMLElement {
237
+ constructor() {
238
+ assign(super(), {
239
+ _wrap: Promise.withResolvers(),
240
+ srcCode: "",
241
+ executed: false,
242
+ });
243
+ }
244
+ get _pyodide() {
245
+ // TODO: deprecate this hidden attribute already
246
+ // currently used by integration tests
247
+ return this._wrap;
248
+ }
249
+ get id() {
250
+ return super.id || (super.id = getID());
251
+ }
252
+ set id(value) {
253
+ super.id = value;
254
+ }
255
+ async connectedCallback() {
256
+ if (!this.executed) {
257
+ this.executed = true;
258
+ const { io, run, runAsync } = await this._wrap.promise;
259
+ const runner = this.hasAttribute("async") ? runAsync : run;
260
+ this.srcCode = await fetchSource(
261
+ this,
262
+ io,
263
+ !this.childElementCount,
264
+ );
265
+ this.replaceChildren();
266
+ // notify before the code runs
267
+ dispatch(this, TYPE);
268
+ runner(this.srcCode);
269
+ this.style.display = "block";
270
+ }
265
271
  }
266
272
  }
273
+
274
+ // define py-script only if the config didn't throw an error
275
+ if (!error) customElements.define(`${TYPE}-script`, PyScriptElement);
267
276
  }
268
277
 
269
- // define py-script only if the config didn't throw an error
270
- error || customElements.define("py-script", PyScriptElement);
278
+ // TBD: I think manual worker cases are interesting in pyodide only
279
+ // so for the time being we should be fine with this export.
271
280
 
272
281
  /**
273
282
  * A `Worker` facade able to bootstrap on the worker thread only a PyScript module.
@@ -1,9 +1,9 @@
1
1
  // PyScript Error Plugin
2
2
  import { hooks } from "../core.js";
3
3
 
4
- hooks.onBeforeRun.add(function override(pyScript) {
4
+ hooks.onInterpreterReady.add(function override(pyScript) {
5
5
  // be sure this override happens only once
6
- hooks.onBeforeRun.delete(override);
6
+ hooks.onInterpreterReady.delete(override);
7
7
 
8
8
  // trap generic `stderr` to propagate to it regardless
9
9
  const { stderr } = pyScript.io;
@@ -31,4 +31,13 @@
31
31
 
32
32
  from pyscript.magic_js import RUNNING_IN_WORKER, window, document, sync
33
33
  from pyscript.display import HTML, display
34
- from pyscript.event_handling import when
34
+
35
+ try:
36
+ from pyscript.event_handling import when
37
+ except:
38
+ from pyscript.util import NotSupported
39
+
40
+ when = NotSupported(
41
+ "pyscript.when",
42
+ "pyscript.when currently not available with this interpreter"
43
+ )
@@ -150,5 +150,12 @@ def display(*values, target=None, append=True):
150
150
  target = current_target()
151
151
 
152
152
  element = document.getElementById(target)
153
+
154
+ # if element is a <script type="py">, it has a 'target' attribute which
155
+ # points to the visual element holding the displayed values. In that case,
156
+ # use that.
157
+ if element.tagName == 'SCRIPT' and hasattr(element, 'target'):
158
+ element = element.target
159
+
153
160
  for v in values:
154
161
  _write(element, v, append=append)
@@ -5,12 +5,11 @@ class NotSupported:
5
5
  """
6
6
 
7
7
  def __init__(self, name, error):
8
- # we set attributes using self.__dict__ to bypass the __setattr__
9
- self.__dict__['name'] = name
10
- self.__dict__['error'] = error
8
+ object.__setattr__(self, "name", name)
9
+ object.__setattr__(self, "error", error)
11
10
 
12
11
  def __repr__(self):
13
- return f'<NotSupported {self.name} [{self.error}]>'
12
+ return f"<NotSupported {self.name} [{self.error}]>"
14
13
 
15
14
  def __getattr__(self, attr):
16
15
  raise AttributeError(self.error)
@@ -1,11 +1,11 @@
1
1
  // ⚠️ This file is an artifact: DO NOT MODIFY
2
2
  export default {
3
3
  "pyscript": {
4
- "__init__.py": "# Some notes about the naming conventions and the relationship between various\n# similar-but-different names.\n#\n# import pyscript\n# this package contains the main user-facing API offered by pyscript. All\n# the names which are supposed be used by end users should be made\n# available in pyscript/__init__.py (i.e., this file)\n#\n# import _pyscript\n# this is an internal module implemented in JS. It is used internally by\n# the pyscript package, end users should not use it directly. For its\n# implementation, grep for `interpreter.registerJsModule(\"_pyscript\",\n# ...)` in core.js\n#\n# import js\n# this is the JS globalThis, as exported by pyodide and/or micropython's\n# FFIs. As such, it contains different things in the main thread or in a\n# worker.\n#\n# import pyscript.magic_js\n# this submodule abstracts away some of the differences between the main\n# thread and the worker. In particular, it defines `window` and `document`\n# in such a way that these names work in both cases: in the main thread,\n# they are the \"real\" objects, in the worker they are proxies which work\n# thanks to coincident.\n#\n# from pyscript import window, document\n# these are just the window and document objects as defined by\n# pyscript.magic_js. This is the blessed way to access them from pyscript,\n# as it works transparently in both the main thread and worker cases.\n\nfrom pyscript.magic_js import RUNNING_IN_WORKER, window, document, sync\nfrom pyscript.display import HTML, display\nfrom pyscript.event_handling import when\n",
5
- "display.py": "import base64\nimport html\nimport io\nimport re\n\nfrom pyscript.magic_js import document, window, current_target\n\n_MIME_METHODS = {\n \"__repr__\": \"text/plain\",\n \"_repr_html_\": \"text/html\",\n \"_repr_markdown_\": \"text/markdown\",\n \"_repr_svg_\": \"image/svg+xml\",\n \"_repr_png_\": \"image/png\",\n \"_repr_pdf_\": \"application/pdf\",\n \"_repr_jpeg_\": \"image/jpeg\",\n \"_repr_latex\": \"text/latex\",\n \"_repr_json_\": \"application/json\",\n \"_repr_javascript_\": \"application/javascript\",\n \"savefig\": \"image/png\",\n}\n\n\ndef _render_image(mime, value, meta):\n # If the image value is using bytes we should convert it to base64\n # otherwise it will return raw bytes and the browser will not be able to\n # render it.\n if isinstance(value, bytes):\n value = base64.b64encode(value).decode(\"utf-8\")\n\n # This is the pattern of base64 strings\n base64_pattern = re.compile(\n r\"^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$\"\n )\n # If value doesn't match the base64 pattern we should encode it to base64\n if len(value) > 0 and not base64_pattern.match(value):\n value = base64.b64encode(value.encode(\"utf-8\")).decode(\"utf-8\")\n\n data = f\"data:{mime};charset=utf-8;base64,{value}\"\n attrs = \" \".join(['{k}=\"{v}\"' for k, v in meta.items()])\n return f'<img src=\"{data}\" {attrs}></img>'\n\n\ndef _identity(value, meta):\n return value\n\n\n_MIME_RENDERERS = {\n \"text/plain\": html.escape,\n \"text/html\": _identity,\n \"image/png\": lambda value, meta: _render_image(\"image/png\", value, meta),\n \"image/jpeg\": lambda value, meta: _render_image(\"image/jpeg\", value, meta),\n \"image/svg+xml\": _identity,\n \"application/json\": _identity,\n \"application/javascript\": lambda value, meta: f\"<script>{value}<\\\\/script>\",\n}\n\n\nclass HTML:\n \"\"\"\n Wrap a string so that display() can render it as plain HTML\n \"\"\"\n\n def __init__(self, html):\n self._html = html\n\n def _repr_html_(self):\n return self._html\n\n\ndef _eval_formatter(obj, print_method):\n \"\"\"\n Evaluates a formatter method.\n \"\"\"\n if print_method == \"__repr__\":\n return repr(obj)\n elif hasattr(obj, print_method):\n if print_method == \"savefig\":\n buf = io.BytesIO()\n obj.savefig(buf, format=\"png\")\n buf.seek(0)\n return base64.b64encode(buf.read()).decode(\"utf-8\")\n return getattr(obj, print_method)()\n elif print_method == \"_repr_mimebundle_\":\n return {}, {}\n return None\n\n\ndef _format_mime(obj):\n \"\"\"\n Formats object using _repr_x_ methods.\n \"\"\"\n if isinstance(obj, str):\n return html.escape(obj), \"text/plain\"\n\n mimebundle = _eval_formatter(obj, \"_repr_mimebundle_\")\n if isinstance(mimebundle, tuple):\n format_dict, _ = mimebundle\n else:\n format_dict = mimebundle\n\n output, not_available = None, []\n for method, mime_type in reversed(_MIME_METHODS.items()):\n if mime_type in format_dict:\n output = format_dict[mime_type]\n else:\n output = _eval_formatter(obj, method)\n\n if output is None:\n continue\n elif mime_type not in _MIME_RENDERERS:\n not_available.append(mime_type)\n continue\n break\n if output is None:\n if not_available:\n window.console.warn(\n f\"Rendered object requested unavailable MIME renderers: {not_available}\"\n )\n output = repr(output)\n mime_type = \"text/plain\"\n elif isinstance(output, tuple):\n output, meta = output\n else:\n meta = {}\n return _MIME_RENDERERS[mime_type](output, meta), mime_type\n\n\ndef _write(element, value, append=False):\n html, mime_type = _format_mime(value)\n if html == \"\\\\n\":\n return\n\n if append:\n out_element = document.createElement(\"div\")\n element.append(out_element)\n else:\n out_element = element.lastElementChild\n if out_element is None:\n out_element = element\n\n if mime_type in (\"application/javascript\", \"text/html\"):\n script_element = document.createRange().createContextualFragment(html)\n out_element.append(script_element)\n else:\n out_element.innerHTML = html\n\n\ndef display(*values, target=None, append=True):\n if target is None:\n target = current_target()\n\n element = document.getElementById(target)\n for v in values:\n _write(element, v, append=append)\n",
4
+ "__init__.py": "# Some notes about the naming conventions and the relationship between various\n# similar-but-different names.\n#\n# import pyscript\n# this package contains the main user-facing API offered by pyscript. All\n# the names which are supposed be used by end users should be made\n# available in pyscript/__init__.py (i.e., this file)\n#\n# import _pyscript\n# this is an internal module implemented in JS. It is used internally by\n# the pyscript package, end users should not use it directly. For its\n# implementation, grep for `interpreter.registerJsModule(\"_pyscript\",\n# ...)` in core.js\n#\n# import js\n# this is the JS globalThis, as exported by pyodide and/or micropython's\n# FFIs. As such, it contains different things in the main thread or in a\n# worker.\n#\n# import pyscript.magic_js\n# this submodule abstracts away some of the differences between the main\n# thread and the worker. In particular, it defines `window` and `document`\n# in such a way that these names work in both cases: in the main thread,\n# they are the \"real\" objects, in the worker they are proxies which work\n# thanks to coincident.\n#\n# from pyscript import window, document\n# these are just the window and document objects as defined by\n# pyscript.magic_js. This is the blessed way to access them from pyscript,\n# as it works transparently in both the main thread and worker cases.\n\nfrom pyscript.magic_js import RUNNING_IN_WORKER, window, document, sync\nfrom pyscript.display import HTML, display\n\ntry:\n from pyscript.event_handling import when\nexcept:\n from pyscript.util import NotSupported\n\n when = NotSupported(\n \"pyscript.when\",\n \"pyscript.when currently not available with this interpreter\"\n )\n",
5
+ "display.py": "import base64\nimport html\nimport io\nimport re\n\nfrom pyscript.magic_js import document, window, current_target\n\n_MIME_METHODS = {\n \"__repr__\": \"text/plain\",\n \"_repr_html_\": \"text/html\",\n \"_repr_markdown_\": \"text/markdown\",\n \"_repr_svg_\": \"image/svg+xml\",\n \"_repr_png_\": \"image/png\",\n \"_repr_pdf_\": \"application/pdf\",\n \"_repr_jpeg_\": \"image/jpeg\",\n \"_repr_latex\": \"text/latex\",\n \"_repr_json_\": \"application/json\",\n \"_repr_javascript_\": \"application/javascript\",\n \"savefig\": \"image/png\",\n}\n\n\ndef _render_image(mime, value, meta):\n # If the image value is using bytes we should convert it to base64\n # otherwise it will return raw bytes and the browser will not be able to\n # render it.\n if isinstance(value, bytes):\n value = base64.b64encode(value).decode(\"utf-8\")\n\n # This is the pattern of base64 strings\n base64_pattern = re.compile(\n r\"^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$\"\n )\n # If value doesn't match the base64 pattern we should encode it to base64\n if len(value) > 0 and not base64_pattern.match(value):\n value = base64.b64encode(value.encode(\"utf-8\")).decode(\"utf-8\")\n\n data = f\"data:{mime};charset=utf-8;base64,{value}\"\n attrs = \" \".join(['{k}=\"{v}\"' for k, v in meta.items()])\n return f'<img src=\"{data}\" {attrs}></img>'\n\n\ndef _identity(value, meta):\n return value\n\n\n_MIME_RENDERERS = {\n \"text/plain\": html.escape,\n \"text/html\": _identity,\n \"image/png\": lambda value, meta: _render_image(\"image/png\", value, meta),\n \"image/jpeg\": lambda value, meta: _render_image(\"image/jpeg\", value, meta),\n \"image/svg+xml\": _identity,\n \"application/json\": _identity,\n \"application/javascript\": lambda value, meta: f\"<script>{value}<\\\\/script>\",\n}\n\n\nclass HTML:\n \"\"\"\n Wrap a string so that display() can render it as plain HTML\n \"\"\"\n\n def __init__(self, html):\n self._html = html\n\n def _repr_html_(self):\n return self._html\n\n\ndef _eval_formatter(obj, print_method):\n \"\"\"\n Evaluates a formatter method.\n \"\"\"\n if print_method == \"__repr__\":\n return repr(obj)\n elif hasattr(obj, print_method):\n if print_method == \"savefig\":\n buf = io.BytesIO()\n obj.savefig(buf, format=\"png\")\n buf.seek(0)\n return base64.b64encode(buf.read()).decode(\"utf-8\")\n return getattr(obj, print_method)()\n elif print_method == \"_repr_mimebundle_\":\n return {}, {}\n return None\n\n\ndef _format_mime(obj):\n \"\"\"\n Formats object using _repr_x_ methods.\n \"\"\"\n if isinstance(obj, str):\n return html.escape(obj), \"text/plain\"\n\n mimebundle = _eval_formatter(obj, \"_repr_mimebundle_\")\n if isinstance(mimebundle, tuple):\n format_dict, _ = mimebundle\n else:\n format_dict = mimebundle\n\n output, not_available = None, []\n for method, mime_type in reversed(_MIME_METHODS.items()):\n if mime_type in format_dict:\n output = format_dict[mime_type]\n else:\n output = _eval_formatter(obj, method)\n\n if output is None:\n continue\n elif mime_type not in _MIME_RENDERERS:\n not_available.append(mime_type)\n continue\n break\n if output is None:\n if not_available:\n window.console.warn(\n f\"Rendered object requested unavailable MIME renderers: {not_available}\"\n )\n output = repr(output)\n mime_type = \"text/plain\"\n elif isinstance(output, tuple):\n output, meta = output\n else:\n meta = {}\n return _MIME_RENDERERS[mime_type](output, meta), mime_type\n\n\ndef _write(element, value, append=False):\n html, mime_type = _format_mime(value)\n if html == \"\\\\n\":\n return\n\n if append:\n out_element = document.createElement(\"div\")\n element.append(out_element)\n else:\n out_element = element.lastElementChild\n if out_element is None:\n out_element = element\n\n if mime_type in (\"application/javascript\", \"text/html\"):\n script_element = document.createRange().createContextualFragment(html)\n out_element.append(script_element)\n else:\n out_element.innerHTML = html\n\n\ndef display(*values, target=None, append=True):\n if target is None:\n target = current_target()\n\n element = document.getElementById(target)\n\n # if element is a <script type=\"py\">, it has a 'target' attribute which\n # points to the visual element holding the displayed values. In that case,\n # use that.\n if element.tagName == 'SCRIPT' and hasattr(element, 'target'):\n element = element.target\n\n for v in values:\n _write(element, v, append=append)\n",
6
6
  "event_handling.py": "import inspect\n\nfrom pyodide.ffi.wrappers import add_event_listener\nfrom pyscript.magic_js import document\n\n\ndef when(event_type=None, selector=None):\n \"\"\"\n Decorates a function and passes py-* events to the decorated function\n The events might or not be an argument of the decorated function\n \"\"\"\n\n def decorator(func):\n if isinstance(selector, str):\n elements = document.querySelectorAll(selector)\n else:\n # TODO: This is a hack that will be removed when pyscript becomes a package\n # and we can better manage the imports without circular dependencies\n from pyweb import pydom\n\n if isinstance(selector, pydom.Element):\n elements = [selector._js]\n elif isinstance(selector, pydom.ElementCollection):\n elements = [el._js for el in selector]\n else:\n raise ValueError(\n f\"Invalid selector: {selector}. Selector must\"\n \" be a string, a pydom.Element or a pydom.ElementCollection.\"\n )\n\n sig = inspect.signature(func)\n # Function doesn't receive events\n if not sig.parameters:\n\n def wrapper(*args, **kwargs):\n func()\n\n for el in elements:\n add_event_listener(el, event_type, wrapper)\n else:\n for el in elements:\n add_event_listener(el, event_type, func)\n return func\n\n return decorator\n",
7
7
  "magic_js.py": "from pyscript.util import NotSupported\nimport js as globalThis\n\nRUNNING_IN_WORKER = not hasattr(globalThis, \"document\")\n\nif RUNNING_IN_WORKER:\n import polyscript\n\n PyWorker = NotSupported(\n 'pyscript.PyWorker',\n 'pyscript.PyWorker works only when running in the main thread')\n window = polyscript.xworker.window\n document = window.document\n sync = polyscript.xworker.sync\n\n # in workers the display does not have a default ID\n # but there is a sync utility from xworker\n def current_target():\n return polyscript.target\n\nelse:\n import _pyscript\n from _pyscript import PyWorker\n window = globalThis\n document = globalThis.document\n sync = NotSupported(\n 'pyscript.sync',\n 'pyscript.sync works only when running in a worker')\n\n # in MAIN the current element target exist, just use it\n def current_target():\n return _pyscript.target\n",
8
- "util.py": "class NotSupported:\n \"\"\"\n Small helper that raises exceptions if you try to get/set any attribute on\n it.\n \"\"\"\n\n def __init__(self, name, error):\n # we set attributes using self.__dict__ to bypass the __setattr__\n self.__dict__['name'] = name\n self.__dict__['error'] = error\n\n def __repr__(self):\n return f'<NotSupported {self.name} [{self.error}]>'\n\n def __getattr__(self, attr):\n raise AttributeError(self.error)\n\n def __setattr__(self, attr, value):\n raise AttributeError(self.error)\n\n def __call__(self, *args):\n raise TypeError(self.error)\n"
8
+ "util.py": "class NotSupported:\n \"\"\"\n Small helper that raises exceptions if you try to get/set any attribute on\n it.\n \"\"\"\n\n def __init__(self, name, error):\n object.__setattr__(self, \"name\", name)\n object.__setattr__(self, \"error\", error)\n\n def __repr__(self):\n return f\"<NotSupported {self.name} [{self.error}]>\"\n\n def __getattr__(self, attr):\n raise AttributeError(self.error)\n\n def __setattr__(self, attr, value):\n raise AttributeError(self.error)\n\n def __call__(self, *args):\n raise TypeError(self.error)\n"
9
9
  },
10
10
  "pyweb": {
11
11
  "pydom.py": "import sys\nimport warnings\nfrom functools import cached_property\nfrom typing import Any\n\nfrom pyodide.ffi import JsProxy\nfrom pyscript import display, document, window\n\n# from pyscript import when as _when\n\nalert = window.alert\n\n\nclass BaseElement:\n def __init__(self, js_element):\n self._js = js_element\n self._parent = None\n self.style = StyleProxy(self)\n\n def __eq__(self, obj):\n \"\"\"Check if the element is the same as the other element by comparing\n the underlying JS element\"\"\"\n return isinstance(obj, BaseElement) and obj._js == self._js\n\n @property\n def parent(self):\n if self._parent:\n return self._parent\n\n if self._js.parentElement:\n self._parent = self.__class__(self._js.parentElement)\n\n return self._parent\n\n @property\n def __class(self):\n return self.__class__ if self.__class__ != PyDom else Element\n\n def create(self, type_, is_child=True, classes=None, html=None, label=None):\n js_el = document.createElement(type_)\n element = self.__class(js_el)\n\n if classes:\n for class_ in classes:\n element.add_class(class_)\n\n if html is not None:\n element.html = html\n\n if label is not None:\n element.label = label\n\n if is_child:\n self.append(element)\n\n return element\n\n def find(self, selector):\n \"\"\"Return an ElementCollection representing all the child elements that\n match the specified selector.\n\n Args:\n selector (str): A string containing a selector expression\n\n Returns:\n ElementCollection: A collection of elements matching the selector\n \"\"\"\n elements = self._js.querySelectorAll(selector)\n if not elements:\n return None\n return ElementCollection([Element(el) for el in elements])\n\n\nclass Element(BaseElement):\n @property\n def children(self):\n return [self.__class__(el) for el in self._js.children]\n\n def append(self, child):\n # TODO: this is Pyodide specific for now!!!!!!\n # if we get passed a JSProxy Element directly we just map it to the\n # higher level Python element\n if isinstance(child, JsProxy):\n return self.append(Element(child))\n\n elif isinstance(child, Element):\n self._js.appendChild(child._js)\n\n return child\n\n elif isinstance(child, ElementCollection):\n for el in child:\n self.append(el)\n\n # -------- Pythonic Interface to Element -------- #\n @property\n def html(self):\n return self._js.innerHTML\n\n @html.setter\n def html(self, value):\n self._js.innerHTML = value\n\n @property\n def content(self):\n # TODO: This breaks with with standard template elements. Define how to best\n # handle this specifica use case. Just not support for now?\n if self._js.tagName == \"TEMPLATE\":\n warnings.warn(\n \"Content attribute not supported for template elements.\", stacklevel=2\n )\n return None\n return self._js.innerHTML\n\n @content.setter\n def content(self, value):\n # TODO: (same comment as above)\n if self._js.tagName == \"TEMPLATE\":\n warnings.warn(\n \"Content attribute not supported for template elements.\", stacklevel=2\n )\n return\n\n display(value, target=self.id)\n\n @property\n def id(self):\n return self._js.id\n\n @id.setter\n def id(self, value):\n self._js.id = value\n\n def clone(self, new_id=None):\n clone = Element(self._js.cloneNode(True))\n clone.id = new_id\n\n return clone\n\n def remove_class(self, classname):\n classList = self._js.classList\n if isinstance(classname, list):\n classList.remove(*classname)\n else:\n classList.remove(classname)\n return self\n\n def add_class(self, classname):\n classList = self._js.classList\n if isinstance(classname, list):\n classList.add(*classname)\n else:\n self._js.classList.add(classname)\n return self\n\n @property\n def classes(self):\n classes = self._js.classList.values()\n return [x for x in classes]\n\n def show_me(self):\n self._js.scrollIntoView()\n\n def when(self, event, handler):\n document.when(event, selector=self)(handler)\n\n\nclass StyleProxy(dict):\n def __init__(self, element: Element) -> None:\n self._element = element\n\n @cached_property\n def _style(self):\n return self._element._js.style\n\n def __getitem__(self, key):\n return self._style.getPropertyValue(key)\n\n def __setitem__(self, key, value):\n self._style.setProperty(key, value)\n\n def remove(self, key):\n self._style.removeProperty(key)\n\n def set(self, **kws):\n for k, v in kws.items():\n self._element._js.style.setProperty(k, v)\n\n # CSS Properties\n # Reference: https://github.com/microsoft/TypeScript/blob/main/src/lib/dom.generated.d.ts#L3799C1-L5005C2\n # Following prperties automatically generated from the above reference using\n # tools/codegen_css_proxy.py\n @property\n def visible(self):\n return self._element._js.style.visibility\n\n @visible.setter\n def visible(self, value):\n self._element._js.style.visibility = value\n\n\nclass StyleCollection:\n def __init__(self, collection: \"ElementCollection\") -> None:\n self._collection = collection\n\n def __get__(self, obj, objtype=None):\n return obj._get_attribute(\"style\")\n\n def __getitem__(self, key):\n return self._collection._get_attribute(\"style\")[key]\n\n def __setitem__(self, key, value):\n for element in self._collection._elements:\n element.style[key] = value\n\n def remove(self, key):\n for element in self._collection._elements:\n element.style.remove(key)\n\n\nclass ElementCollection:\n def __init__(self, elements: [Element]) -> None:\n self._elements = elements\n self.style = StyleCollection(self)\n\n def __getitem__(self, key):\n # If it's an integer we use it to access the elements in the collection\n if isinstance(key, int):\n return self._elements[key]\n # If it's a slice we use it to support slice operations over the elements\n # in the collection\n elif isinstance(key, slice):\n return ElementCollection(self._elements[key])\n\n # If it's anything else (basically a string) we use it as a selector\n # TODO: Write tests!\n elements = self._element.querySelectorAll(key)\n return ElementCollection([Element(el) for el in elements])\n\n def __len__(self):\n return len(self._elements)\n\n def __eq__(self, obj):\n \"\"\"Check if the element is the same as the other element by comparing\n the underlying JS element\"\"\"\n return isinstance(obj, ElementCollection) and obj._elements == self._elements\n\n def _get_attribute(self, attr, index=None):\n if index is None:\n return [getattr(el, attr) for el in self._elements]\n\n # As JQuery, when getting an attr, only return it for the first element\n return getattr(self._elements[index], attr)\n\n def _set_attribute(self, attr, value):\n for el in self._elements:\n setattr(el, attr, value)\n\n @property\n def html(self):\n return self._get_attribute(\"html\")\n\n @html.setter\n def html(self, value):\n self._set_attribute(\"html\", value)\n\n @property\n def children(self):\n return self._elements\n\n def __iter__(self):\n yield from self._elements\n\n def __repr__(self):\n return f\"{self.__class__.__name__} (length: {len(self._elements)}) {self._elements}\"\n\n\nclass DomScope:\n def __getattr__(self, __name: str) -> Any:\n element = document[f\"#{__name}\"]\n if element:\n return element[0]\n\n\nclass PyDom(BaseElement):\n # Add objects we want to expose to the DOM namespace since this class instance is being\n # remapped as \"the module\" itself\n BaseElement = BaseElement\n Element = Element\n ElementCollection = ElementCollection\n\n def __init__(self):\n super().__init__(document)\n self.ids = DomScope()\n self.body = Element(document.body)\n self.head = Element(document.head)\n\n def create(self, type_, parent=None, classes=None, html=None):\n return super().create(type_, is_child=False)\n\n def __getitem__(self, key):\n if isinstance(key, int):\n indices = range(*key.indices(len(self.list)))\n return [self.list[i] for i in indices]\n\n elements = self._js.querySelectorAll(key)\n if not elements:\n return None\n return ElementCollection([Element(el) for el in elements])\n\n\ndom = PyDom()\n\nsys.modules[__name__] = dom\n"
@@ -1,2 +0,0 @@
1
- import{hooks as e}from"./core.js";function o(e){const o=document.createElement("div");o.className="py-error",o.textContent=e,o.style.cssText="\n border: 1px solid red;\n background: #ffdddd;\n color: black;\n font-family: courier, monospace;\n white-space: pre;\n overflow-x: auto;\n padding: 8px;\n margin-top: 8px;\n ",document.body.append(o)}e.onBeforeRun.add((function n(r){e.onBeforeRun.delete(n);const{stderr:t}=r.io;r.io.stderr=(e,...n)=>(o(e.message||e),t(e,...n)),addEventListener("error",(({message:e})=>{e.startsWith("Uncaught PythonError")&&o(e)}))}));export{o as notify};
2
- //# sourceMappingURL=error-87e0706c.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"error-87e0706c.js","sources":["../src/plugins/error.js"],"sourcesContent":["// PyScript Error Plugin\nimport { hooks } from \"../core.js\";\n\nhooks.onBeforeRun.add(function override(pyScript) {\n // be sure this override happens only once\n hooks.onBeforeRun.delete(override);\n\n // trap generic `stderr` to propagate to it regardless\n const { stderr } = pyScript.io;\n\n // override it with our own logic\n pyScript.io.stderr = (error, ...rest) => {\n notify(error.message || error);\n // let other plugins or stderr hook, if any, do the rest\n return stderr(error, ...rest);\n };\n\n // be sure uncaught Python errors are also visible\n addEventListener(\"error\", ({ message }) => {\n if (message.startsWith(\"Uncaught PythonError\")) notify(message);\n });\n});\n\n// Error hook utilities\n\n// Custom function to show notifications\nexport function notify(message) {\n const div = document.createElement(\"div\");\n div.className = \"py-error\";\n div.textContent = message;\n div.style.cssText = `\n border: 1px solid red;\n background: #ffdddd;\n color: black;\n font-family: courier, monospace;\n white-space: pre;\n overflow-x: auto;\n padding: 8px;\n margin-top: 8px;\n `;\n document.body.append(div);\n}\n"],"names":["notify","message","div","document","createElement","className","textContent","style","cssText","body","append","hooks","onBeforeRun","add","override","pyScript","delete","stderr","io","error","rest","addEventListener","startsWith"],"mappings":"kCA0BO,SAASA,EAAOC,GACnB,MAAMC,EAAMC,SAASC,cAAc,OACnCF,EAAIG,UAAY,WAChBH,EAAII,YAAcL,EAClBC,EAAIK,MAAMC,QAAU,6MAUpBL,SAASM,KAAKC,OAAOR,EACzB,CAtCAS,EAAMC,YAAYC,KAAI,SAASC,EAASC,GAEpCJ,EAAMC,YAAYI,OAAOF,GAGzB,MAAMG,OAAEA,GAAWF,EAASG,GAG5BH,EAASG,GAAGD,OAAS,CAACE,KAAUC,KAC5BpB,EAAOmB,EAAMlB,SAAWkB,GAEjBF,EAAOE,KAAUC,IAI5BC,iBAAiB,SAAS,EAAGpB,cACrBA,EAAQqB,WAAW,yBAAyBtB,EAAOC,EAAQ,GAEvE"}
File without changes