Skip to main content

Module helps with generating field sets and fields for usage in the architecture aSTEP-2019 (At Aalborg Uversity). This can be used for any service written in flask, to make sure you don't create too much duplicate code. it's strongly inspired by Django.

Supported versions

Python >=3.5

Example

Create an instance of FieldSet which handles the different fields both visualized in the /fields/ endpoint as well as verified with /render/

An example with with basis in fields could be:

from astep_form_utils import FieldSet, StringField, FloatField
from flask import Flask, jsonify

app = Flask(__name__, static_url_path="")

@app.route("/fields")
def fields():
    field_set = FieldSet(
        StringField("my_name", label="Input Your Name:"),
        FloatField("my_age", label="Input Your Age:")
    )
    return jsonify(field_set.as_form_fields())

which will return json as follows:

[
  {
    "label": "Input Your Name:",
    "name": "my_name",
    "type": "input"
  },
  {
    "label": "Input Your Age:",
    "name": "my_age",
    "type": "input-number"
  }
]

You can also use enums:

from astep_form_utils import FieldSet, EnumField
from enum import Enum

class MyModel(Enum):
    PRETTY_MODEL = auto()
    UGLY_MODEL = auto()

field_set = FieldSet(
    EnumField("my_string", MyModel, label="Pick a model")
)

'

You can use a fieldset in your /render/ endpoint as well, to validate data:

from astep_form_utils import FieldSet, StringField, FloatField, is_data_empty
from flask import Flask, jsonify, request

app = Flask(__name__, static_url_path="")

@app.route("/render")
def render():
    field_set = FieldSet(
        StringField("my_name", label="Input Your Name:"),
        FloatField("my_age", label="Input Your Age:")
    )


    if is_data_empty(request):
        return jsonify({'chart_type':'text', 'content':'Supply data please!'})

    if field_set.is_valid(request):
        my_name = field_set.cleaned_data['my_name']
        my_age = field_set.cleaned_data['my_age']

        return jsonify({
            'chart_type':'text',
            'content':f'Hello my name is {my_name} and i am {my_age} years old.'
        })

To make it more DRY and reuse the FieldSet definition, you can utilize the factory function field_set_factory, to reuse the same definition like this:

from astep_form_utils import StringField, FloatField, \
    is_data_empty, field_set_factory
from flask import Flask, jsonify, request

app = Flask(__name__, static_url_path="")
# returns a method that creates a FieldSet with the two fields
FIELD_SET_FACTORY = field_set_factory(
        StringField("my_name", label="Input Your Name:"),
        FloatField("my_age", label="Input Your Age:")
)
@app.route("/render")
def render():
    # Creates a fieldset with the defined fields
    field_set =  FIELD_SET_FACTORY()

    if is_data_empty(request):
        return jsonify({'chart_type':'text', 'content':'Supply data please!'})

    if field_set.is_valid(request):
        my_name = field_set.cleaned_data['my_name']
        my_age = field_set.cleaned_data['my_age']

        return jsonify({
            'chart_type':'text',
            'content':f'Hello my name is {my_name} and i am {my_age} years old.'
        })

    raise NotImplementedError()

@app.route("/fields")
def fields():
    field_set = FIELD_SET_FACTORY() 
    return jsonify(field_set.as_form_fields())

A complete solution can be found in the implementation of the chart type test microservice.

Metadata

Release files for astep-forms-utils 0.2.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for astep-forms-utils 0.2.7
File Size Uploaded
astep_forms_utils-0.2.7.tar.gz 6.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for astep-forms-utils 0.2.7
File Interpreter ABI Platform
astep_forms_utils-0.2.7-py3-none-any.whl Python 3 none any Details

Total release size: 18.8 kB

Release files / astep_forms_utils-0.2.7.tar.gz

Download URL astep_forms_utils-0.2.7.tar.gz
Size 6.3 kB
Tags Source
SHA-256 checksum
How to use checksums
13fa966bb763de56b94cb8c4326d1cbea6fcb58f4906a8d11058761b20e8bc9d
BLAKE2b-256 checksum
How to use checksums
746f607c4cde59c37b9361489745be198a34ee1fbc523c367f2ea28daeb9c54b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

Release files / astep_forms_utils-0.2.7-py3-none-any.whl

Download URL astep_forms_utils-0.2.7-py3-none-any.whl
Size 12.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d83b52d09fbc2905dec195aefa50f20f2972ae01258a79985c4b507d4f5870cf
BLAKE2b-256 checksum
How to use checksums
aff5e7c7690aa4add7bf844e27adef9f28cf8deb09c060472d89db000364cc93
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

Release history Release notifications | RSS feed

This release

0.2.7 This release

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page