Form is formlessness, formlessness is form.
--The Heart Sutra
Formlessness is a Python library for handling the backend work of web forms. It helps with serialization, validation and generating the form specification for the frontend.
- Easier dynamic generation of everything form related.
- Simple representation of complex validation logic.
- High extendability.
from pprint import pprint
from formlessness.forms import BasicForm
from formlessness.fields import IntField, StrField
from formlessness.constraints import GE
form = BasicForm(
label='Person',
children=[
BasicForm(
label='Name',
children=[
StrField(label='First Name'),
StrField(label='Last Name'),
]
),
IntField(label='Age', required=False, extra_data_constraints=[GE(0)])
],
)
pprint(form.display(), sort_dicts=False)
This is the form definition for the front end.
{'type': 'form',
'label': 'Person',
'collapsable': False,
'collapsed': False,
'contents': [{'type': 'form',
'label': 'Name',
'collapsable': False,
'collapsed': False,
'contents': [{'type': 'field',
'label': 'First Name',
'widget': {'type': 'text'},
'objectPath': '/name/first_name'},
{'type': 'field',
'label': 'Last Name',
'widget': {'type': 'text'},
'objectPath': '/name/last_name'}],
'objectPath': '/name'},
{'type': 'field',
'label': 'Age',
'widget': {'type': 'text'},
'objectPath': '/age'}],
'objectPath': ''}
A JSON Schema is automatically generated.
pprint(form.data_schema(), sort_dicts=False)
{'$schema': 'http://json-schema.org/draft-07/schema#',
'type': 'object',
'properties': {'name': {'type': 'object',
'properties': {'first_name': {'type': 'string'},
'last_name': {'type': 'string'}},
'required': ['first_name', 'last_name'],
'unevaluatedProperties': False},
'age': {'allOf': [{'type': 'integer'}, {'minimum': 0}]}},
'required': ['name'],
'unevaluatedProperties': False}
If you're on MacOS with brew:
brew install poetry
Otherwise, follow the installation docs.
poetry install
pytest
pre-commit run -a
mkdocs serve
Formlessness is not an official Netflix project.