Lists and nested iteration
Understand the moving parts
Walk through the code
from pysx import Fragment, each, pysx, signal
def app() -> Fragment:
groups = signal(["Read", "Build"])
snapshot = len(groups())
selected = signal("None")
def group(name: str) -> Fragment:
def child(label: str) -> Fragment:
def choose(_: object) -> None:
selected.set(f"{name} / {label}")
choice_id = name + "-" + label
return pysx(t"""
button(data-choice={choice_id} onClick={choose}): {label}
""")
return pysx(t"""
li(data-group={name})
h3: {name}
div: {each(("first", "second"), child, key=str)}
""")
def add(_: object) -> None:
if len(groups()) < 12:
groups.update(lambda rows: [*rows, f"Group {len(rows) + 1}"])
def reverse(_: object) -> None:
groups.update(lambda rows: list(reversed(rows)))
return pysx(t"""
div:
button(id="group-add" onClick={add}): "Add group"
button(id="group-reverse" onClick={reverse}): "Reverse groups"
ul(id="groups"): {each(groups, group, key=str)}
output(id="group-selection"): {selected}
p: "Snapshot group count: " {snapshot}
""")Try it live
What to observe
Read Build
Snapshot group count: