@pyscript/core 0.1.18 → 0.1.20

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 (52) hide show
  1. package/README.md +19 -2
  2. package/dist/core.js +3 -3
  3. package/dist/core.js.map +1 -1
  4. package/dist/error-87e0706c.js +2 -0
  5. package/dist/error-87e0706c.js.map +1 -0
  6. package/docs/README.md +3 -12
  7. package/package.json +4 -3
  8. package/src/config.js +110 -0
  9. package/src/core.js +84 -106
  10. package/src/fetch.js +3 -0
  11. package/src/plugins/error.js +1 -1
  12. package/src/plugins.js +1 -1
  13. package/src/stdlib/pyscript/__init__.py +34 -0
  14. package/src/stdlib/pyscript/display.py +154 -0
  15. package/src/stdlib/pyscript/event_handling.py +45 -0
  16. package/src/stdlib/pyscript/magic_js.py +32 -0
  17. package/src/stdlib/pyscript/util.py +22 -0
  18. package/src/stdlib/pyscript.js +9 -5
  19. package/src/stdlib/pyweb/pydom.py +314 -0
  20. package/tests/integration/__init__.py +0 -0
  21. package/tests/integration/conftest.py +184 -0
  22. package/tests/integration/support.py +1038 -0
  23. package/tests/integration/test_00_support.py +495 -0
  24. package/tests/integration/test_01_basic.py +353 -0
  25. package/tests/integration/test_02_display.py +452 -0
  26. package/tests/integration/test_03_element.py +303 -0
  27. package/tests/integration/test_assets/line_plot.png +0 -0
  28. package/tests/integration/test_assets/tripcolor.png +0 -0
  29. package/tests/integration/test_async.py +197 -0
  30. package/tests/integration/test_event_handling.py +193 -0
  31. package/tests/integration/test_importmap.py +66 -0
  32. package/tests/integration/test_interpreter.py +98 -0
  33. package/tests/integration/test_plugins.py +419 -0
  34. package/tests/integration/test_py_config.py +294 -0
  35. package/tests/integration/test_py_repl.py +663 -0
  36. package/tests/integration/test_py_terminal.py +270 -0
  37. package/tests/integration/test_runtime_attributes.py +64 -0
  38. package/tests/integration/test_script_type.py +121 -0
  39. package/tests/integration/test_shadow_root.py +33 -0
  40. package/tests/integration/test_splashscreen.py +124 -0
  41. package/tests/integration/test_stdio_handling.py +370 -0
  42. package/tests/integration/test_style.py +47 -0
  43. package/tests/integration/test_warnings_and_banners.py +32 -0
  44. package/tests/integration/test_zz_examples.py +419 -0
  45. package/tests/integration/test_zzz_docs_snippets.py +305 -0
  46. package/types/config.d.ts +3 -0
  47. package/types/core.d.ts +1 -2
  48. package/types/fetch.d.ts +1 -0
  49. package/types/plugins/error.d.ts +1 -1
  50. package/types/stdlib/pyscript.d.ts +8 -4
  51. package/dist/error-91f1c2f6.js +0 -2
  52. package/dist/error-91f1c2f6.js.map +0 -1
@@ -0,0 +1,45 @@
1
+ import inspect
2
+
3
+ from pyodide.ffi.wrappers import add_event_listener
4
+ from pyscript.magic_js import document
5
+
6
+
7
+ def when(event_type=None, selector=None):
8
+ """
9
+ Decorates a function and passes py-* events to the decorated function
10
+ The events might or not be an argument of the decorated function
11
+ """
12
+
13
+ def decorator(func):
14
+ if isinstance(selector, str):
15
+ elements = document.querySelectorAll(selector)
16
+ else:
17
+ # TODO: This is a hack that will be removed when pyscript becomes a package
18
+ # and we can better manage the imports without circular dependencies
19
+ from pyweb import pydom
20
+
21
+ if isinstance(selector, pydom.Element):
22
+ elements = [selector._js]
23
+ elif isinstance(selector, pydom.ElementCollection):
24
+ elements = [el._js for el in selector]
25
+ else:
26
+ raise ValueError(
27
+ f"Invalid selector: {selector}. Selector must"
28
+ " be a string, a pydom.Element or a pydom.ElementCollection."
29
+ )
30
+
31
+ sig = inspect.signature(func)
32
+ # Function doesn't receive events
33
+ if not sig.parameters:
34
+
35
+ def wrapper(*args, **kwargs):
36
+ func()
37
+
38
+ for el in elements:
39
+ add_event_listener(el, event_type, wrapper)
40
+ else:
41
+ for el in elements:
42
+ add_event_listener(el, event_type, func)
43
+ return func
44
+
45
+ return decorator
@@ -0,0 +1,32 @@
1
+ from pyscript.util import NotSupported
2
+ import js as globalThis
3
+
4
+ RUNNING_IN_WORKER = not hasattr(globalThis, "document")
5
+
6
+ if RUNNING_IN_WORKER:
7
+ import polyscript
8
+
9
+ PyWorker = NotSupported(
10
+ 'pyscript.PyWorker',
11
+ 'pyscript.PyWorker works only when running in the main thread')
12
+ window = polyscript.xworker.window
13
+ document = window.document
14
+ sync = polyscript.xworker.sync
15
+
16
+ # in workers the display does not have a default ID
17
+ # but there is a sync utility from xworker
18
+ def current_target():
19
+ return polyscript.target
20
+
21
+ else:
22
+ import _pyscript
23
+ from _pyscript import PyWorker
24
+ window = globalThis
25
+ document = globalThis.document
26
+ sync = NotSupported(
27
+ 'pyscript.sync',
28
+ 'pyscript.sync works only when running in a worker')
29
+
30
+ # in MAIN the current element target exist, just use it
31
+ def current_target():
32
+ return _pyscript.target
@@ -0,0 +1,22 @@
1
+ class NotSupported:
2
+ """
3
+ Small helper that raises exceptions if you try to get/set any attribute on
4
+ it.
5
+ """
6
+
7
+ def __init__(self, name, error):
8
+ # we set attributes using self.__dict__ to bypass the __setattr__
9
+ self.__dict__['name'] = name
10
+ self.__dict__['error'] = error
11
+
12
+ def __repr__(self):
13
+ return f'<NotSupported {self.name} [{self.error}]>'
14
+
15
+ def __getattr__(self, attr):
16
+ raise AttributeError(self.error)
17
+
18
+ def __setattr__(self, attr, value):
19
+ raise AttributeError(self.error)
20
+
21
+ def __call__(self, *args):
22
+ raise TypeError(self.error)
@@ -1,9 +1,13 @@
1
1
  // ⚠️ This file is an artifact: DO NOT MODIFY
