Skip to content

Instantly share code, notes, and snippets.

@wenchy
Last active April 23, 2018 05:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wenchy/652046c75db5d821693b61de4c74b593 to your computer and use it in GitHub Desktop.
Save wenchy/652046c75db5d821693b61de4c74b593 to your computer and use it in GitHub Desktop.
HTML form generator
#!/usr/bin/python
# coding: utf-8
import json
import inspect
def html_form(func):
argspec = inspect.getargspec(func)
default_start = len(argspec[0]) - len(argspec[3])
index = 0
args = {}
for arg in argspec[0]:
if index >= default_start:
args[arg] = argspec[3][index - default_start]
else:
args[arg] = ""
index += 1
form = {}
try:
form = json.loads(func.__doc__)
for arg,default in args.items():
if not arg in form:
form[arg] = {"desc": arg, "input": "text", "default": ""}
form[arg]["default"] = default
except ValueError:
print 'Decoding JSON of Docstring failed, then just treat it as title'
form["title"] = func.__doc__
for arg,default in args.items():
form[arg] = {"desc": arg, "input": "text", "default": ""}
form[arg]["default"] = default
form["name"] = func.__name__
print json.dumps(form)
func.__form__ = form
return func
@html_form
def func(arg1, arg2 = 4, arg3 = "hhaa"):
'''
{
"title": "title of form",
"tip": "tip of form",
"arg1": {
"desc": "desc_arg1",
"tip": "tip of arg",
"input": "select",
"options": {
"1": "activity",
"2": "zone",
"3": "rank"
},
"datalist": {
"1": "activity",
"2": "zone",
"3": "rank"
}
}
}
'''
pass
@html_form
def docstr(arg1, arg2 = 4, arg3 = "hhaa"):
'''test not json docstr'''
pass
#print getattr(func, "__form__")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment