Skip to content
Open
Changes from all commits
Commits
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
55 changes: 44 additions & 11 deletions puepy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ def __init__(
if "id" in kwargs:
self._element_id = kwargs["id"]
elif self._page and self._page.application:
self._element_id = self._page.application.element_id_generator.get_id_for_element(self)
self._element_id = (
self._page.application.element_id_generator.get_id_for_element(self)
)
else:
self._element_id = f"ppauto-{id(self)}"

Expand All @@ -153,7 +155,9 @@ def __init__(
if isinstance(parent_component, Component):
self.parent_component = parent_component
elif parent_component:
raise Exception(f"Unknown parent_component type {type(parent_component)}: {repr(parent_component)}")
raise Exception(
f"Unknown parent_component type {type(parent_component)}: {repr(parent_component)}"
)
else:
self.parent_component = None

Expand Down Expand Up @@ -182,7 +186,9 @@ def _handle_bind(self, kwargs):
input_type = kwargs.get("type")
tag_name = self.tag_name.lower()

if "value" in kwargs and not (tag_name == "input" and input_type == "radio"):
if "value" in kwargs and not (
tag_name == "input" and input_type == "radio"
):
raise Exception("Cannot specify both 'bind' and 'value'")

else:
Expand Down Expand Up @@ -556,7 +562,9 @@ def redraw(self):
if is_server_side:
old_active_element_id = None
else:
old_active_element_id = self.document.activeElement.id if self.document.activeElement else None
old_active_element_id = (
self.document.activeElement.id if self.document.activeElement else None
)

self.recursive_call("_retain_implicit_attrs")

Expand Down Expand Up @@ -726,7 +734,9 @@ def _on_state_change(self, context, key, value):
if key in redraw_rule:
self.page.redraw_tag(self)
else:
raise Exception(f"Unknown value for redraw rule: {redraw_rule} (context: {context})")
raise Exception(
f"Unknown value for redraw rule: {redraw_rule} (context: {context})"
)

def insert_slot(self, name="default", **kwargs):
"""
Expand All @@ -740,9 +750,17 @@ def insert_slot(self, name="default", **kwargs):
Slot: The inserted slot object.
"""
if name in self.slots:
self.slots[name].parent = Tag.stack[-1] # The children will be cleared during redraw, so re-establish
self.slots[name].parent = Tag.stack[
-1
] # The children will be cleared during redraw, so re-establish
else:
self.slots[name] = Slot(ref=f"slot={name}", slot_name=name, page=self.page, parent=Tag.stack[-1], **kwargs)
self.slots[name] = Slot(
ref=f"slot={name}",
slot_name=name,
page=self.page,
parent=Tag.stack[-1],
**kwargs,
)
slot = self.slots[name]
if self.origin:
slot.origin = self.origin
Expand Down Expand Up @@ -780,11 +798,24 @@ def __exit__(self, exc_type, exc_val, exc_tb):
return False

def __str__(self):
return f"{self.component_name or self.__class__.__name__} ({self.ref} {id(self)})"
return (
f"{self.component_name or self.__class__.__name__} ({self.ref} {id(self)})"
)

def __repr__(self):
return f"<{self}>"

def trigger_redraw(self):
"""
Triggers a redraw of this component and its children.
"""
if hasattr(self, "page") and self.page:
self.page.redraw_tag(self)
else:
# Fallback: try to redraw self if page is not set
if hasattr(self, "redraw_tag"):
self.redraw_tag(self)


class Page(Component):
def __init__(self, matched_route=None, application=None, **kwargs):
Expand Down Expand Up @@ -877,9 +908,9 @@ def generate_tag(self, tag_name, *children, **kwargs):
tag_name = tag_name.replace("_", "-")

if tag_name == "insert_slot":
print(f"Called t.insert_slot. Did you mean self.insert_slot?")
print("Called t.insert_slot. Did you mean self.insert_slot?")
elif tag_name == "slot":
print(f"Called t.slot. Did you mean <component>.slot?")
print("Called t.slot. Did you mean <component>.slot?")

parent = Tag.stack[-1] if Tag.stack else None
parent_component = Tag.component_stack[-1] if Tag.component_stack else None
Expand All @@ -896,7 +927,9 @@ def generate_tag(self, tag_name, *children, **kwargs):
raise Exception("t.generate_tag called without a context")

# Determine ref value
ref_part = "__" + (f"{parent.ref}.{tag_name}_{len(parent.children) + 1}").lstrip("_")
ref_part = "__" + (
f"{parent.ref}.{tag_name}_{len(parent.children) + 1}"
).lstrip("_")

ref = kwargs.pop("ref", ref_part)

Expand Down