2
2
  export default {
3
- "_pyscript": {
4
- "__init__.py": "import js as window\n\nIS_WORKER = not hasattr(window, \"document\")\n\nif IS_WORKER:\n from polyscript import xworker as _xworker\n\n window = _xworker.window\n document = window.document\n sync = _xworker.sync\nelse:\n document = window.document\n",
5
- "display.py": "import base64\nimport html\nimport io\nimport re\n\nfrom . import 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_png_\": \"image/png\",\n \"_repr_pdf_\": \"application/pdf\",\n \"_repr_jpeg_\": \"image/jpeg\",\n \"_repr_latex\": \"text/latex\",\n \"_repr_json_\": \"application/json\",\n \"_repr_javascript_\": \"application/javascript\",\n \"savefig\": \"image/png\",\n}\n\n\ndef _render_image(mime, value, meta):\n # If the image value is using bytes we should convert it to base64\n # otherwise it will return raw bytes and the browser will not be able to\n # render it.\n if isinstance(value, bytes):\n value = base64.b64encode(value).decode(\"utf-8\")\n\n # This is the pattern of base64 strings\n base64_pattern = re.compile(\n r\"^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$\"\n )\n # If value doesn't match the base64 pattern we should encode it to base64\n if len(value) > 0 and not base64_pattern.match(value):\n value = base64.b64encode(value.encode(\"utf-8\")).decode(\"utf-8\")\n\n data = f\"data:{mime};charset=utf-8;base64,{value}\"\n attrs = \" \".join(['{k}=\"{v}\"' for k, v in meta.items()])\n return f'<img src=\"{data}\" {attrs}></img>'\n\n\ndef _identity(value, meta):\n return value\n\n\n_MIME_RENDERERS = {\n \"text/plain\": html.escape,\n \"text/html\": _identity,\n \"image/png\": lambda value, meta: _render_image(\"image/png\", value, meta),\n \"image/jpeg\": lambda value, meta: _render_image(\"image/jpeg\", value, meta),\n \"image/svg+xml\": _identity,\n \"application/json\": _identity,\n \"application/javascript\": lambda value, meta: f\"<script>{value}<\\\\/script>\",\n}\n\n\nclass HTML:\n \"\"\"\n Wrap a string so that display() can render it as plain HTML\n \"\"\"\n\n def __init__(self, html):\n self._html = html\n\n def _repr_html_(self):\n return self._html\n\n\ndef _eval_formatter(obj, print_method):\n \"\"\"\n Evaluates a formatter method.\n \"\"\"\n if print_method == \"__repr__\":\n return repr(obj)\n elif hasattr(obj, print_method):\n if print_method == \"savefig\":\n buf = io.BytesIO()\n obj.savefig(buf, format=\"png\")\n buf.seek(0)\n return base64.b64encode(buf.read()).decode(\"utf-8\")\n return getattr(obj, print_method)()\n elif print_method == \"_repr_mimebundle_\":\n return {}, {}\n return None\n\n\ndef _format_mime(obj):\n \"\"\"\n Formats object using _repr_x_ methods.\n \"\"\"\n if isinstance(obj, str):\n return html.escape(obj), \"text/plain\"\n\n mimebundle = _eval_formatter(obj, \"_repr_mimebundle_\")\n if isinstance(mimebundle, tuple):\n format_dict, _ = mimebundle\n else:\n format_dict = mimebundle\n\n output, not_available = None, []\n for method, mime_type in reversed(_MIME_METHODS.items()):\n if mime_type in format_dict:\n output = format_dict[mime_type]\n else:\n output = _eval_formatter(obj, method)\n\n if output is None:\n continue\n elif mime_type not in _MIME_RENDERERS:\n not_available.append(mime_type)\n continue\n break\n if output is None:\n if not_available:\n window.console.warn(\n f\"Rendered object requested unavailable MIME renderers: {not_available}\"\n )\n output = repr(output)\n mime_type = \"text/plain\"\n elif isinstance(output, tuple):\n output, meta = output\n else:\n meta = {}\n return _MIME_RENDERERS[mime_type](output, meta), mime_type\n\n\ndef _write(element, value, append=False):\n html, mime_type = _format_mime(value)\n if html == \"\\\\n\":\n return\n\n if append:\n out_element = document.createElement(\"div\")\n element.append(out_element)\n else:\n out_element = element.lastElementChild\n if out_element is None:\n out_element = element\n\n if mime_type in (\"application/javascript\", \"text/html\"):\n script_element = document.createRange().createContextualFragment(html)\n out_element.append(script_element)\n else:\n out_element.innerHTML = html\n\n\ndef display(*values, target=None, append=True):\n element = document.getElementById(target)\n for v in values:\n _write(element, v, append=append)\n",
6
- "event_handling.py": "import inspect\n\nfrom pyodide.ffi.wrappers import add_event_listener\nfrom pyscript 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 elements = document.querySelectorAll(selector)\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"
3
+ "pyscript": {
4
+ "__init__.py": "# Some notes about the naming conventions and the relationship between various\n# similar-but-different names.\n#\n# import pyscript\n# this package contains the main user-facing API offered by pyscript. All\n# the names which are supposed be used by end users should be made\n# available in pyscript/__init__.py (i.e., this file)\n#\n# import _pyscript\n# this is an internal module implemented in JS. It is used internally by\n# the pyscript package, end users should not use it directly. For its\n# implementation, grep for `interpreter.registerJsModule(\"_pyscript\",\n# ...)` in core.js\n#\n# import js\n# this is the JS globalThis, as exported by pyodide and/or micropython's\n# FFIs. As such, it contains different things in the main thread or in a\n# worker.\n#\n# import pyscript.magic_js\n# this submodule abstracts away some of the differences between the main\n# thread and the worker. In particular, it defines `window` and `document`\n# in such a way that these names work in both cases: in the main thread,\n# they are the \"real\" objects, in the worker they are proxies which work\n# thanks to coincident.\n#\n# from pyscript import window, document\n# these are just the window and document objects as defined by\n# pyscript.magic_js. This is the blessed way to access them from pyscript,\n# as it works transparently in both the main thread and worker cases.\n\nfrom pyscript.magic_js import RUNNING_IN_WORKER, window, document, sync\nfrom pyscript.display import HTML, display\nfrom pyscript.event_handling import when\n",
5
+ "display.py": "import base64\nimport html\nimport io\nimport re\n\nfrom pyscript.magic_js import document, window, current_target\n\n_MIME_METHODS = {\n \"__repr__\": \"text/plain\",\n \"_repr_html_\": \"text/html\",\n \"_repr_markdown_\": \"text/markdown\",\n \"_repr_svg_\": \"image/svg+xml\",\n \"_repr_png_\": \"image/png\",\n \"_repr_pdf_\": \"application/pdf\",\n \"_repr_jpeg_\": \"image/jpeg\",\n \"_repr_latex\": \"text/latex\",\n \"_repr_json_\": \"application/json\",\n \"_repr_javascript_\": \"application/javascript\",\n \"savefig\": \"image/png\",\n}\n\n\ndef _render_image(mime, value, meta):\n # If the image value is using bytes we should convert it to base64\n # otherwise it will return raw bytes and the browser will not be able to\n # render it.\n if isinstance(value, bytes):\n value = base64.b64encode(value).decode(\"utf-8\")\n\n # This is the pattern of base64 strings\n base64_pattern = re.compile(\n r\"^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$\"\n )\n # If value doesn't match the base64 pattern we should encode it to base64\n if len(value) > 0 and not base64_pattern.match(value):\n value = base64.b64encode(value.encode(\"utf-8\")).decode(\"utf-8\")\n\n data = f\"data:{mime};charset=utf-8;base64,{value}\"\n attrs = \" \".join(['{k}=\"{v}\"' for k, v in meta.items()])\n return f'<img src=\"{data}\" {attrs}></img>'\n\n\ndef _identity(value, meta):\n return value\n\n\n_MIME_RENDERERS = {\n \"text/plain\": html.escape,\n \"text/html\": _identity,\n \"image/png\": lambda value, meta: _render_image(\"image/png\", value, meta),\n \"image/jpeg\": lambda value, meta: _render_image(\"image/jpeg\", value, meta),\n \"image/svg+xml\": _identity,\n \"application/json\": _identity,\n \"application/javascript\": lambda value, meta: f\"<script>{value}<\\\\/script>\",\n}\n\n\nclass HTML:\n \"\"\"\n Wrap a string so that display() can render it as plain HTML\n \"\"\"\n\n def __init__(self, html):\n self._html = html\n\n def _repr_html_(self):\n return self._html\n\n\ndef _eval_formatter(obj, print_method):\n \"\"\"\n Evaluates a formatter method.\n \"\"\"\n if print_method == \"__repr__\":\n return repr(obj)\n elif hasattr(obj, print_method):\n if print_method == \"savefig\":\n buf = io.BytesIO()\n obj.savefig(buf, format=\"png\")\n buf.seek(0)\n return base64.b64encode(buf.read()).decode(\"utf-8\")\n return getattr(obj, print_method)()\n elif print_method == \"_repr_mimebundle_\":\n return {}, {}\n return None\n\n\ndef _format_mime(obj):\n \"\"\"\n Formats object using _repr_x_ methods.\n \"\"\"\n if isinstance(obj, str):\n return html.escape(obj), \"text/plain\"\n\n mimebundle = _eval_formatter(obj, \"_repr_mimebundle_\")\n if isinstance(mimebundle, tuple):\n format_dict, _ = mimebundle\n else:\n format_dict = mimebundle\n\n output, not_available = None, []\n for method, mime_type in reversed(_MIME_METHODS.items()):\n if mime_type in format_dict:\n output = format_dict[mime_type]\n else:\n output = _eval_formatter(obj, method)\n\n if output is None:\n continue\n elif mime_type not in _MIME_RENDERERS:\n not_available.append(mime_type)\n continue\n break\n if output is None:\n if not_available:\n window.console.warn(\n f\"Rendered object requested unavailable MIME renderers: {not_available}\"\n )\n output = repr(output)\n mime_type = \"text/plain\"\n elif isinstance(output, tuple):\n output, meta = output\n else:\n meta = {}\n return _MIME_RENDERERS[mime_type](output, meta), mime_type\n\n\ndef _write(element, value, append=False):\n html, mime_type = _format_mime(value)\n if html == \"\\\\n\":\n return\n\n if append:\n out_element = document.createElement(\"div\")\n element.append(out_element)\n else:\n out_element = element.lastElementChild\n if out_element is None:\n out_element = element\n\n if mime_type in (\"application/javascript\", \"text/html\"):\n script_element = document.createRange().createContextualFragment(html)\n out_element.append(script_element)\n else:\n out_element.innerHTML = html\n\n\ndef display(*values, target=None, append=True):\n if target is None:\n target = current_target()\n\n element = document.getElementById(target)\n for v in values:\n _write(element, v, append=append)\n",
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",
8
+ "util.py": "class NotSupported:\n \"\"\"\n Small helper that raises exceptions if you try to get/set any attribute on\n it.\n \"\"\"\n\n def __init__(self, name, error):\n # we set attributes using self.__dict__ to bypass the __setattr__\n self.__dict__['name'] = name\n self.__dict__['error'] = error\n\n def __repr__(self):\n return f'<NotSupported {self.name} [{self.error}]>'\n\n def __getattr__(self, attr):\n raise AttributeError(self.error)\n\n def __setattr__(self, attr, value):\n raise AttributeError(self.error)\n\n def __call__(self, *args):\n raise TypeError(self.error)\n"
7
9
  },
8
- "pyscript.py": "# export only what we want to expose as `pyscript` module\n# but not what is WORKER/MAIN dependent\nfrom _pyscript import window, document, IS_WORKER\nfrom _pyscript.display import HTML, display as _display\nfrom _pyscript.event_handling import when\n\n# this part is needed to disambiguate between MAIN and WORKER\nif IS_WORKER:\n # in workers the display does not have a default ID\n # but there is a sync utility from xworker\n import polyscript as _polyscript\n from _pyscript import sync\n\n def current_target():\n return _polyscript.target\n\nelse:\n # in MAIN both PyWorker and current element target exist\n # so these are both exposed and the display will use,\n # if not specified otherwise, such current element target\n import _pyscript_js\n\n PyWorker = _pyscript_js.PyWorker\n\n def current_target():\n return _pyscript_js.target\n\n\n# the display provides a handy default target either in MAIN or WORKER\ndef display(*values, target=None, append=True):\n if target is None:\n target = current_target()\n\n return _display(*values, target=target, append=append)\n"
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\n# from pyscript import when as _when\n\nalert = window.alert\n\n\nclass BaseElement:\n def __init__(self, js_element):\n self._js = js_element\n self._parent = None\n self.style = StyleProxy(self)\n\n def __eq__(self, obj):\n \"\"\"Check if the element is the same as the other element by comparing\n the underlying JS element\"\"\"\n return isinstance(obj, BaseElement) and obj._js == self._js\n\n @property\n def parent(self):\n if self._parent:\n return self._parent\n\n if self._js.parentElement:\n self._parent = self.__class__(self._js.parentElement)\n\n return self._parent\n\n @property\n def __class(self):\n return self.__class__ if self.__class__ != PyDom else Element\n\n def create(self, type_, is_child=True, classes=None, html=None, label=None):\n js_el = document.createElement(type_)\n element = self.__class(js_el)\n\n if classes:\n for class_ in classes:\n element.add_class(class_)\n\n if html is not None:\n element.html = html\n\n if label is not None:\n element.label = label\n\n if is_child:\n self.append(element)\n\n return element\n\n def find(self, selector):\n \"\"\"Return an ElementCollection representing all the child elements that\n match the specified selector.\n\n Args:\n selector (str): A string containing a selector expression\n\n Returns:\n ElementCollection: A collection of elements matching the selector\n \"\"\"\n elements = self._js.querySelectorAll(selector)\n if not elements:\n return None\n return ElementCollection([Element(el) for el in elements])\n\n\nclass Element(BaseElement):\n @property\n def children(self):\n return [self.__class__(el) for el in self._js.children]\n\n def append(self, child):\n # TODO: this is Pyodide specific for now!!!!!!\n # if we get passed a JSProxy Element directly we just map it to the\n # higher level Python element\n if isinstance(child, JsProxy):\n return self.append(Element(child))\n\n elif isinstance(child, Element):\n self._js.appendChild(child._js)\n\n return child\n\n elif isinstance(child, ElementCollection):\n for el in child:\n self.append(el)\n\n # -------- Pythonic Interface to Element -------- #\n @property\n def html(self):\n return self._js.innerHTML\n\n @html.setter\n def html(self, value):\n self._js.innerHTML = value\n\n @property\n def content(self):\n # TODO: This breaks with with standard template elements. Define how to best\n # handle this specifica use case. Just not support for now?\n if self._js.tagName == \"TEMPLATE\":\n warnings.warn(\n \"Content attribute not supported for template elements.\", stacklevel=2\n )\n return None\n return self._js.innerHTML\n\n @content.setter\n def content(self, value):\n # TODO: (same comment as above)\n if self._js.tagName == \"TEMPLATE\":\n warnings.warn(\n \"Content attribute not supported for template elements.\", stacklevel=2\n )\n return\n\n display(value, target=self.id)\n\n @property\n def id(self):\n return self._js.id\n\n @id.setter\n def id(self, value):\n self._js.id = value\n\n def clone(self, new_id=None):\n clone = Element(self._js.cloneNode(True))\n clone.id = new_id\n\n return clone\n\n def remove_class(self, classname):\n classList = self._js.classList\n if isinstance(classname, list):\n classList.remove(*classname)\n else:\n classList.remove(classname)\n return self\n\n def add_class(self, classname):\n classList = self._js.classList\n if isinstance(classname, list):\n classList.add(*classname)\n else:\n self._js.classList.add(classname)\n return self\n\n @property\n def classes(self):\n classes = self._js.classList.values()\n return [x for x in classes]\n\n def show_me(self):\n self._js.scrollIntoView()\n\n def when(self, event, handler):\n document.when(event, selector=self)(handler)\n\n\nclass StyleProxy(dict):\n def __init__(self, element: Element) -> None:\n self._element = element\n\n @cached_property\n def _style(self):\n return self._element._js.style\n\n def __getitem__(self, key):\n return self._style.getPropertyValue(key)\n\n def __setitem__(self, key, value):\n self._style.setProperty(key, value)\n\n def remove(self, key):\n self._style.removeProperty(key)\n\n def set(self, **kws):\n for k, v in kws.items():\n self._element._js.style.setProperty(k, v)\n\n # CSS Properties\n # Reference: https://github.com/microsoft/TypeScript/blob/main/src/lib/dom.generated.d.ts#L3799C1-L5005C2\n # Following prperties automatically generated from the above reference using\n # tools/codegen_css_proxy.py\n @property\n def visible(self):\n return self._element._js.style.visibility\n\n @visible.setter\n def visible(self, value):\n self._element._js.style.visibility = value\n\n\nclass StyleCollection:\n def __init__(self, collection: \"ElementCollection\") -> None:\n self._collection = collection\n\n def __get__(self, obj, objtype=None):\n return obj._get_attribute(\"style\")\n\n def __getitem__(self, key):\n return self._collection._get_attribute(\"style\")[key]\n\n def __setitem__(self, key, value):\n for element in self._collection._elements:\n element.style[key] = value\n\n def remove(self, key):\n for element in self._collection._elements:\n element.style.remove(key)\n\n\nclass ElementCollection:\n def __init__(self, elements: [Element]) -> None:\n self._elements = elements\n self.style = StyleCollection(self)\n\n def __getitem__(self, key):\n # If it's an integer we use it to access the elements in the collection\n if isinstance(key, int):\n return self._elements[key]\n # If it's a slice we use it to support slice operations over the elements\n # in the collection\n elif isinstance(key, slice):\n return ElementCollection(self._elements[key])\n\n # If it's anything else (basically a string) we use it as a selector\n # TODO: Write tests!\n elements = self._element.querySelectorAll(key)\n return ElementCollection([Element(el) for el in elements])\n\n def __len__(self):\n return len(self._elements)\n\n def __eq__(self, obj):\n \"\"\"Check if the element is the same as the other element by comparing\n the underlying JS element\"\"\"\n return isinstance(obj, ElementCollection) and obj._elements == self._elements\n\n def _get_attribute(self, attr, index=None):\n if index is None:\n return [getattr(el, attr) for el in self._elements]\n\n # As JQuery, when getting an attr, only return it for the first element\n return getattr(self._elements[index], attr)\n\n def _set_attribute(self, attr, value):\n for el in self._elements:\n setattr(el, attr, value)\n\n @property\n def html(self):\n return self._get_attribute(\"html\")\n\n @html.setter\n def html(self, value):\n self._set_attribute(\"html\", value)\n\n @property\n def children(self):\n return self._elements\n\n def __iter__(self):\n yield from self._elements\n\n def __repr__(self):\n return f\"{self.__class__.__name__} (length: {len(self._elements)}) {self._elements}\"\n\n\nclass DomScope:\n def __getattr__(self, __name: str) -> Any:\n element = document[f\"#{__name}\"]\n if element:\n return element[0]\n\n\nclass PyDom(BaseElement):\n # Add objects we want to expose to the DOM namespace since this class instance is being\n # remapped as \"the module\" itself\n BaseElement = BaseElement\n Element = Element\n ElementCollection = ElementCollection\n\n def __init__(self):\n super().__init__(document)\n self.ids = DomScope()\n self.body = Element(document.body)\n self.head = Element(document.head)\n\n def create(self, type_, parent=None, classes=None, html=None):\n return super().create(type_, is_child=False)\n\n def __getitem__(self, key):\n if isinstance(key, int):\n indices = range(*key.indices(len(self.list)))\n return [self.list[i] for i in indices]\n\n elements = self._js.querySelectorAll(key)\n if not elements:\n return None\n return ElementCollection([Element(el) for el in elements])\n\n\ndom = PyDom()\n\nsys.modules[__name__] = dom\n"
12
+ }
9
13
  };
