Form Generator

How to generate Forms that trigger services.

From time to time, you may encounter scenarios in which you need to create a frontend for initiating a service, often to be used by non-technical individuals. Both FX and K8X offer the capability to define forms using a straightforward JSON structure.

These forms are automatically generated by the Ferris Management UI, based on the ‘parameters.json’ file.

When a service directory includes a ‘parameters.json’ file, the ‘Run’ button in the Management UI will automatically change its icon to a ‘Form’ icon.

To add the ‘parameters.json’ file to an existing service directory, ensure that the ‘allow_manual_triggering’ in the manifest.json file is set to ’true.’

Template parameters.json file

The ‘parameters.json’ file contains a JSON definition of fields. These fields are presented to the user when manually triggering a package execution to collect the parameter values needed to run the package. This approach allows the same package to be easily adapted and reused for different scenarios or environments simply by providing different parameter values to the same package.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
{
  "fields": [
    {
      "type": "text",
      "label": "Some Text",
      "name": "some_text",
      "required": true,
      "description": "This field is required"
    },
    {
      "type": "textarea",
      "label": "Some Textarea",
      "name": "some_textarea"
    },
    {
      "type": "file",
      "label": "Some File",
      "name": "some_file",
      "data": {
        "bucket": "testbucket",
        "async": true
      }
    },
    {
      "type": "int",
      "label": "Some Number",
      "name": "some_number",
      "default": 1,
      "min": 0,
      "max": 10
    },
    {
      "type": "float",
      "label": "Some Float",
      "name": "some_float",
      "placeholder": "0.01",
      "step": 0.01,
      "min": 0,
      "max": 10
    },
    {
      "type": "select",
      "label": "Some Select",
      "name": "some_select",
      "default": "value 2",
      "choices": [
        {
          "title": "Choice 1",
          "value": "value 1"
        },
        {
          "title": "Choice 2",
          "value": "value 2"
        },
        {
          "title": "Choice 3",
          "value": "value 3"
        }
      ]
    },
    {
      "type": "multiselect",
      "label": "Some MultiSelect",
      "name": "some_multiselect",
      "default": ["value 2", "value 3"],
      "choices": [
        {
          "title": "Choice 1",
          "value": "value 1"
        },
        {
          "title": "Choice 2",
          "value": "value 2"
        },
        {
          "title": "Choice 3",
          "value": "value 3"
        }
      ]
    },
    {
      "type": "radio",
      "label": "Some Radio",
      "name": "some_radio",
      "choices": [
        {
          "title": "Choice 1",
          "value": "value 1"
        },
        {
          "title": "Choice 2",
          "value": "value 2"
        },
        {
          "title": "Choice 3",
          "value": "value 3"
        }
      ]
    }
  ]
}

The provided template will display a form as follows:

When users enter values in the form and click the ‘Run’ button, the form parameters and values will be sent to the service upon triggering. These parameters will be available to the service as if it were triggered by an event with the same payload as the form values.

Below is a sample script that extracts the parameters (notice that it’s not fundamentally different from a script triggered by an event). The only exception is the text areas, which are treated as String data types and may require conversion using the appropriate JSON library.

1
2
3
4
5
6
7
8
9

from fx_ef import context
import json

event_type = context.params.get("sample_event_type")
event_source = context.package.name
data = json.loads(context.params.get("sample_payload"))

context.events.send(event_type, event_source, data=data)

Last modified November 24, 2023: update (f4bc5ea)