@pyscript/core 0.2.6 → 0.2.8

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.
@@ -1,2 +1,2 @@
1
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
2
+ //# sourceMappingURL=error-q53fTtv2.js.map
@@ -1 +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\n\n/**\n * Add a banner to the top of the page, notifying the user of an error\n * @param {string} message\n */\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":"kCA+BO,SAASA,EAAOC,GACnB,MAAMC,EAAMC,SAASC,cAAc,OACnCF,EAAIG,UAAY,WAChBH,EAAII,YAAcL,EAClBC,EAAIK,MAAMC,QAAU,6MAUpBL,SAASM,KAAKC,OAAOR,EACzB,CA3CAS,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"}
1
+ {"version":3,"file":"error-q53fTtv2.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\n\n/**\n * Add a banner to the top of the page, notifying the user of an error\n * @param {string} message\n */\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":"kCA+BO,SAASA,EAAOC,GACnB,MAAMC,EAAMC,SAASC,cAAc,OACnCF,EAAIG,UAAY,WAChBH,EAAII,YAAcL,EAClBC,EAAIK,MAAMC,QAAU,6MAUpBL,SAASM,KAAKC,OAAOR,EACzB,CA3CAS,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.2.6",
3
+ "version": "0.2.8",
4
4
  "type": "module",
5
5
  "description": "PyScript",
6
6
  "module": "./index.js",
@@ -19,7 +19,7 @@
19
19
  "./package.json": "./package.json"
20
20
  },
21
21
  "scripts": {
22
- "server": "npx static-handler --cors --coep --coop --corp .",
22
+ "server": "npx static-handler --coi .",
23
23
  "build": "node rollup/stdlib.cjs && node rollup/plugins.cjs && rm -rf dist && rollup --config rollup/core.config.js && eslint src/ && npm run ts",
24
24
  "size": "echo -e \"\\033[1mdist/*.js file size\\033[0m\"; for js in $(ls dist/*.js); do echo -e \"\\033[2m$js:\\033[0m $(cat $js | brotli | wc -c) bytes\"; done",
25
25
  "ts": "tsc -p ."
@@ -33,17 +33,17 @@
33
33
  "dependencies": {
34
34
  "@ungap/with-resolvers": "^0.1.0",
35
35
  "basic-devtools": "^0.1.6",
36
- "polyscript": "^0.4.11",
36
+ "polyscript": "^0.4.13",
37
37
  "type-checked-collections": "^0.1.7"
38
38
  },
39
39
  "devDependencies": {
40
- "@rollup/plugin-node-resolve": "^15.2.1",
41
- "@rollup/plugin-terser": "^0.4.3",
42
- "eslint": "^8.50.0",
43
- "rollup": "^3.29.4",
40
+ "@rollup/plugin-node-resolve": "^15.2.3",
41
+ "@rollup/plugin-terser": "^0.4.4",
42
+ "eslint": "^8.51.0",
43
+ "rollup": "^4.0.2",
44
44
  "rollup-plugin-postcss": "^4.0.2",
45
45
  "rollup-plugin-string": "^3.0.0",
46
- "static-handler": "^0.4.2",
46
+ "static-handler": "^0.4.3",
47
47
  "typescript": "^5.2.2"
48
48
  },
49
49
  "repository": {
package/src/config.js CHANGED
@@ -50,7 +50,7 @@ const syntaxError = (type, url, { message }) => {
50
50
  const configs = new Map();
51
51
 
52
52
  for (const [TYPE] of TYPES) {
53
- /** @type {Promise<any> | undefined} A Promise wrapping any plugins which should be loaded. */
53
+ /** @type {Promise<[...any]>} A Promise wrapping any plugins which should be loaded. */
54
54
  let plugins;
55
55
 
56
56
  /** @type {any} The PyScript configuration parsed from the JSON or TOML object*. May be any of the return types of JSON.parse() or toml-j0.4's parse() ( {number | string | boolean | null | object | Array} ) */
@@ -119,7 +119,7 @@ for (const [TYPE] of TYPES) {
119
119
  }
120
120
 
121
121
  // assign plugins as Promise.all only if needed
122
- if (toBeAwaited.length) plugins = Promise.all(toBeAwaited);
122
+ plugins = Promise.all(toBeAwaited);
123
123
 
124
124
  configs.set(TYPE, { config: parsed, plugins, error });
125
125
  }
package/src/core.js CHANGED
@@ -128,162 +128,162 @@ for (const [TYPE, interpreter] of TYPES) {
128
128
  // define the module as both `<script type="py">` and `<py-script>`
129
129
  // but only if the config didn't throw an error
130
130
  if (!error) {
131
- // possible early errors sent by polyscript
132
- const errors = new Map();
133
-
134
- define(TYPE, {
135
- config,
136
- interpreter,
137
- env: `${TYPE}-script`,
138
- version: config?.interpreter,
139
- onerror(error, element) {
140
- errors.set(element, error);
141
- },
142
- ...workerHooks,
143
- onWorkerReady(_, xworker) {
144
- assign(xworker.sync, sync);
145
- for (const callback of hooks.onWorkerReady)
146
- callback(_, xworker);
147
- },
148
- onBeforeRun(wrap, element) {
149
- currentElement = element;
150
- bootstrapNodeAndPlugins(wrap, element, before, "onBeforeRun");
151
- },
152
- onBeforeRunAsync(wrap, element) {
153
- currentElement = element;
154
- bootstrapNodeAndPlugins(
155
- wrap,
156
- element,
157
- before,
158
- "onBeforeRunAsync",
159
- );
160
- },
161
- onAfterRun(wrap, element) {
162
- bootstrapNodeAndPlugins(wrap, element, after, "onAfterRun");
163
- },
164
- onAfterRunAsync(wrap, element) {
165
- bootstrapNodeAndPlugins(
166
- wrap,
167
- element,
168
- after,
169
- "onAfterRunAsync",
170
- );
171
- },
172
- async onInterpreterReady(wrap, element) {
173
- if (shouldRegister) {
174
- shouldRegister = false;
175
- registerModule(wrap);
176
- }
177
-
178
- // ensure plugins are bootstrapped already
179
- if (plugins) await plugins;
180
-
181
- // allows plugins to do whatever they want with the element
182
- // before regular stuff happens in here
183
- for (const callback of hooks.onInterpreterReady)
184
- callback(wrap, element);
185
-
186
- // now that all possible plugins are configured,
187
- // bail out if polyscript encountered an error
188
- if (errors.has(element)) {
189
- let { message } = errors.get(element);
190
- errors.delete(element);
191
- const clone = message === INVALID_CONTENT;
192
- message = `(${ErrorCode.CONFLICTING_CODE}) ${message} for `;
193
- message += element.cloneNode(clone).outerHTML;
194
- wrap.io.stderr(message);
195
- return;
196
- }
197
-
198
- if (isScript(element)) {
199
- const {
200
- attributes: { async: isAsync, target },
201
- } = element;
202
- const hasTarget = !!target?.value;
203
- const show = hasTarget
204
- ? queryTarget(element, target.value)
205
- : document.createElement("script-py");
131
+ // ensure plugins are bootstrapped already before custom type definition
132
+ // NOTE: we cannot top-level await in here as plugins import other utilities
133
+ // from core.js itself so that custom definition should not be blocking.
134
+ plugins.then(() => {
135
+ // possible early errors sent by polyscript
136
+ const errors = new Map();
137
+
138
+ define(TYPE, {
139
+ config,
140
+ interpreter,
141
+ env: `${TYPE}-script`,
142
+ version: config?.interpreter,
143
+ onerror(error, element) {
144
+ errors.set(element, error);
145
+ },
146
+ ...workerHooks,
147
+ onWorkerReady(_, xworker) {
148
+ assign(xworker.sync, sync);
149
+ for (const callback of hooks.onWorkerReady)
150
+ callback(_, xworker);
151
+ },
152
+ onBeforeRun(wrap, element) {
153
+ currentElement = element;
154
+ bootstrapNodeAndPlugins(
155
+ wrap,
156
+ element,
157
+ before,
158
+ "onBeforeRun",
159
+ );
160
+ },
161
+ onBeforeRunAsync(wrap, element) {
162
+ currentElement = element;
163
+ bootstrapNodeAndPlugins(
164
+ wrap,
165
+ element,
166
+ before,
167
+ "onBeforeRunAsync",
168
+ );
169
+ },
170
+ onAfterRun(wrap, element) {
171
+ bootstrapNodeAndPlugins(wrap, element, after, "onAfterRun");
172
+ },
173
+ onAfterRunAsync(wrap, element) {
174
+ bootstrapNodeAndPlugins(
175
+ wrap,
176
+ element,
177
+ after,
178
+ "onAfterRunAsync",
179
+ );
180
+ },
181
+ async onInterpreterReady(wrap, element) {
182
+ if (shouldRegister) {
183
+ shouldRegister = false;
184
+ registerModule(wrap);
185
+ }
206
186
 
207
- if (!hasTarget) {
208
- const { head, body } = document;
209
- if (head.contains(element)) body.append(show);
210
- else element.after(show);
187
+ // allows plugins to do whatever they want with the element
188
+ // before regular stuff happens in here
189
+ for (const callback of hooks.onInterpreterReady)
190
+ callback(wrap, element);
191
+
192
+ // now that all possible plugins are configured,
193
+ // bail out if polyscript encountered an error
194
+ if (errors.has(element)) {
195
+ let { message } = errors.get(element);
196
+ errors.delete(element);
197
+ const clone = message === INVALID_CONTENT;
198
+ message = `(${ErrorCode.CONFLICTING_CODE}) ${message} for `;
199
+ message += element.cloneNode(clone).outerHTML;
200
+ wrap.io.stderr(message);
201
+ return;
211
202
  }
212
- if (!show.id) show.id = getID();
213
203
 
214
- // allows the code to retrieve the target element via
215
- // document.currentScript.target if needed
216
- defineProperty(element, "target", { value: show });
204
+ if (isScript(element)) {
205
+ const {
206
+ attributes: { async: isAsync, target },
207
+ } = element;
208
+ const hasTarget = !!target?.value;
209
+ const show = hasTarget
210
+ ? queryTarget(element, target.value)
211
+ : document.createElement("script-py");
212
+
213
+ if (!hasTarget) {
214
+ const { head, body } = document;
215
+ if (head.contains(element)) body.append(show);
216
+ else element.after(show);
217
+ }
218
+ if (!show.id) show.id = getID();
219
+
220
+ // allows the code to retrieve the target element via
221
+ // document.currentScript.target if needed
222
+ defineProperty(element, "target", { value: show });
223
+
224
+ // notify before the code runs
225
+ dispatch(element, TYPE, "ready");
226
+ dispatchDone(
227
+ element,
228
+ isAsync,
229
+ wrap[`run${isAsync ? "Async" : ""}`](
230
+ await fetchSource(element, wrap.io, true),
231
+ ),
232
+ );
233
+ } else {
234
+ // resolve PyScriptElement to allow connectedCallback
235
+ element._wrap.resolve(wrap);
236
+ }
237
+ console.debug("[pyscript/main] PyScript Ready");
238
+ },
239
+ });
217
240
 
218
- // notify before the code runs
219
- dispatch(element, TYPE, "ready");
220
- dispatchDone(
221
- element,
222
- isAsync,
223
- wrap[`run${isAsync ? "Async" : ""}`](
224
- await fetchSource(element, wrap.io, true),
225
- ),
226
- );
227
- } else {
228
- // resolve PyScriptElement to allow connectedCallback
229
- element._wrap.resolve(wrap);
230
- }
231
- console.debug("[pyscript/main] PyScript Ready");
232
- },
241
+ customElements.define(
242
+ `${TYPE}-script`,
243
+ class extends HTMLElement {
244
+ constructor() {
245
+ assign(super(), {
246
+ _wrap: Promise.withResolvers(),
247
+ srcCode: "",
248
+ executed: false,
249
+ });
250
+ }
251
+ get id() {
252
+ return super.id || (super.id = getID());
253
+ }
254
+ set id(value) {
255
+ super.id = value;
256
+ }
257
+ async connectedCallback() {
258
+ if (!this.executed) {
259
+ this.executed = true;
260
+ const isAsync = this.hasAttribute("async");
261
+ const { io, run, runAsync } = await this._wrap
262
+ .promise;
263
+ this.srcCode = await fetchSource(
264
+ this,
265
+ io,
266
+ !this.childElementCount,
267
+ );
268
+ this.replaceChildren();
269
+ this.style.display = "block";
270
+ dispatch(this, TYPE, "ready");
271
+ dispatchDone(
272
+ this,
273
+ isAsync,
274
+ (isAsync ? runAsync : run)(this.srcCode),
275
+ );
276
+ }
277
+ }
278
+ },
279
+ );
233
280
  });
234
281
  }
235
282
 
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 isAsync = this.hasAttribute("async");
259
- const { io, run, runAsync } = await this._wrap.promise;
260
- this.srcCode = await fetchSource(
261
- this,
262
- io,
263
- !this.childElementCount,
264
- );
265
- this.replaceChildren();
266
- this.style.display = "block";
267
- dispatch(this, TYPE, "ready");
268
- dispatchDone(
269
- this,
270
- isAsync,
271
- (isAsync ? runAsync : run)(this.srcCode),
272
- );
273
- }
274
- }
275
- }
276
-
277
- // define py-script only if the config didn't throw an error
278
- if (!error) customElements.define(`${TYPE}-script`, PyScriptElement);
279
-
280
283
  // export the used config without allowing leaks through it
281
284
  exportedConfig[TYPE] = structuredClone(config);
282
285
  }
283
286
 
284
- // TBD: I think manual worker cases are interesting in pyodide only
285
- // so for the time being we should be fine with this export.
286
-
287
287
  /**
288
288
  * A `Worker` facade able to bootstrap on the worker thread only a PyScript module.
289
289
  * @param {string} file the python file to run ina worker.
@@ -297,8 +297,8 @@ export function PyWorker(file, options) {
297
297
  // and as `pyodide` is the only default interpreter that can deal with
298
298
  // all the features we need to deliver pyscript out there.
299
299
  const xworker = XWorker.call(new Hook(null, workerHooks), file, {
300
- ...options,
301
300
  type: "pyodide",
301
+ ...options,
302
302
  });
303
303
  assign(xworker.sync, sync);
304
304
  return xworker;
@@ -29,8 +29,15 @@
29
29
  # pyscript.magic_js. This is the blessed way to access them from pyscript,
30
30
  # as it works transparently in both the main thread and worker cases.
31
31
 
32
- from pyscript.magic_js import RUNNING_IN_WORKER, window, document, sync
33
32
  from pyscript.display import HTML, display
33
+ from pyscript.magic_js import (
34
+ RUNNING_IN_WORKER,
35
+ PyWorker,
36
+ current_target,
37
+ document,
38
+ sync,
39
+ window,
40
+ )
34
41
 
35
42
  try:
36
43
  from pyscript.event_handling import when
@@ -38,6 +45,5 @@ except:
38
45
  from pyscript.util import NotSupported
39
46
 
40
47
  when = NotSupported(
41
- "pyscript.when",
42
- "pyscript.when currently not available with this interpreter"
48
+ "pyscript.when", "pyscript.when currently not available with this interpreter"
43
49
  )
@@ -148,9 +148,23 @@ def _write(element, value, append=False):
148
148
  def display(*values, target=None, append=True):
149
149
  if target is None:
150
150
  target = current_target()
151
+ elif not isinstance(target, str):
152
+ raise TypeError(f"target must be str or None, not {target.__class__.__name__}")
153
+ elif target == "":
154
+ raise ValueError("Cannot have an empty target")
155
+ elif target.startswith("#"):
156
+ # note: here target is str and not None!
157
+ # align with @when behavior
158
+ target = target[1:]
151
159
 
152
160
  element = document.getElementById(target)
153
161
 
162
+ # If target cannot be found on the page, a ValueError is raised
163
+ if element is None:
164
+ raise ValueError(
165
+ f"Invalid selector with id={target}. Cannot be found in the page."
166
+ )
167
+
154
168
  # if element is a <script type="py">, it has a 'target' attribute which
155
169
  # points to the visual element holding the displayed values. In that case,
156
170
  # use that.
@@ -1,5 +1,5 @@
1
- from pyscript.util import NotSupported
2
1
  import js as globalThis
2
+ from pyscript.util import NotSupported
3
3
 
4
4
  RUNNING_IN_WORKER = not hasattr(globalThis, "document")
5
5
 
@@ -7,8 +7,9 @@ if RUNNING_IN_WORKER:
7
7
  import polyscript
8
8
 
9
9
  PyWorker = NotSupported(
10
- 'pyscript.PyWorker',
11
- 'pyscript.PyWorker works only when running in the main thread')
10
+ "pyscript.PyWorker",
11
+ "pyscript.PyWorker works only when running in the main thread",
12
+ )
12
13
  window = polyscript.xworker.window
13
14
  document = window.document
14
15
  sync = polyscript.xworker.sync
@@ -21,11 +22,12 @@ if RUNNING_IN_WORKER:
21
22
  else:
22
23
  import _pyscript
23
24
  from _pyscript import PyWorker
25
+
24
26
  window = globalThis
25
27
  document = globalThis.document
26
28
  sync = NotSupported(
27
- 'pyscript.sync',
28
- 'pyscript.sync works only when running in a worker')
29
+ "pyscript.sync", "pyscript.sync works only when running in a worker"
30
+ )
29
31
 
30
32
  # in MAIN the current element target exist, just use it
31
33
  def current_target():
@@ -1,10 +1,10 @@
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\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 current_target, document, window\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_pdf_\": \"application/pdf\",\n \"_repr_jpeg_\": \"image/jpeg\",\n \"_repr_png_\": \"image/png\",\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 if not append:\n element.replaceChildren()\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.display import HTML, display\nfrom pyscript.magic_js import (\n RUNNING_IN_WORKER,\n PyWorker,\n current_target,\n document,\n sync,\n window,\n)\n\ntry:\n from pyscript.event_handling import when\nexcept:\n from pyscript.util import NotSupported\n\n when = NotSupported(\n \"pyscript.when\", \"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 current_target, document, window\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_pdf_\": \"application/pdf\",\n \"_repr_jpeg_\": \"image/jpeg\",\n \"_repr_png_\": \"image/png\",\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 elif not isinstance(target, str):\n raise TypeError(f\"target must be str or None, not {target.__class__.__name__}\")\n elif target == \"\":\n raise ValueError(\"Cannot have an empty target\")\n elif target.startswith(\"#\"):\n # note: here target is str and not None!\n # align with @when behavior\n target = target[1:]\n\n element = document.getElementById(target)\n\n # If target cannot be found on the page, a ValueError is raised\n if element is None:\n raise ValueError(\n f\"Invalid selector with id={target}. Cannot be found in the page.\"\n )\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 if not append:\n element.replaceChildren()\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
- "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",
7
+ "magic_js.py": "import js as globalThis\nfrom pyscript.util import NotSupported\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 )\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\n window = globalThis\n document = globalThis.document\n sync = NotSupported(\n \"pyscript.sync\", \"pyscript.sync works only when running in a worker\"\n )\n\n # in MAIN the current element target exist, just use it\n def current_target():\n return _pyscript.target\n",
8
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": {