Python Guestbook (CGI): Difference between revisions
(Created page with "<source lang="python"> #!/usr/bin/python3 import cgitb; cgitb.enable() import json print ("Content-type:text/html;charset=utf-8") print() print ("""<!DOCTYPE html> <html> <h...") |
No edit summary |
||
Line 1: | Line 1: | ||
== comment.html == | |||
<source lang="html"> | |||
<!DOCTYPE html> | |||
<html> | |||
<head> | |||
<meta charset="utf-8"> | |||
<style> | |||
textarea { | |||
width: 100%; | |||
height: 5em; | |||
} | |||
</style> | |||
</head> | |||
<body> | |||
<form method="post" action="/sandbox/cgi-bin/mmurtaugh/save_comment.cgi"> | |||
<textarea autofocus placeholder="COMMENT" name="text"></textarea> | |||
<input type="color" name="color"> | |||
<input type="range" min="0" max="100" name="x"> | |||
<input type="range" min="0" max="100" name="y"> | |||
<input type="submit" name="_submit" value="send"> | |||
<input type="submit" name="_submit" value="cancel"> | |||
</form> | |||
</form> | |||
</body> | |||
</html> | |||
</source> | |||
== save_comment.cgi == | |||
<source lang="python"> | |||
</source> | |||
== comments.cgi == | |||
<source lang="python"> | <source lang="python"> | ||
#!/usr/bin/python3 | #!/usr/bin/python3 |
Latest revision as of 17:29, 3 February 2021
comment.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
textarea {
width: 100%;
height: 5em;
}
</style>
</head>
<body>
<form method="post" action="/sandbox/cgi-bin/mmurtaugh/save_comment.cgi">
<textarea autofocus placeholder="COMMENT" name="text"></textarea>
<input type="color" name="color">
<input type="range" min="0" max="100" name="x">
<input type="range" min="0" max="100" name="y">
<input type="submit" name="_submit" value="send">
<input type="submit" name="_submit" value="cancel">
</form>
</form>
</body>
</html>
save_comment.cgi
comments.cgi
#!/usr/bin/python3
import cgitb; cgitb.enable()
import json
print ("Content-type:text/html;charset=utf-8")
print()
print ("""<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Thanks for your comment!</title>
<style>
.comment {
margin-bottom: 1em;
border-top: 1px solid black;
}
</style>
</head>
<body>
<h1>Comments</h1>
<section id="comments">
""")
# Pass 1: load all the d's (posts)
items = []
with open("/home/mmurtaugh/public_html/messages.json") as fin:
for line in fin:
d=json.loads(line)
items.append(d)
# OPTIONAL... reorder
items.reverse()
# Pass 2: display them
for i, d in enumerate(items):
# if (i+1 == len(items)): # for chronological order
if (i == 0):
print (f"""<div id="latest" class="comment" style="color: {d['color']}">{d['text']}</div>""")
else:
print (f"""<div id="msg{i}" class="comment" style="color: {d['color']}">{d['text']}</div>""")
print ("""
<p><a href="/sandbox/~mmurtaugh/comment.html">Add a comment</a></p>
</section>
</body>
</html>""")