Skip to content

Page builder

Blocks dropped from a library onto a page and moved between two columns

A page builder like Webflow’s, Wix’s or Elementor’s: examples/dnd/page_builder.py.

Terminal window
python examples/dnd/page_builder.py
  1. Blocks hold blocks. A page is a list of blocks; a “Two columns” block has two lists of its own:

    def block(kind):
    b = {"id": f"block-{next(_ids)}", "kind": kind}
    if kind == "columns":
    b["left"], b["right"] = [], []
    return b
  2. Every list has an id. The page is "page" and each column is "<block id>:left" or ":right". A function finds them all, so one handler serves every list:

    def lists(self):
    found = {"page": self.page}
    for b in self.page:
    if b["kind"] == "columns":
    found[b["id"] + ":left"], found[b["id"] + ":right"] = b["left"], b["right"]
    return found
    def drop(self, event):
    if event.from_list == "library":
    self.lists()[event.target].insert(event.index, block(event.data))
    else:
    pz.dnd.move(self.lists(), event)
  3. Sortables inside sortables. A columns block draws two sortables, with the same group as the page. The deepest list under the pointer takes the drop; a block being dragged never takes itself.

    pz.sortable(items=b[side], item=self.render, id=f"{b['id']}:{side}", group="blocks",
    handle=True, padding=8, width="fill", on_reorder=self.drop,
    can_drop=lambda e: not is_columns(e.data)) # no columns in columns
  4. The library is a grid that copies. direction="wrap" lays it out in rows, pull="clone" and sort=False make it a palette.

  5. Phone width. A segmented control changes the page width, and the columns stack. The sortables animate to their new places.