import json
import sys
import re
def camel_case(x):
return re.sub('(^_+|_+$)', '', re.sub(r'[^a-zA-Z0-9]+', '_', x.strip().lower()))
def generate_form(defn):
result = ''
for name, value in defn.items():
ftype = value[0]
if ftype != 'check':
result += f'<div className="flex flex-row items-center">{name}</div>\n<div className="col-span-3">\n'
else:
result += '<div className="col-span-1 md:col-span-4">\n'
if ftype == 'text':
result += '<input className="input input-sm input-primary input-bordered w-full" />\n'
elif ftype == 'number':
result += '<input type="number" className="input input-sm input-primary input-bordered w-full" />\n'
elif ftype == 'select':
result += '<select className="input input-sm input-primary input-bordered w-full">'
for opt in value[1]:
result += f'<option value="{camel_case(opt)}">{opt}</option>\n'
result += '</select>'
elif ftype == 'check':
result += f'<Toggle>{name}</Toggle>'
elif ftype == 'color':
result += '<input type="color" />'
result += '\n</div>\n'
return result
print(generate_form(json.loads(sys.stdin.read())))
To embed this program on your website, copy the following code and paste it into your website's HTML: