-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyhtml.py
More file actions
67 lines (55 loc) · 1.65 KB
/
myhtml.py
File metadata and controls
67 lines (55 loc) · 1.65 KB
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
br = "<br />\n"
def indent(s):
lines = s.splitlines()
lines = [" " + l for l in lines]
return "\n".join(lines)
def form(txt, button, method="get"):
# txt = indent(txt)
template = """<form method="{method}">
{txt}
<br />
<input type="submit" value="{button}">
</form>
"""
return template.format(method=method, txt=txt, button=button)
def myinput(name, mytype="text", mymin=None, mymax=None, value=None,
checked=False):
d = {'min': mymin, 'max': mymax, 'value': value}
s = " ".join(i + '="' + d[i] + '"' for i in d if d[i])
ch = ' checked' if checked else ''
res = (name + ': <input name="' + name + '" type="' + mytype + '" '
+ s + ch + '>\n')
return res
def page(title, body, color=None):
# body = indent(body)
style = " style='background-color:" + color + "'" if color else ""
template = """<!DOCTYPE html>
<html lang="en">
<head>
<title>
{title}
</title>
</head>
<body{style}>
{body}
</body>
</html>
"""
return template.format(title=title, style=style, body=body)
def select(name, options, selected=None):
s = name + ': <select name="' + name + '">\n'
for option in options:
sel = ''
if option == selected:
sel = ' selected="selected"'
template = '<option value="{option}"{sel}>{option}</option>\n'
s += template.format(option=option, sel=sel)
s += "</select>\n"
return s
def textarea(name=None, rows=None, cols=None, txt=""):
d = {'name': name, 'rows': rows, 'cols': cols}
s = " ".join(i+'="'+d[i]+'"' for i in d if d[i])
template = """<textarea {s}>
{txt}</textarea>
"""
return template.format(s=s, txt=txt)