@@ -0,0 +1,314 @@
1
+ import sys
2
+ import warnings
3
+ from functools import cached_property
4
+ from typing import Any
5
+
6
+ from pyodide.ffi import JsProxy
7
+ from pyscript import display, document, window
8
+
9
+ # from pyscript import when as _when
10
+
11
+ alert = window.alert
12
+
13
+
14
+ class BaseElement:
15
+ def __init__(self, js_element):
16
+ self._js = js_element
17
+ self._parent = None
18
+ self.style = StyleProxy(self)
19
+
20
+ def __eq__(self, obj):
21
+ """Check if the element is the same as the other element by comparing
22
+ the underlying JS element"""
23
+ return isinstance(obj, BaseElement) and obj._js == self._js
24
+
25
+ @property
26
+ def parent(self):
27
+ if self._parent:
28
+ return self._parent
29
+
30
+ if self._js.parentElement:
31
+ self._parent = self.__class__(self._js.parentElement)
32
+
33
+ return self._parent
34
+
35
+ @property
36
+ def __class(self):
37
+ return self.__class__ if self.__class__ != PyDom else Element
38
+
39
+ def create(self, type_, is_child=True, classes=None, html=None, label=None):
40
+ js_el = document.createElement(type_)
41
+ element = self.__class(js_el)
42
+
43
+ if classes:
44
+ for class_ in classes:
45
+ element.add_class(class_)
46
+
47
+ if html is not None:
48
+ element.html = html
49
+
50
+ if label is not None:
51
+ element.label = label
52
+
53
+ if is_child:
54
+ self.append(element)
55
+
56
+ return element
57
+
58
+ def find(self, selector):
59
+ """Return an ElementCollection representing all the child elements that
60
+ match the specified selector.
61
+
62
+ Args:
63
+ selector (str): A string containing a selector expression
64
+
65
+ Returns:
66
+ ElementCollection: A collection of elements matching the selector
67
+ """
68
+ elements = self._js.querySelectorAll(selector)
69
+ if not elements:
70
+ return None
71
+ return ElementCollection([Element(el) for el in elements])
72
+
73
+
74
+ class Element(BaseElement):
75
+ @property
76
+ def children(self):
77
+ return [self.__class__(el) for el in self._js.children]
78
+
79
+ def append(self, child):
80
+ # TODO: this is Pyodide specific for now!!!!!!
81
+ # if we get passed a JSProxy Element directly we just map it to the
82
+ # higher level Python element
83
+ if isinstance(child, JsProxy):
84
+ return self.append(Element(child))
85
+
86
+ elif isinstance(child, Element):
87
+ self._js.appendChild(child._js)
88
+
89
+ return child
90
+
91
+ elif isinstance(child, ElementCollection):
92
+ for el in child:
93
+ self.append(el)
94
+
95
+ # -------- Pythonic Interface to Element -------- #
96
+ @property
97
+ def html(self):
98
+ return self._js.innerHTML
99
+
100
+ @html.setter
101
+ def html(self, value):
102
+ self._js.innerHTML = value
103
+
104
+ @property
105
+ def content(self):
106
+ # TODO: This breaks with with standard template elements. Define how to best
107
+ # handle this specifica use case. Just not support for now?
108
+ if self._js.tagName == "TEMPLATE":
109
+ warnings.warn(
110
+ "Content attribute not supported for template elements.", stacklevel=2
111
+ )
112
+ return None
113
+ return self._js.innerHTML
114
+
115
+ @content.setter
116
+ def content(self, value):
117
+ # TODO: (same comment as above)
118
+ if self._js.tagName == "TEMPLATE":
119
+ warnings.warn(
120
+ "Content attribute not supported for template elements.", stacklevel=2
121
+ )
122
+ return
123
+
124
+ display(value, target=self.id)
125
+
126
+ @property
127
+ def id(self):
128
+ return self._js.id
129
+
130
+ @id.setter
131
+ def id(self, value):
132
+ self._js.id = value
133
+
134
+ def clone(self, new_id=None):
135
+ clone = Element(self._js.cloneNode(True))
136
+ clone.id = new_id
137
+
138
+ return clone
139
+
140
+ def remove_class(self, classname):
141
+ classList = self._js.classList
142
+ if isinstance(classname, list):
143
+ classList.remove(*classname)
144
+ else:
145
+ classList.remove(classname)
146
+ return self
147
+
148
+ def add_class(self, classname):
149
+ classList = self._js.classList
150
+ if isinstance(classname, list):
151
+ classList.add(*classname)
152
+ else:
153
+ self._js.classList.add(classname)
154
+ return self
155
+
156
+ @property
157
+ def classes(self):
158
+ classes = self._js.classList.values()
159
+ return [x for x in classes]
160
+
161
+ def show_me(self):
162
+ self._js.scrollIntoView()
163
+
164
+ def when(self, event, handler):
165
+ document.when(event, selector=self)(handler)
166
+
167
+
168
+ class StyleProxy(dict):
169
+ def __init__(self, element: Element) -> None:
170
+ self._element = element
171
+
172
+ @cached_property
173
+ def _style(self):
174
+ return self._element._js.style
175
+
176
+ def __getitem__(self, key):
177
+ return self._style.getPropertyValue(key)
178
+
179
+ def __setitem__(self, key, value):
180
+ self._style.setProperty(key, value)
181
+
182
+ def remove(self, key):
183
+ self._style.removeProperty(key)
184
+
185
+ def set(self, **kws):
186
+ for k, v in kws.items():
187
+ self._element._js.style.setProperty(k, v)
188
+
189
+ # CSS Properties
190
+ # Reference: https://github.com/microsoft/TypeScript/blob/main/src/lib/dom.generated.d.ts#L3799C1-L5005C2
191
+ # Following prperties automatically generated from the above reference using
192
+ # tools/codegen_css_proxy.py
193
+ @property
194
+ def visible(self):
195
+ return self._element._js.style.visibility
196
+
197
+ @visible.setter
198
+ def visible(self, value):
199
+ self._element._js.style.visibility = value
200
+
201
+
202
+ class StyleCollection:
203
+ def __init__(self, collection: "ElementCollection") -> None:
204
+ self._collection = collection
205
+
206
+ def __get__(self, obj, objtype=None):
207
+ return obj._get_attribute("style")
208
+
209
+ def __getitem__(self, key):
210
+ return self._collection._get_attribute("style")[key]
211
+
212
+ def __setitem__(self, key, value):
213
+ for element in self._collection._elements:
214
+ element.style[key] = value
215
+
216
+ def remove(self, key):
217
+ for element in self._collection._elements:
218
+ element.style.remove(key)
219
+
220
+
221
+ class ElementCollection:
222
+ def __init__(self, elements: [Element]) -> None:
223
+ self._elements = elements
224
+ self.style = StyleCollection(self)
225
+
226
+ def __getitem__(self, key):
227
+ # If it's an integer we use it to access the elements in the collection
228
+ if isinstance(key, int):
229
+ return self._elements[key]
230
+ # If it's a slice we use it to support slice operations over the elements
231
+ # in the collection
232
+ elif isinstance(key, slice):
233
+ return ElementCollection(self._elements[key])
234
+
235
+ # If it's anything else (basically a string) we use it as a selector
236
+ # TODO: Write tests!
237
+ elements = self._element.querySelectorAll(key)
238
+ return ElementCollection([Element(el) for el in elements])
239
+
240
+ def __len__(self):
241
+ return len(self._elements)
242
+
243
+ def __eq__(self, obj):
244
+ """Check if the element is the same as the other element by comparing
245
+ the underlying JS element"""
246
+ return isinstance(obj, ElementCollection) and obj._elements == self._elements
247
+
248
+ def _get_attribute(self, attr, index=None):
249
+ if index is None:
250
+ return [getattr(el, attr) for el in self._elements]
251
+
252
+ # As JQuery, when getting an attr, only return it for the first element
253
+ return getattr(self._elements[index], attr)
254
+
255
+ def _set_attribute(self, attr, value):
256
+ for el in self._elements:
257
+ setattr(el, attr, value)
258
+
259
+ @property
260
+ def html(self):
261
+ return self._get_attribute("html")
262
+
263
+ @html.setter
264
+ def html(self, value):
265
+ self._set_attribute("html", value)
266
+
267
+ @property
268
+ def children(self):
269
+ return self._elements
270
+
271
+ def __iter__(self):
272
+ yield from self._elements
273
+
274
+ def __repr__(self):
275
+ return f"{self.__class__.__name__} (length: {len(self._elements)}) {self._elements}"
276
+
277
+
278
+ class DomScope:
279
+ def __getattr__(self, __name: str) -> Any:
280
+ element = document[f"#{__name}"]
281
+ if element:
282
+ return element[0]
283
+
284
+
285
+ class PyDom(BaseElement):
286
+ # Add objects we want to expose to the DOM namespace since this class instance is being
287
+ # remapped as "the module" itself
288
+ BaseElement = BaseElement
289
+ Element = Element
290
+ ElementCollection = ElementCollection
291
+
292
+ def __init__(self):
293
+ super().__init__(document)
294
+ self.ids = DomScope()
295
+ self.body = Element(document.body)
296
+ self.head = Element(document.head)
297
+
298
+ def create(self, type_, parent=None, classes=None, html=None):
299
+ return super().create(type_, is_child=False)
300
+
301
+ def __getitem__(self, key):
302
+ if isinstance(key, int):
303
+ indices = range(*key.indices(len(self.list)))
304
+ return [self.list[i] for i in indices]
305
+
306
+ elements = self._js.querySelectorAll(key)
307
+ if not elements:
308
+ return None
309
+ return ElementCollection([Element(el) for el in elements])
310
+
311
+
312
+ dom = PyDom()
313
+
314
+ sys.modules[__name__] = dom
File without changes
@@ -0,0 +1,184 @@
1
+ import shutil
2
+ import threading
3
+ from http.server import HTTPServer as SuperHTTPServer
4
+ from http.server import SimpleHTTPRequestHandler
5
+
6
+ import pytest
7
+
8
+ from .support import Logger
9
+
10
+
11
+ def pytest_cmdline_main(config):
12
+ """
13
+ If we pass --clear-http-cache, we don't enter the main pytest logic, but
14
+ use our custom main instead
15
+ """
16
+
17
+ def mymain(config, session):
18
+ print()
19
+ print("-" * 20, "SmartRouter HTTP cache", "-" * 20)
20
+ # unfortunately pytest-cache doesn't offer a public API to selectively
21
+ # clear the cache, so we need to peek its internal. The good news is
22
+ # that pytest-cache is very old, stable and robust, so it's likely
23
+ # that this won't break anytime soon.
24
+ cache = config.cache
25
+ base = cache._cachedir.joinpath(cache._CACHE_PREFIX_VALUES, "pyscript")
26
+ if not base.exists():
27
+ print("No cache found, nothing to do")
28
+ return 0
29
+ #
30
+ print("Requests found in the cache:")
31
+ for f in base.rglob("*"):
32
+ if f.is_file():
33
+ # requests are saved in dirs named pyscript/http:/foo/bar, let's turn
34
+ # them into a proper url
35
+ url = str(f.relative_to(base))
36
+ url = url.replace(":/", "://")
37
+ print(" ", url)
38
+ shutil.rmtree(base)
39
+ print("Cache cleared")
40
+ return 0
41
+
42
+ if config.option.clear_http_cache:
43
+ from _pytest.main import wrap_session
44
+
45
+ return wrap_session(config, mymain)
46
+ return None
47
+
48
+
49
+ def pytest_configure(config):
50
+ """
51
+ THIS IS A WORKAROUND FOR A pytest QUIRK!
52
+
53
+ At the moment of writing this conftest defines two new options, --dev and
54
+ --no-fake-server, but because of how pytest works, they are available only
55
+ if this is the "root conftest" for the test session.
56
+
57
+ This means that if you are in the pyscriptjs directory:
58
+
59
+ $ py.test # does NOT work
60
+ $ py.test tests/integration/ # works
61
+
62
+ This happens because there is also test py-unit directory, so in the first
63
+ case the "root conftest" would be tests/conftest.py (which doesn't exist)
64
+ instead of this.
65
+
66
+ There are various workarounds, but for now we can just detect it and
67
+ inform the user.
68
+
69
+ Related StackOverflow answer: https://stackoverflow.com/a/51733980
70
+ """
71
+ if not hasattr(config.option, "dev"):
72
+ msg = """
73
+ Running a bare "pytest" command from the pyscriptjs directory
74
+ is not supported. Please use one of the following commands:
75
+ - pytest tests/integration
76
+ - pytest tests/py-unit
77
+ - pytest tests/*
78
+ - cd tests/integration; pytest
79
+ """
80
+ pytest.fail(msg)
81
+ else:
82
+ if config.option.dev:
83
+ config.option.headed = True
84
+ config.option.no_fake_server = True
85
+
86
+
87
+ @pytest.fixture(scope="session")
88
+ def logger():
89
+ return Logger()
90
+
91
+
92
+ def pytest_addoption(parser):
93
+ parser.addoption(
94
+ "--no-fake-server",
95
+ action="store_true",
96
+ help="Use a real HTTP server instead of http://fakeserver",
97
+ )
98
+ parser.addoption(
99
+ "--dev",
100
+ action="store_true",
101
+ help="Automatically open a devtools panel. Implies --headed and --no-fake-server",
102
+ )
103
+ parser.addoption(
104
+ "--clear-http-cache",
105
+ action="store_true",
106
+ help="Clear the cache of HTTP requests for SmartRouter",
107
+ )
108
+
109
+
110
+ @pytest.fixture(scope="session")
111
+ def browser_type_launch_args(request):
112
+ """
113
+ Override the browser_type_launch_args defined by pytest-playwright to
114
+ support --devtools.
115
+
116
+ NOTE: this has been tested with pytest-playwright==0.3.0. It might break
117
+ with newer versions of it.
118
+ """
119
+ # this calls the "original" fixture defined by pytest_playwright.py
120
+ launch_options = request.getfixturevalue("browser_type_launch_args")
121
+ if request.config.option.dev:
122
+ launch_options["devtools"] = True
123
+ return launch_options
124
+
125
+
126
+ class DevServer(SuperHTTPServer):
127
+ """
128
+ Class for wrapper to run SimpleHTTPServer on Thread.
129
+ Ctrl +Only Thread remains dead when terminated with C.
130
+ Keyboard Interrupt passes.
131
+ """
132
+
133
+ def __init__(self, base_url, *args, **kwargs):
134
+ self.base_url = base_url
135
+ super().__init__(*args, **kwargs)
136
+
137
+ def run(self):
138
+ try:
139
+ self.serve_forever()
140
+ except KeyboardInterrupt:
141
+ pass
142
+ finally:
143
+ self.server_close()
144
+
145
+
146
+ @pytest.fixture(scope="session")
147
+ def dev_server(logger):
148
+ class MyHTTPRequestHandler(SimpleHTTPRequestHandler):
149
+ enable_cors_headers = True
150
+
151
+ @classmethod
152
+ def my_headers(cls):
153
+ if cls.enable_cors_headers:
154
+ return {
155
+ "Cross-Origin-Embedder-Policy": "require-corp",
156
+ "Cross-Origin-Opener-Policy": "same-origin",
157
+ }
158
+ return {}
159
+
160
+ def end_headers(self):
161
+ self.send_my_headers()
162
+ SimpleHTTPRequestHandler.end_headers(self)
163
+
164
+ def send_my_headers(self):
165
+ for k, v in self.my_headers().items():
166
+ self.send_header(k, v)
167
+
168
+ def log_message(self, fmt, *args):
169
+ logger.log("http_server", fmt % args, color="blue")
170
+
171
+ host, port = "localhost", 8080
172
+ base_url = f"http://{host}:{port}"
173
+
174
+ # serve_Run forever under thread
175
+ server = DevServer(base_url, (host, port), MyHTTPRequestHandler)
176
+
177
+ thread = threading.Thread(None, server.run)
178
+ thread.start()
179
+
180
+ yield server # Transition to test here
181
+
182
+ # End thread
183
+ server.shutdown()
184
+ thread.join()