@pyscript/core 0.1.19 → 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 (41) 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/docs/README.md +3 -12
  5. package/package.json +3 -3
  6. package/src/config.js +32 -24
  7. package/src/core.js +1 -1
  8. package/src/stdlib/pyscript/__init__.py +34 -0
  9. package/src/stdlib/pyscript/display.py +154 -0
  10. package/src/stdlib/pyscript/event_handling.py +45 -0
  11. package/src/stdlib/pyscript/magic_js.py +32 -0
  12. package/src/stdlib/pyscript/util.py +22 -0
  13. package/src/stdlib/pyscript.js +9 -5
  14. package/src/stdlib/pyweb/pydom.py +314 -0
  15. package/tests/integration/__init__.py +0 -0
  16. package/tests/integration/conftest.py +184 -0
  17. package/tests/integration/support.py +1038 -0
  18. package/tests/integration/test_00_support.py +495 -0
  19. package/tests/integration/test_01_basic.py +353 -0
  20. package/tests/integration/test_02_display.py +452 -0
  21. package/tests/integration/test_03_element.py +303 -0
  22. package/tests/integration/test_assets/line_plot.png +0 -0
  23. package/tests/integration/test_assets/tripcolor.png +0 -0
  24. package/tests/integration/test_async.py +197 -0
  25. package/tests/integration/test_event_handling.py +193 -0
  26. package/tests/integration/test_importmap.py +66 -0
  27. package/tests/integration/test_interpreter.py +98 -0
  28. package/tests/integration/test_plugins.py +419 -0
  29. package/tests/integration/test_py_config.py +294 -0
  30. package/tests/integration/test_py_repl.py +663 -0
  31. package/tests/integration/test_py_terminal.py +270 -0
  32. package/tests/integration/test_runtime_attributes.py +64 -0
  33. package/tests/integration/test_script_type.py +121 -0
  34. package/tests/integration/test_shadow_root.py +33 -0
  35. package/tests/integration/test_splashscreen.py +124 -0
  36. package/tests/integration/test_stdio_handling.py +370 -0
  37. package/tests/integration/test_style.py +47 -0
  38. package/tests/integration/test_warnings_and_banners.py +32 -0
  39. package/tests/integration/test_zz_examples.py +419 -0
  40. package/tests/integration/test_zzz_docs_snippets.py +305 -0
  41. package/types/stdlib/pyscript.d.ts +8 -4
@@ -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()