@pyscript/core 0.2.5 → 0.2.6

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pyscript/core",
3
- "version": "0.2.5",
3
+ "version": "0.2.6",
4
4
  "type": "module",
5
5
  "description": "PyScript",
6
6
  "module": "./index.js",
@@ -33,7 +33,8 @@
33
33
  "dependencies": {
34
34
  "@ungap/with-resolvers": "^0.1.0",
35
35
  "basic-devtools": "^0.1.6",
36
- "polyscript": "^0.4.8"
36
+ "polyscript": "^0.4.11",
37
+ "type-checked-collections": "^0.1.7"
37
38
  },
38
39
  "devDependencies": {
39
40
  "@rollup/plugin-node-resolve": "^15.2.1",
package/src/all-done.js CHANGED
@@ -1,62 +1,17 @@
1
1
  import TYPES from "./types.js";
2
- import hooks from "./hooks.js";
3
-
4
- const DONE = "py:all-done";
5
-
6
- const {
7
- onAfterRun,
8
- onAfterRunAsync,
9
- codeAfterRunWorker,
10
- codeAfterRunWorkerAsync,
11
- } = hooks;
12
2
 
13
3
  const waitForIt = [];
14
- const codes = [];
15
-
16
- const codeFor = (element) => {
17
- const isAsync = element.hasAttribute("async");
18
- const { promise, resolve } = Promise.withResolvers();
19
- const type = `${DONE}:${waitForIt.push(promise)}`;
20
4
 
21
- // resolve each promise once notified
22
- addEventListener(type, resolve, { once: true });
23
-
24
- if (element.hasAttribute("worker")) {
25
- const code = `
26
- from pyscript import window as _w
27
- _w.dispatchEvent(_w.Event.new("${type}"))
28
- `;
29
- if (isAsync) codeAfterRunWorkerAsync.add(code);
30
- else codeAfterRunWorker.add(code);
31
- return code;
5
+ for (const [TYPE] of TYPES) {
6
+ const selectors = [`script[type="${TYPE}"]`, `${TYPE}-script`];
7
+ for (const element of document.querySelectorAll(selectors.join(","))) {
8
+ const { promise, resolve } = Promise.withResolvers();
9
+ waitForIt.push(promise);
10
+ element.addEventListener(`${TYPE}:done`, resolve, { once: true });
32
11
  }
33
-
34
- // dispatch only once the ready element is the same
35
- const code = (_, el) => {
36
- if (el === element) dispatchEvent(new Event(type));
37
- };
38
-
39
- if (isAsync) onAfterRunAsync.add(code);
40
- else onAfterRun.add(code);
41
- return code;
42
- };
43
-
44
- const selector = [];
45
- for (const [TYPE] of TYPES)
46
- selector.push(`script[type="${TYPE}"]`, `${TYPE}-script`);
47
-
48
- // loop over all known scripts and elements
49
- for (const element of document.querySelectorAll(selector.join(",")))
50
- codes.push(codeFor(element));
12
+ }
51
13
 
52
14
  // wait for all the things then cleanup
53
15
  Promise.all(waitForIt).then(() => {
54
- // cleanup unnecessary hooks
55
- for (const code of codes) {
56
- onAfterRun.delete(code);
57
- onAfterRunAsync.delete(code);
58
- codeAfterRunWorker.delete(code);
59
- codeAfterRunWorkerAsync.delete(code);
60
- }
61
- dispatchEvent(new Event(DONE));
16
+ dispatchEvent(new Event("py:all-done"));
62
17
  });
package/src/core.js CHANGED
@@ -90,6 +90,11 @@ const exportedConfig = {};
90
90
  export { exportedConfig as config, hooks };
91
91
 
92
92
  for (const [TYPE, interpreter] of TYPES) {
93
+ const dispatchDone = (element, isAsync, result) => {
94
+ if (isAsync) result.then(() => dispatch(element, TYPE, "done"));
95
+ else dispatch(element, TYPE, "done");
96
+ };
97
+
93
98
  const { config, plugins, error } = configs.get(TYPE);
94
99
 
95
100
  // create a unique identifier when/if needed
@@ -137,6 +142,8 @@ for (const [TYPE, interpreter] of TYPES) {
137
142
  ...workerHooks,
138
143
  onWorkerReady(_, xworker) {
139
144
  assign(xworker.sync, sync);
145
+ for (const callback of hooks.onWorkerReady)
146
+ callback(_, xworker);
140
147
  },
141
148
  onBeforeRun(wrap, element) {
142
149
  currentElement = element;
@@ -209,9 +216,13 @@ for (const [TYPE, interpreter] of TYPES) {
209
216
  defineProperty(element, "target", { value: show });
210
217
 
211
218
  // notify before the code runs
212
- dispatch(element, TYPE);
213
- wrap[`run${isAsync ? "Async" : ""}`](
214
- await fetchSource(element, wrap.io, true),
219
+ dispatch(element, TYPE, "ready");
220
+ dispatchDone(
221
+ element,
222
+ isAsync,
223
+ wrap[`run${isAsync ? "Async" : ""}`](
224
+ await fetchSource(element, wrap.io, true),
225
+ ),
215
226
  );
216
227
  } else {
217
228
  // resolve PyScriptElement to allow connectedCallback
@@ -244,18 +255,21 @@ for (const [TYPE, interpreter] of TYPES) {
244
255
  async connectedCallback() {
245
256
  if (!this.executed) {
246
257
  this.executed = true;
258
+ const isAsync = this.hasAttribute("async");
247
259
  const { io, run, runAsync } = await this._wrap.promise;
248
- const runner = this.hasAttribute("async") ? runAsync : run;
249
260
  this.srcCode = await fetchSource(
250
261
  this,
251
262
  io,
252
263
  !this.childElementCount,
253
264
  );
254
265
  this.replaceChildren();
255
- // notify before the code runs
256
- dispatch(this, TYPE);
257
- runner(this.srcCode);
258
266
  this.style.display = "block";
267
+ dispatch(this, TYPE, "ready");
268
+ dispatchDone(
269
+ this,
270
+ isAsync,
271
+ (isAsync ? runAsync : run)(this.srcCode),
272
+ );
259
273
  }
260
274
  }
261
275
  }
package/src/hooks.js CHANGED
@@ -1,21 +1,28 @@
1
+ import { typedSet } from "type-checked-collections";
2
+
3
+ const SetFunction = typedSet({ typeof: "function" });
4
+ const SetString = typedSet({ typeof: "string" });
5
+
1
6
  export default {
2
7
  /** @type {Set<function>} */
3
- onBeforeRun: new Set(),
8
+ onInterpreterReady: new SetFunction(),
4
9
  /** @type {Set<function>} */
5
- onBeforeRunAsync: new Set(),
10
+ onBeforeRun: new SetFunction(),
6
11
  /** @type {Set<function>} */
7
- onAfterRun: new Set(),
12
+ onBeforeRunAsync: new SetFunction(),
8
13
  /** @type {Set<function>} */
9
- onAfterRunAsync: new Set(),
14
+ onAfterRun: new SetFunction(),
10
15
  /** @type {Set<function>} */
11
- onInterpreterReady: new Set(),
16
+ onAfterRunAsync: new SetFunction(),
12
17
 
18
+ /** @type {Set<function>} */
19
+ onWorkerReady: new SetFunction(),
13
20
  /** @type {Set<string>} */
14
- codeBeforeRunWorker: new Set(),
21
+ codeBeforeRunWorker: new SetString(),
15
22
  /** @type {Set<string>} */
16
- codeBeforeRunWorkerAsync: new Set(),
23
+ codeBeforeRunWorkerAsync: new SetString(),
17
24
  /** @type {Set<string>} */
18
- codeAfterRunWorker: new Set(),
25
+ codeAfterRunWorker: new SetString(),
19
26
  /** @type {Set<string>} */
20
- codeAfterRunWorkerAsync: new Set(),
27
+ codeAfterRunWorkerAsync: new SetString(),
21
28
  };
@@ -3,7 +3,7 @@ import html
3
3
  import io
4
4
  import re
5
5
 
6
- from pyscript.magic_js import document, window, current_target
6
+ from pyscript.magic_js import current_target, document, window
7
7
 
8
8
  _MIME_METHODS = {
9
9
  "__repr__": "text/plain",
@@ -154,8 +154,10 @@ def display(*values, target=None, append=True):
154
154
  # if element is a <script type="py">, it has a 'target' attribute which
155
155
  # points to the visual element holding the displayed values. In that case,
156
156
  # use that.
157
- if element.tagName == 'SCRIPT' and hasattr(element, 'target'):
157
+ if element.tagName == "SCRIPT" and hasattr(element, "target"):
158
158
  element = element.target
159
159
 
160
160
  for v in values:
161
+ if not append:
162
+ element.replaceChildren()
161
163
  _write(element, v, append=append)
@@ -2,7 +2,7 @@
2
2
  export default {
3
3
  "pyscript": {
4
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_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 _write(element, v, append=append)\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",
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
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"
package/types/hooks.d.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  declare namespace _default {
2
+ let onInterpreterReady: Set<Function>;
2
3
  let onBeforeRun: Set<Function>;
3
4
  let onBeforeRunAsync: Set<Function>;
4
5
  let onAfterRun: Set<Function>;
5
6
  let onAfterRunAsync: Set<Function>;
6
- let onInterpreterReady: Set<Function>;
7
+ let onWorkerReady: Set<Function>;
7
8
  let codeBeforeRunWorker: Set<string>;
8
9
  let codeBeforeRunWorkerAsync: Set<string>;
9
10
  let codeAfterRunWorker: Set<string>;