@pyscript/core 0.3.7 → 0.3.9

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 (59) hide show
  1. package/dist/codemirror-3hMOlj_s.js +2 -0
  2. package/dist/codemirror-3hMOlj_s.js.map +1 -0
  3. package/dist/codemirror_commands-Y4OsE8Y2.js +2 -0
  4. package/dist/codemirror_commands-Y4OsE8Y2.js.map +1 -0
  5. package/dist/codemirror_lang-python-p0uky9oC.js +2 -0
  6. package/dist/codemirror_lang-python-p0uky9oC.js.map +1 -0
  7. package/dist/codemirror_language-U9l5bLBC.js +2 -0
  8. package/dist/codemirror_language-U9l5bLBC.js.map +1 -0
  9. package/dist/codemirror_state-zH-H1oSI.js +2 -0
  10. package/dist/codemirror_state-zH-H1oSI.js.map +1 -0
  11. package/dist/codemirror_view-tmIj4olz.js +2 -0
  12. package/dist/codemirror_view-tmIj4olz.js.map +1 -0
  13. package/dist/core-ifSZyON7.js +5 -0
  14. package/dist/core-ifSZyON7.js.map +1 -0
  15. package/dist/core.css +1 -1
  16. package/dist/core.js +1 -4
  17. package/dist/core.js.map +1 -1
  18. package/dist/deprecations-manager-u20LUlLl.js +2 -0
  19. package/dist/{deprecations-manager-uwH5iaZ9.js.map → deprecations-manager-u20LUlLl.js.map} +1 -1
  20. package/dist/error-ZSXf8h5x.js +2 -0
  21. package/dist/{error-96hMSEw8.js.map → error-ZSXf8h5x.js.map} +1 -1
  22. package/dist/index-qM2iyXcZ.js +2 -0
  23. package/dist/index-qM2iyXcZ.js.map +1 -0
  24. package/dist/py-editor-HVgDslnW.js +2 -0
  25. package/dist/py-editor-HVgDslnW.js.map +1 -0
  26. package/dist/py-terminal-lYFDY-Tg.js +2 -0
  27. package/dist/{py-terminal-qRUZOJY9.js.map → py-terminal-lYFDY-Tg.js.map} +1 -1
  28. package/dist/xterm-f2QfYNGL.js.map +1 -1
  29. package/dist/xterm-readline-ONk85xtH.js.map +1 -1
  30. package/dist/xterm.css +0 -1
  31. package/dist/xterm_addon-fit-E4yMPZTX.js.map +1 -1
  32. package/package.json +12 -5
  33. package/src/3rd-party/xterm-readline.js +0 -1
  34. package/src/3rd-party/xterm.css +0 -1
  35. package/src/3rd-party/xterm.js +0 -1
  36. package/src/3rd-party/xterm_addon-fit.js +0 -1
  37. package/src/config.js +38 -12
  38. package/src/core.css +38 -0
  39. package/src/exceptions.js +1 -1
  40. package/src/hooks.js +1 -1
  41. package/src/plugins/py-editor.js +229 -0
  42. package/src/plugins.js +1 -0
  43. package/src/stdlib/pyscript/magic_js.py +2 -1
  44. package/src/stdlib/pyscript.js +2 -2
  45. package/src/stdlib/pyweb/pydom.py +100 -0
  46. package/src/stdlib.js +7 -3
  47. package/types/3rd-party/codemirror.d.ts +1 -0
  48. package/types/3rd-party/codemirror_commands.d.ts +1 -0
  49. package/types/3rd-party/codemirror_lang-python.d.ts +1 -0
  50. package/types/3rd-party/codemirror_language.d.ts +1 -0
  51. package/types/3rd-party/codemirror_state.d.ts +1 -0
  52. package/types/3rd-party/codemirror_view.d.ts +1 -0
  53. package/types/plugins/py-editor.d.ts +2 -0
  54. package/types/plugins.d.ts +1 -0
  55. package/dist/deprecations-manager-uwH5iaZ9.js +0 -2
  56. package/dist/error-96hMSEw8.js +0 -2
  57. package/dist/py-terminal-qRUZOJY9.js +0 -2
  58. package/types/toml.d.ts +0 -12
  59. package/types/utils.d.ts +0 -1
