Skip to main content

A tool to generate form JSON schemas from prompts

Project description

FormGen

FormGen is a Python tool that helps you quickly generate React form components from JSON schemas. It’s useful for frontend-backend integration workflows, rapid prototyping, or generating boilerplate form code in large projects.

Installation

Install this package using:

pip install pyformgen

How to Use

The core functionality of FormGen is provided by the generate_react_form function, located within the react_generator module of the formgen package. This function takes a JSON schema and an output file path to generate a React form component.

1. Prepare a JSON schema

Create a JSON file that defines your form fields and their properties.

Example: sample_schema.json

{
  "first_name": {
    "label": "First Name",
    "type": "text",
    "placeholder": "Enter your first name",
    "required": true
  },
  "age": {
    "label": "Age",
    "type": "number",
    "placeholder": "Enter your age",
    "required": true,
    "min": 0
  },
  "gender": {
    "label": "Gender",
    "type": "select",
    "required": true,
    "options": [
      { "value": "male", "label": "Male" },
      { "value": "female", "label": "Female" },
      { "value": "other", "label": "Other" }
    ]
  },
  "subscribe": {
    "label": "Subscribe to Newsletter",
    "type": "checkbox",
    "required": false
  }
}

2. Generate your React form component

Use the generate_react_form function from pyformgen in your Python script.

Example Python Script:

import json
from formgen import generate_react_form

# Load your JSON schema
with open("sample_schema.json") as f:
    schema = json.load(f)

# Define the output path for your React component
output_path = "GeneratedForm.jsx"

# Generate the React form component
generate_react_form(schema, output_path)

print(f"React form component generated at: {output_path}")

3. Run the script

python your_script_name.py

After running, you'll find a GeneratedForm.jsx file (or whatever you named it) containing a full React form component ready for use in your React application.

Output

The generated JSX form will look like this (simplified):

export default function GeneratedForm() {
  const [first_name, setFirst_name] = useState("");
  const [age, setAge] = useState("");
  const [gender, setGender] = useState("");
  const [subscribe, setSubscribe] = useState("");

  const handleSubmit = (e) => {
    e.preventDefault();
    const formData = {
      first_name,
      age,
      gender,
      subscribe,
    };
    console.log("Submitted data:", formData);
  };

  return (
    <form onSubmit={handleSubmit}>

      <div>
        <label>First Name</label>
        <input
          name="first_name"
          value={ first_name }
          onChange={(e) => setFirst_name(e.target.value)}
          type="text"
           placeholder="Enter your first name" required
        />
      </div>
      <div>
        <label>Age</label>
        <input
          name="age"
          value={ age }
          onChange={(e) => setAge(e.target.value)}
          type="number"
           placeholder="Enter your age" required min="0"
        />
      </div>
      <div>
        <label>Gender</label>
        <select
          name="gender"
          value={ gender }
          onChange={(e) => setGender(e.target.value)}
           required
        >
            <option value="male">Male</option>
            <option value="female">Female</option>
            <option value="other">Other</option>
        </select>
      </div>
      <div>
        <label>Subscribe to Newsletter</label>
        <input
          name="subscribe"
          value={ subscribe }
          onChange={(e) => setSubscribe(e.target.value)}
          type="checkbox"
          
        />
      </div>
      <button type="submit">Submit</button>
    </form>
  );
}

Field Attributes Supported

Each field in your JSON schema can have the following attributes:

  • label: The label text displayed beside the input field.

  • type: The HTML input type (e.g., text, email, password, number, checkbox, radio, textarea, select).

  • min: Sets the minimum numeric value or date.

  • min: Sets the maximum numeric value or date.

  • pattern: Defines a regex pattern the input must match.

  • readOnly: Prevents the user from editing the field.

  • disabled: Disables the field so it can't be interacted with.

  • autoComplete: Suggests previously entered values for the field.

  • size: Defines the visible width of the input in characters.

  • multiple: Allows selection of multiple values (usually for file or select inputs).

  • autoFocus: Automatically focuses the field on page load.

  • defaultValue: Sets a default value for the input field.

  • required: A boolean value (true or false) to mark the field as required.

  • placeholder: Placeholder text displayed inside the input field before user input.

  • options: (For select or radio types) An array of objects, each with value and label properties.

"role": {
  "label": "Role",
  "type": "select",
  "options": [
    {"value": "admin", "label": "Administrator"},
    {"value": "user", "label": "Regular User"}
  ]
}

License

MIT License. See LICENSE file for more details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyformgen-0.2.2.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyformgen-0.2.2-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file pyformgen-0.2.2.tar.gz.

File metadata

  • Download URL: pyformgen-0.2.2.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.13

File hashes

Hashes for pyformgen-0.2.2.tar.gz
Algorithm Hash digest
SHA256 6e71e45e90568d27f64fe196958a5668d3b1db81b6eb03e9b9adb6d169569c34
MD5 89f35f7f4b3a5fd033b9bf969091db9b
BLAKE2b-256 e4454f7485c6302947d1af7ee2ca80afdd9f4f94a5d808c5d8d804c4aeb161b0

See more details on using hashes here.

File details

Details for the file pyformgen-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: pyformgen-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.13

File hashes

Hashes for pyformgen-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 87f3196b5690083c31030853e8a067b46e816837d061236d97afa5c6db053492
MD5 f762afb6aecd373326ed8fde2dd032af
BLAKE2b-256 2f8eb097e45fbef1d85d9732f8ad8135128db7e4dade5ff162a4001a0cfab5c1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page