Form builder

A form builder like Typeform’s or Google Forms’:
examples/dnd/form_builder.py.
python examples/dnd/form_builder.py-
The palette copies. Its items are the kinds of field.
pull="clone"leaves them in the palette when dragged out andsort=Falsekeeps its order fixed:palette = pz.sortable(items=list(KINDS), item=self.palette_item, key=lambda k: f"palette-{k}",id="palette", group="fields", pull="clone", sort=False, placeholder="none") -
The form takes them. Same
group, so palette items can be dropped in it.handle=Truedrags its fields only by their grip, so clicking a field selects it:form = pz.sortable(items=self.fields, item=self.editor_item, id="form", group="fields",handle=True, spacing=8, padding=12, height="fill", on_reorder=self.drop) -
A template becomes a field. A drop from the palette carries the kind of field (
event.data,"email"); the handler makes a new field atevent.index. A drop from the form itself is a reorder:def drop(self, event):if event.from_list == "palette":field = new_field(event.data)self.fields.insert(event.index, field)self.selected = field["id"]else:pz.dnd.move(self.fields, event)pz.dnd.move(self.fields, event, make=new_field)would do both in one line. -
Only form fields go to the trash. The trash accepts the group and rules out palette items:
trash = pz.droppable(bin_, "trash", accepts="fields", can_drop=lambda e: e.from_list == "form",on_drop=self.remove) -
Edit and preview. Clicking a field selects it; the inspector edits its label, hint and whether it is required, and the preview draws the real form from the same list.
Ideas to go further
Section titled “Ideas to go further”- Sections: a field of kind
"section"with a list of its own (see nesting). - Save
self.fieldsas JSON and draw the same form in another app withpreview_field.