@@ -5,4 +5,3 @@
5
5
  * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
6
6
  */
7
7
  var e,t,r={exports:{}};self;var s=r.exports=(e=t={},Object.defineProperty(e,"__esModule",{value:!0}),e.FitAddon=void 0,e.FitAddon=class{activate(e){this._terminal=e}dispose(){}fit(){const e=this.proposeDimensions();if(!e||!this._terminal||isNaN(e.cols)||isNaN(e.rows))return;const t=this._terminal._core;this._terminal.rows===e.rows&&this._terminal.cols===e.cols||(t._renderService.clear(),this._terminal.resize(e.cols,e.rows))}proposeDimensions(){if(!this._terminal)return;if(!this._terminal.element||!this._terminal.element.parentElement)return;const e=this._terminal._core,t=e._renderService.dimensions;if(0===t.css.cell.width||0===t.css.cell.height)return;const r=0===this._terminal.options.scrollback?0:e.viewport.scrollBarWidth,s=window.getComputedStyle(this._terminal.element.parentElement),i=parseInt(s.getPropertyValue("height")),o=Math.max(0,parseInt(s.getPropertyValue("width"))),n=window.getComputedStyle(this._terminal.element),l=i-(parseInt(n.getPropertyValue("padding-top"))+parseInt(n.getPropertyValue("padding-bottom"))),a=o-(parseInt(n.getPropertyValue("padding-right"))+parseInt(n.getPropertyValue("padding-left")))-r;return{cols:Math.max(2,Math.floor(a/t.css.cell.width)),rows:Math.max(1,Math.floor(l/t.css.cell.height))}}},t),i=r.exports.FitAddon,o=r.exports.__esModule;export{i as FitAddon,o as __esModule,s as default};
8
- //# sourceMappingURL=/sm/953d1f83f6be91dd2caca418b17a24b185a9453fac166d91d6521988e6344577.map
package/src/config.js CHANGED
@@ -3,15 +3,17 @@
3
3
  * to use as base config for all py-script elements, importing
4
4
  * also a queue of plugins *before* the interpreter (if any) resolves.
5
5
  */
6
- import { $ } from "basic-devtools";
6
+ import { $$ } from "basic-devtools";
7
7
 
8
8
  import TYPES from "./types.js";
9
9
  import allPlugins from "./plugins.js";
10
10
  import { robustFetch as fetch, getText } from "./fetch.js";
11
11
  import { ErrorCode } from "./exceptions.js";
12
12
 
13
+ const { BAD_CONFIG, CONFLICTING_CODE } = ErrorCode;
14
+
13
15
  const badURL = (url, expected = "") => {
14
- let message = `(${ErrorCode.BAD_CONFIG}): Invalid URL: ${url}`;
16
+ let message = `(${BAD_CONFIG}): Invalid URL: ${url}`;
15
17
  if (expected) message += `\nexpected ${expected} content`;
16
18
  throw new Error(message);
17
19
  };
@@ -41,8 +43,10 @@ const configDetails = async (config, type) => {
41
43
  return { json, toml: toml || (!json && !!text), text, url };
42
44
  };
43
45
 
46
+ const conflictError = (reason) => new Error(`(${CONFLICTING_CODE}): ${reason}`);
47
+
44
48
  const syntaxError = (type, url, { message }) => {
45
- let str = `(${ErrorCode.BAD_CONFIG}): Invalid ${type}`;
49
+ let str = `(${BAD_CONFIG}): Invalid ${type}`;
46
50
  if (url) str += ` @ ${url}`;
47
51
  return new SyntaxError(`${str}\n${message}`);
48
52
  };
