travisjungroth / formlessness

A Python library for making web forms on the back end
MIT License
5 stars 2 forks source link

Tests CodeQL License Code style: black codecov pre-commit.ci status

Formlessness

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.

Motivations

Example

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}

Development

Poetry

Installing Poetry

If you're on MacOS with brew:

brew install poetry

Otherwise, follow the installation docs.

Installing Dev Dependencies

poetry install

Testing

pytest

Formatting

pre-commit run -a

Run docs locally

mkdocs serve

Formlessness is not an official Netflix project.