8000 The all-new, pyscript.web (ignore the branch name :) ) by mchilvers · Pull Request #2129 · pyscript/pyscript · GitHub
[go: up one dir, main page]

Skip to content

The all-new, pyscript.web (ignore the branch name :) ) #2129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 39 commits into from
Aug 1, 2024
Merged
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
d456b37
Minor cleanups: move all Element classes to bottom of module.
mchilvers Jul 24, 2024
73feddd
Commenting.
mchilvers Jul 24, 2024
d09963a
Commenting.
mchilvers Jul 24, 2024
0d3472f
Commenting.
mchilvers Jul 24, 2024
18a2f2b
Group dunder methods.
mchilvers Jul 24, 2024
035e2e1
Don't cache the element's parent.
mchilvers Jul 26, 2024
3714711
Remove style type check until we decide whether or not to add for cla…
mchilvers Jul 29, 2024
7d6f1e9
Add ability to register/unregister element classes.
mchilvers Jul 29, 2024
9e6f65a
Implement __iter__ for container elements.
mchilvers Jul 29, 2024
e2c0b88
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 29, 2024
8110fd7
Minor renaming to make it clear when we have an Element instance vs a…
mchilvers Jul 29, 2024
bef0a90
Merge branch 'mc/pyscript-web-minor-cleanup' of github.com:pyscript/p…
mchilvers Jul 29, 2024
7a066ec
remove duplication: added Element.get_tag_name
mchilvers Jul 29, 2024
24c1e37
Commenting.
mchilvers Jul 29, 2024
ab1178f
Allow Element.append to 1) use *args, 2) accept iterables
mchilvers Jul 29, 2024
52779ba
Remove iterable check - inteferes with js proxies.
mchilvers Jul 29, 2024
73ab702
Don't use *args, so it quacks more like a list ;)
mchilvers Jul 29, 2024
395c785
Element.append take 2 :)
mchilvers Jul 29, 2024
3725f0c
Remove unused code.
mchilvers Jul 29, 2024
fe7f70f
Move to web.py with a page object!
mchilvers Jul 31, 2024
bbee95a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 31, 2024
6e02a84
Added 'page.title' too :)
mchilvers Jul 31, 2024
38d147b
Merge branch 'mc/pyscript-web-minor-cleanup' of github.com:pyscript/p…
mchilvers Jul 31, 2024
ef94e29
Add __getitem__ as a shortcut for page.find
mchilvers Jul 31, 2024
b9b239b
Add Element.__getitem__ to be consistent
mchilvers Jul 31, 2024
ef4a185
Make __getitem__ consistent for Page, Element and ElementCollection.
mchilvers Jul 31, 2024
0d9b1fb
Docstringing.
mchilvers Jul 31, 2024
1eed03a
Docstringing.
mchilvers Jul 31, 2024
47bbf32
Docstringing/commenting.
mchilvers Jul 31, 2024
fb63c6c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 31, 2024
e5a84b9
fix select.add (revert InnerHTML->html)
mchilvers Jul 31, 2024
61c14fb
Merge branch 'mc/pyscript-web-minor-cleanup' of github.com:pyscript/p…
mchilvers Jul 31, 2024
2efbd8f
Commenting.
mchilvers Jul 31, 2024
d879cea
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 31, 2024
73b32e5
Hand-edit some of the AI :)
mchilvers Jul 31, 2024
2c759e8
Merge branch 'mc/pyscript-web-minor-cleanup' of github.com:pyscript/p…
mchilvers Jul 31, 2024
d4e7d39
Rename ElementCollection.children -> ElementCollection.elements
mchilvers Jul 31, 2024
19ac775
Merge branch 'main' into mc/pyscript-web-minor-cleanup
mchilvers Jul 31, 2024
bd025ec
Remove unnecessary guard.
mchilvers Jul 31, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Implement __iter__ for container elements.
  • Loading branch information
mchilvers committed Jul 29, 2024
commit 9e6f65a77f00eda4d5823b4f64a38c22f4703f3e
38 changes: 15 additions & 23 deletions pyscript.core/src/stdlib/pyscript/web/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def __getattr__(self, name):
# with Python keywords or built-ins (e.g. the output element has an
# attribute `for` which is a Python keyword, so you can access it on the
# Element instance via `for_`).
if name.endswith("_"):
if name.endswith("_") and not name.endswith("__"):
name = name[:-1]

return getattr(self._dom_element, name)
Expand All @@ -96,7 +96,7 @@ def __setattr__(self, name, value):
# with Python keywords or built-ins (e.g. the output element has an
# attribute `for` which is a Python keyword, so you can access it on the
# Element instance via `for_`).
if name.endswith("_"):
if name.endswith("_") and not name.endswith("__"):
name = name[:-1]

setattr(self._dom_element, name, value)
Expand Down Expand Up @@ -402,6 +402,9 @@ def __init__(
else:
self.innerHTML += child

def __iter__(self):
yield from self.children


class ClassesCollection:
def __init__(self, collection: "ElementCollection") -> None:
Expand Down Expand Up @@ -465,11 +468,10 @@ class StyleCollection:
def __init__(self, collection: "ElementCollection") -> None:
self._collection = collection

def __get__(self, obj, objtype=None):
return obj._get_attribute("style")

def __getitem__(self, key):
return self._collection._get_attribute("style")[key]
return [
element.style[key] for element in self._collection._elements
]

def __setitem__(self, key, value):
for element in self._collection._elements:
Expand Down Expand Up @@ -518,20 +520,21 @@ def __repr__(self):
f"{self._elements}"
)

def __getattr__(self, item):
return self._get_attribute(item)
def __getattr__(self, name):
return [getattr(el, name) for el in self._elements]

def __setattr__(self, key, value):
def __setattr__(self, name, value):
# This class overrides `__setattr__` to delegate "public" attributes to the
# elements in the collection. BUT, we don't use the usual Python pattern where
# we set attributes on the collection itself via `self.__dict__` as that is not
# yet supported in our build of MicroPython. Instead, we handle it here by
# using super for all "private" attributes (those starting with an underscore).
if key.startswith("_"):
super().__setattr__(key, value)
if name.startswith("_"):
super().__setattr__(name, value)

else:
self._set_attribute(key, value)
for el in self._elements:
setattr(el, name, value)

@property
def children(self):
Expand All @@ -552,17 +555,6 @@ def find(self, selector):

return ElementCollection(elements)

def _get_attribute(self, attr, index=None):
if index is None:
return [getattr(el, attr) for el in self._elements]

# As JQuery, when getting an attr, only return it for the first element
return getattr(self._elements[index], attr)

def _set_attribute(self, attr, value):
for el in self._elements:
setattr(el, attr, value)


# Classes for every HTML element. If the element tag name (e.g. "input") clashes with
# either a Python keyword or common symbol, then we suffix the class name with an "_"
Expand Down
0