@@ -56,27 +60,49 @@ for (const [TYPE] of TYPES) {
56
60
  /** @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} ) */
57
61
  let parsed;
58
62
 
59
- /** @type {SyntaxError | undefined} The error thrown when parsing the PyScript config, if any.*/
63
+ /** @type {Error | undefined} The error thrown when parsing the PyScript config, if any.*/
60
64
  let error;
61
65
 
62
66
  let config,
63
67
  type,
64
- pyConfig = $(`${TYPE}-config`);
65
- if (pyConfig) {
66
- config = pyConfig.getAttribute("src") || pyConfig.textContent;
67
- type = pyConfig.getAttribute("type");
68
- } else {
69
- pyConfig = $(
68
+ pyElement,
69
+ pyConfigs = $$(`${TYPE}-config`),
70
+ attrConfigs = $$(
70
71
  [
71
72
  `script[type="${TYPE}"][config]:not([worker])`,
72
73
  `${TYPE}-script[config]:not([worker])`,
73
74
  ].join(","),
74
75
  );
75
- if (pyConfig) config = pyConfig.getAttribute("config");
76
+
77
+ // throw an error if there are multiple <py-config> or <mpy-config>
78
+ if (pyConfigs.length > 1) {
79
+ error = conflictError(`Too many ${TYPE}-config`);
80
+ } else {
81
+ // throw an error if there are <x-config> and config="x" attributes
82
+ if (pyConfigs.length && attrConfigs.length) {
83
+ error = conflictError(
84
+ `Ambiguous ${TYPE}-config VS config attribute`,
85
+ );
86
+ } else if (pyConfigs.length) {
87
+ [pyElement] = pyConfigs;
88
+ config = pyElement.getAttribute("src") || pyElement.textContent;
89
+ type = pyElement.getAttribute("type");
90
+ } else if (attrConfigs.length) {
91
+ [pyElement, ...attrConfigs] = attrConfigs;
92
+ config = pyElement.getAttribute("config");
93
+ // throw an error if dirrent scripts use different configs
94
+ if (
95
+ attrConfigs.some((el) => el.getAttribute("config") !== config)
96
+ ) {
97
+ error = conflictError(
98
+ "Unable to use different configs on main",
99
+ );
100
+ }
101
+ }
76
102
  }
77
103
 
78
104
  // catch possible fetch errors
79
- if (config) {
105
+ if (!error && config) {
80
106
  try {
81
107
  const { json, toml, text, url } = await configDetails(config, type);
82
108
  config = text;
package/src/core.css CHANGED
@@ -4,3 +4,41 @@ mpy-script,
4
4
  mpy-config {
5
5
  display: none;
6
6
  }
7
+
8
+ /* PyEditor */
9
+ .py-editor-box,
10
+ .mpy-editor-box {
11
+ padding: 0.5rem;
12
+ }
13
+ .py-editor-input,
14
+ .mpy-editor-input {
15
+ position: relative;
16
+ }
17
+ .py-editor-box::before,
18
+ .mpy-editor-box::before {
19
+ content: attr(data-env);
20
+ display: block;
21
+ font-size: x-small;
22
+ text-align: end;
23
+ }
24
+ .py-editor-output,
25
+ .mpy-editor-output {
26
+ white-space: pre;
27
+ }
28
+ .py-editor-run-button,
29
+ .mpy-editor-run-button {
30
+ position: absolute;
31
+ right: 0.5rem;
32
+ bottom: 0.5rem;
33
+ opacity: 0;
34
+ transition: opacity 0.25s;
35
+ z-index: 1;
36
+ }
37
+ .py-editor-box:hover .py-editor-run-button,
38
+ .mpy-editor-box:hover .mpy-editor-run-button,
39
+ .py-editor-run-button:focus,
40
+ .py-editor-run-button:disabled,
41
+ .mpy-editor-run-button:focus,
42
+ .mpy-editor-run-button:disabled {
43
+ opacity: 1;
44
+ }
package/src/exceptions.js CHANGED
@@ -5,7 +5,7 @@ const CLOSEBUTTON =
5
5
 
6
6
  /**
7
7
  * These error codes are used to identify the type of error that occurred.
8
- * @see https://docs.pyscript.net/latest/reference/exceptions.html?highlight=errors
8
+ * @see https://pyscript.github.io/docs/latest/reference/exceptions.html?highlight=errors
9
9
  */
10
10
  export const ErrorCode = {
11
11
  GENERIC: "PY0000", // Use this only for development then change to a more specific error code
package/src/hooks.js CHANGED
@@ -50,7 +50,7 @@ const inputFailure = `
50
50
  def input(prompt=""):
51
51
  raise Exception("\\n ".join([
52
52
  "input() doesn't work when PyScript runs in the main thread.",
53
- "Consider using the worker attribute: https://docs.pyscript.net/2023.11.2/user-guide/workers/"
53
+ "Consider using the worker attribute: https://pyscript.github.io/docs/2023.11.2/user-guide/workers/"
54
54
  ]))
55
55
 
56
56
  builtins.input = input
@@ -0,0 +1,229 @@
1
+ // PyScript py-editor plugin
2
+ import { Hook, XWorker, dedent } from "polyscript/exports";
3
+ import { TYPES } from "../core.js";
4
+
5
+ const RUN_BUTTON = `<svg style="height:20px;width:20px;vertical-align:-.125em;transform-origin:center;overflow:visible;color:green" viewBox="0 0 384 512" aria-hidden="true" role="img" xmlns="http://www.w3.org/2000/svg"><g transform="translate(192 256)" transform-origin="96 0"><g transform="translate(0,0) scale(1,1)"><path d="M361 215C375.3 223.8 384 239.3 384 256C384 272.7 375.3 288.2 361 296.1L73.03 472.1C58.21 482 39.66 482.4 24.52 473.9C9.377 465.4 0 449.4 0 432V80C0 62.64 9.377 46.63 24.52 38.13C39.66 29.64 58.21 29.99 73.03 39.04L361 215z" fill="currentColor" transform="translate(-192 -256)"></path></g></g></svg>`;
6
+
7
+ let id = 0;
8
+ const getID = (type) => `${type}-editor-${id++}`;
9
+
10
+ const envs = new Map();
11
+
12
+ const hooks = {
13
+ worker: {
14
+ // works on both Pyodide and MicroPython
15
+ onReady: ({ runAsync, io }, { sync }) => {
16
+ io.stdout = (line) => sync.write(line);
17
+ io.stderr = (line) => sync.writeErr(line);
18
+ sync.revoke();
19
+ sync.runAsync = runAsync;
20
+ },
21
+ },
22
+ };
23
+
24
+ async function execute({ currentTarget }) {
25
+ const { env, pySrc, outDiv } = this;
26
+
27
+ currentTarget.disabled = true;
28
+ outDiv.innerHTML = "";
29
+
30
+ if (!envs.has(env)) {
31
+ const srcLink = URL.createObjectURL(new Blob([""]));
32
+ const xworker = XWorker.call(new Hook(null, hooks), srcLink, {
33
+ type: this.interpreter,
34
+ });
35
+
36
+ const { sync } = xworker;
37
+ const { promise, resolve } = Promise.withResolvers();
38
+ envs.set(env, promise);
39
+ sync.revoke = () => {
40
+ URL.revokeObjectURL(srcLink);
41
+ resolve(xworker);
42
+ };
43
+ }
44
+
45
+ // wait for the env then set the target div
46
+ // before executing the current code
47
+ envs.get(env).then((xworker) => {
48
+ xworker.onerror = ({ error }) => {
49
+ outDiv.innerHTML += `<span style='color:red'>${
50
+ error.message || error
51
+ }</span>\n`;
52
+ console.error(error);
53
+ };
54
+
55
+ const enable = () => {
56
+ currentTarget.disabled = false;
57
+ };
58
+ const { sync } = xworker;
59
+ sync.write = (str) => {
60
+ outDiv.innerText += `${str}\n`;
61
+ };
62
+ sync.writeErr = (str) => {
63
+ outDiv.innerHTML += `<span style='color:red'>${str}</span>\n`;
64
+ };
65
+ sync.runAsync(pySrc).then(enable, enable);
66
+ });
67
+ }
68
+
69
+ const makeRunButton = (listener, type) => {
70
+ const runButton = document.createElement("button");
71
+ runButton.className = `absolute ${type}-editor-run-button`;
72
+ runButton.innerHTML = RUN_BUTTON;
73
+ runButton.setAttribute("aria-label", "Python Script Run Button");
74
+ runButton.addEventListener("click", listener);
75
+ return runButton;
76
+ };
77
+
78
+ const makeEditorDiv = (listener, type) => {
79
+ const editorDiv = document.createElement("div");
80
+ editorDiv.className = `${type}-editor-input`;
81
+ editorDiv.setAttribute("aria-label", "Python Script Area");
82
+
83
+ const runButton = makeRunButton(listener, type);
84
+ const editorShadowContainer = document.createElement("div");
85
+
86
+ // avoid outer elements intercepting key events (reveal as example)
87
+ editorShadowContainer.addEventListener("keydown", (event) => {
88
+ event.stopPropagation();
89
+ });
90
+
91
+ editorDiv.append(runButton, editorShadowContainer);
92
+
93
+ return editorDiv;
94
+ };
95
+
96
+ const makeOutDiv = (type) => {
97
+ const outDiv = document.createElement("div");
98
+ outDiv.className = `${type}-editor-output`;
99
+ outDiv.id = `${getID(type)}-output`;
100
+ return outDiv;
101
+ };
102
+
103
+ const makeBoxDiv = (listener, type) => {
104
+ const boxDiv = document.createElement("div");
105
+ boxDiv.className = `${type}-editor-box`;
106
+
107
+ const editorDiv = makeEditorDiv(listener, type);
108
+ const outDiv = makeOutDiv(type);
109
+ boxDiv.append(editorDiv, outDiv);
110
+
111
+ return [boxDiv, outDiv];
112
+ };
113
+
114
+ const init = async (script, type, interpreter) => {
115
+ const [
116
+ { basicSetup, EditorView },
117
+ { Compartment },
118
+ { python },
119
+ { indentUnit },
120
+ { keymap },
121
+ { defaultKeymap },
122
+ ] = await Promise.all([
123
+ // TODO: find a way to actually produce these bundles locally
124
+ import(/* webpackIgnore: true */ "../3rd-party/codemirror.js"),
125
+ import(/* webpackIgnore: true */ "../3rd-party/codemirror_state.js"),
126
+ import(
127
+ /* webpackIgnore: true */ "../3rd-party/codemirror_lang-python.js"
128
+ ),
129
+ import(/* webpackIgnore: true */ "../3rd-party/codemirror_language.js"),
130
+ import(/* webpackIgnore: true */ "../3rd-party/codemirror_view.js"),
131
+ import(/* webpackIgnore: true */ "../3rd-party/codemirror_commands.js"),
132
+ ]);
133
+
134
+ const selector = script.getAttribute("target");
135
+
136
+ let target;
137
+ if (selector) {
138
+ target =
139
+ document.getElementById(selector) ||
140
+ document.querySelector(selector);
141
+ if (!target) throw new Error(`Unknown target ${selector}`);
142
+ } else {
143
+ target = document.createElement(`${type}-editor`);
144
+ target.style.display = "block";
145
+ script.after(target);
146
+ }
147
+
148
+ if (!target.id) target.id = getID(type);
149
+ if (!target.hasAttribute("exec-id")) target.setAttribute("exec-id", 0);
150
+ if (!target.hasAttribute("root")) target.setAttribute("root", target.id);
151
+
152
+ const env = `${interpreter}-${script.getAttribute("env") || getID(type)}`;
153
+ const context = {
154
+ interpreter,
155
+ env,
156
+ get pySrc() {
157
+ return editor.state.doc.toString();
158
+ },
159
+ get outDiv() {
160
+ return outDiv;
161
+ },
162
+ };
163
+
164
+ // @see https://github.com/JeffersGlass/mkdocs-pyscript/blob/main/mkdocs_pyscript/js/makeblocks.js
165
+ const listener = execute.bind(context);
166
+ const [boxDiv, outDiv] = makeBoxDiv(listener, type);
167
+ boxDiv.dataset.env = script.hasAttribute("env") ? env : interpreter;
168
+
169
+ const inputChild = boxDiv.querySelector(`.${type}-editor-input > div`);
170
+ const parent = inputChild.attachShadow({ mode: "open" });
171
+ // avoid inheriting styles from the outer component
172
+ parent.innerHTML = `<style> :host { all: initial; }</style>`;
173
+
174
+ target.appendChild(boxDiv);
175
+
176
+ const doc = dedent(script.textContent).trim();
177
+
178
+ // preserve user indentation, if any
179
+ const indentation = /^(\s+)/m.test(doc) ? RegExp.$1 : " ";
180
+
181
+ const editor = new EditorView({
182
+ extensions: [
183
+ indentUnit.of(indentation),
184
+ new Compartment().of(python()),
185
+ keymap.of([
186
+ ...defaultKeymap,
187
+ { key: "Ctrl-Enter", run: listener, preventDefault: true },
188
+ { key: "Cmd-Enter", run: listener, preventDefault: true },
189
+ { key: "Shift-Enter", run: listener, preventDefault: true },
190
+ ]),
191
+ basicSetup,
192
+ ],
193
+ parent,
194
+ doc,
195
+ });
196
+
197
+ editor.focus();
198
+ };
199
+
200
+ // avoid too greedy MutationObserver operations at distance
201
+ let timeout = 0;
202
+
203
+ // reset interval value then check for new scripts
204
+ const resetTimeout = () => {
205
+ timeout = 0;
206
+ pyEditor();
207
+ };
208
+
209
+ // triggered both ASAP on the living DOM and via MutationObserver later
210
+ const pyEditor = async () => {
211
+ if (timeout) return;
212
+ timeout = setTimeout(resetTimeout, 250);
213
+ for (const [type, interpreter] of TYPES) {
214
+ const selector = `script[type="${type}-editor"]`;
215
+ for (const script of document.querySelectorAll(selector)) {
216
+ // avoid any further bootstrap
217
+ script.type += "-active";
218
+ await init(script, type, interpreter);
219
+ }
220
+ }
221
+ };
222
+
223
+ new MutationObserver(pyEditor).observe(document, {
224
+ childList: true,
225
+ subtree: true,
226
+ });
227
+
228
+ // try to check the current document ASAP
229
+ export default pyEditor();
package/src/plugins.js CHANGED
@@ -2,5 +2,6 @@
2
2
  export default {
3
3
  ["deprecations-manager"]: () => import(/* webpackIgnore: true */ "./plugins/deprecations-manager.js"),
4
4
  error: () => import(/* webpackIgnore: true */ "./plugins/error.js"),
5
+ ["py-editor"]: () => import(/* webpackIgnore: true */ "./plugins/py-editor.js"),
5
6
  ["py-terminal"]: () => import(/* webpackIgnore: true */ "./plugins/py-terminal.js"),
6
7
  };
@@ -1,11 +1,11 @@
1
1
  import js as globalThis
2
2
  from polyscript import js_modules
3
-
4
3
  from pyscript.util import NotSupported
5
4
 
6
5
  RUNNING_IN_WORKER = not hasattr(globalThis, "document")
7
6
 
8
7
  if RUNNING_IN_WORKER:
8
+ import js
9
9
  import polyscript
10
10
 
11
11
  PyWorker = NotSupported(
@@ -14,6 +14,7 @@ if RUNNING_IN_WORKER:
14
14
  )
15
15
  window = polyscript.xworker.window
16
16
  document = window.document
17
+ js.document = document
17
18
  sync = polyscript.xworker.sync
18
19
 
19
20
  # in workers the display does not have a default ID
@@ -4,10 +4,10 @@ export default {
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.display import HTML, display\nfrom pyscript.magic_js import (\n RUNNING_IN_WORKER,\n PyWorker,\n current_target,\n document,\n js_modules,\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
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": "import js as globalThis\nfrom polyscript import js_modules\n\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",
7
+ "magic_js.py": "import js as globalThis\nfrom polyscript import js_modules\nfrom pyscript.util import NotSupported\n\nRUNNING_IN_WORKER = not hasattr(globalThis, \"document\")\n\nif RUNNING_IN_WORKER:\n import js\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 js.document = 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": {
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\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 @property\n def value(self):\n return self._js.value\n\n @value.setter\n def value(self, value):\n # in order to avoid confusion to the user, we don't allow setting the\n # value of elements that don't have a value attribute\n if not hasattr(self._js, \"value\"):\n raise AttributeError(\n f\"Element {self._js.tagName} has no value attribute. If you want to \"\n \"force a value attribute, set it directly using the `_js.value = <value>` \"\n \"javascript API attribute instead.\"\n )\n self._js.value = 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\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 value(self):\n return self._get_attribute(\"value\")\n\n @value.setter\n def value(self, value):\n self._set_attribute(\"value\", 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_, classes=None, html=None):\n return super().create(type_, is_child=False, classes=classes, html=html)\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"
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\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 self._proxies = {}\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 @property\n def options(self):\n if \"options\" in self._proxies:\n return self._proxies[\"options\"]\n\n if not self._js.tagName.lower() in {\"select\", \"datalist\", \"optgroup\"}:\n raise AttributeError(\n f\"Element {self._js.tagName} has no options attribute.\"\n )\n self._proxies[\"options\"] = OptionsProxy(self)\n return self._proxies[\"options\"]\n\n @property\n def value(self):\n return self._js.value\n\n @value.setter\n def value(self, value):\n # in order to avoid confusion to the user, we don't allow setting the\n # value of elements that don't have a value attribute\n if not hasattr(self._js, \"value\"):\n raise AttributeError(\n f\"Element {self._js.tagName} has no value attribute. If you want to \"\n \"force a value attribute, set it directly using the `_js.value = <value>` \"\n \"javascript API attribute instead.\"\n )\n self._js.value = value\n\n @property\n def selected(self):\n return self._js.selected\n\n @selected.setter\n def selected(self, value):\n # in order to avoid confusion to the user, we don't allow setting the\n # value of elements that don't have a value attribute\n if not hasattr(self._js, \"selected\"):\n raise AttributeError(\n f\"Element {self._js.tagName} has no value attribute. If you want to \"\n \"force a value attribute, set it directly using the `_js.value = <value>` \"\n \"javascript API attribute instead.\"\n )\n self._js.selected = 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\nclass OptionsProxy:\n \"\"\"This class represents the options of a select element. It\n allows to access to add and remove options by using the `add` and `remove` methods.\n \"\"\"\n\n def __init__(self, element: Element) -> None:\n self._element = element\n if self._element._js.tagName.lower() != \"select\":\n raise AttributeError(\n f\"Element {self._element._js.tagName} has no options attribute.\"\n )\n\n def add(\n self,\n value: Any = None,\n html: str = None,\n text: str = None,\n before: Element | int = None,\n **kws,\n ) -> None:\n \"\"\"Add a new option to the select element\"\"\"\n # create the option element and set the attributes\n option = document.createElement(\"option\")\n if value is not None:\n kws[\"value\"] = value\n if html is not None:\n option.innerHTML = html\n if text is not None:\n kws[\"text\"] = text\n\n for key, value in kws.items():\n option.setAttribute(key, value)\n\n if before:\n if isinstance(before, Element):\n before = before._js\n\n self._element._js.add(option, before)\n\n def remove(self, item: int) -> None:\n \"\"\"Remove the option at the specified index\"\"\"\n self._element._js.remove(item)\n\n def clear(self) -> None:\n \"\"\"Remove all the options\"\"\"\n for i in range(len(self)):\n self.remove(0)\n\n @property\n def options(self):\n \"\"\"Return the list of options\"\"\"\n return [Element(opt) for opt in self._element._js.options]\n\n @property\n def selected(self):\n \"\"\"Return the selected option\"\"\"\n return self.options[self._element._js.selectedIndex]\n\n def __iter__(self):\n yield from self.options\n\n def __len__(self):\n return len(self.options)\n\n def __repr__(self):\n return f\"{self.__class__.__name__} (length: {len(self)}) {self.options}\"\n\n def __getitem__(self, key):\n return self.options[key]\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 value(self):\n return self._get_attribute(\"value\")\n\n @value.setter\n def value(self, value):\n self._set_attribute(\"value\", 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_, classes=None, html=None):\n return super().create(type_, is_child=False, classes=classes, html=html)\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"
12
12
  }
13
13
  };
@@ -14,6 +14,7 @@ class BaseElement:
14
14
  self._js = js_element
15
15
  self._parent = None
16
16
  self.style = StyleProxy(self)
17
+ self._proxies = {}
17
18
 
18
19
  def __eq__(self, obj):
19
20
  """Check if the element is the same as the other element by comparing
@@ -129,6 +130,18 @@ class Element(BaseElement):
129
130
  def id(self, value):
130
131
  self._js.id = value
131
132
 
133
+ @property
134
+ def options(self):
135
+ if "options" in self._proxies:
136
+ return self._proxies["options"]
137
+
138
+ if not self._js.tagName.lower() in {"select", "datalist", "optgroup"}:
139
+ raise AttributeError(
140
+ f"Element {self._js.tagName} has no options attribute."
141
+ )
142
+ self._proxies["options"] = OptionsProxy(self)
143
+ return self._proxies["options"]
144
+
132
145
  @property
133
146
  def value(self):
134
147
  return self._js.value
@@ -145,6 +158,22 @@ class Element(BaseElement):
145
158
  )
146
159
  self._js.value = value
147
160
 
161
+ @property
162
+ def selected(self):
163
+ return self._js.selected
164
+
165
+ @selected.setter
166
+ def selected(self, value):
167
+ # in order to avoid confusion to the user, we don't allow setting the
168
+ # value of elements that don't have a value attribute
169
+ if not hasattr(self._js, "selected"):
170
+ raise AttributeError(
171
+ f"Element {self._js.tagName} has no value attribute. If you want to "
172
+ "force a value attribute, set it directly using the `_js.value = <value>` "
173
+ "javascript API attribute instead."
174
+ )
175
+ self._js.selected = value
176
+
148
177
  def clone(self, new_id=None):
149
178
  clone = Element(self._js.cloneNode(True))
150
179
  clone.id = new_id
@@ -176,6 +205,77 @@ class Element(BaseElement):
176
205
  self._js.scrollIntoView()
177
206
 
178
207
 
208
+ class OptionsProxy:
209
+ """This class represents the options of a select element. It
210
+ allows to access to add and remove options by using the `add` and `remove` methods.
211
+ """
212
+
213
+ def __init__(self, element: Element) -> None:
214
+ self._element = element
215
+ if self._element._js.tagName.lower() != "select":
216
+ raise AttributeError(
217
+ f"Element {self._element._js.tagName} has no options attribute."
218
+ )
219
+
220
+ def add(
221
+ self,
222
+ value: Any = None,
223
+ html: str = None,
224
+ text: str = None,
225
+ before: Element | int = None,
226
+ **kws,
227
+ ) -> None:
228
+ """Add a new option to the select element"""
229
+ # create the option element and set the attributes
230
+ option = document.createElement("option")
231
+ if value is not None:
232
+ kws["value"] = value
233
+ if html is not None:
234
+ option.innerHTML = html
235
+ if text is not None:
236
+ kws["text"] = text
237
+
238
+ for key, value in kws.items():
239
+ option.setAttribute(key, value)
240
+
241
+ if before:
242
+ if isinstance(before, Element):
243
+ before = before._js
244
+
245
+ self._element._js.add(option, before)
246
+
247
+ def remove(self, item: int) -> None:
248
+ """Remove the option at the specified index"""
249
+ self._element._js.remove(item)
250
+
251
+ def clear(self) -> None:
252
+ """Remove all the options"""
253
+ for i in range(len(self)):
254
+ self.remove(0)
255
+
256
+ @property
257
+ def options(self):
258
+ """Return the list of options"""
259
+ return [Element(opt) for opt in self._element._js.options]
260
+
261
+ @property
262
+ def selected(self):
263
+ """Return the selected option"""
264
+ return self.options[self._element._js.selectedIndex]
265
+
266
+ def __iter__(self):
267
+ yield from self.options
268
+
269
+ def __len__(self):
270
+ return len(self.options)
271
+
272
+ def __repr__(self):
273
+ return f"{self.__class__.__name__} (length: {len(self)}) {self.options}"
274
+
275
+ def __getitem__(self, key):
276
+ return self.options[key]
277
+
278
+
179
279
  class StyleProxy(dict):
180
280
  def __init__(self, element: Element) -> None:
181
281
  self._element = element
package/src/stdlib.js CHANGED
@@ -33,9 +33,13 @@ const write = (base, literal) => {
33
33
 
34
34
  write(".", pyscript);
35
35
 
36
- python.push("del _Path");
37
- python.push("del _path");
38
- python.push("del _os");
36
+ // in order to fix js.document in the Worker case
37
+ // we need to bootstrap pyscript module ASAP
38
+ python.push("import pyscript as _pyscript");
39
+
40
+ python.push(
41
+ ...["_Path", "_path", "_os", "_pyscript"].map((ref) => `del ${ref}`),
42
+ );
39
43
  python.push("\n");
40
44
 
41
45
  export default python.join("\n");
@@ -0,0 +1 @@
1
+ export * from "codemirror";
@@ -0,0 +1 @@
1
+ export * from "@codemirror/commands";
@@ -0,0 +1 @@
1
+ export * from "@codemirror/lang-python";