Python Guestbook (CGI)
Revision as of 17:19, 3 February 2021 by Michael Murtaugh (talk | contribs) (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...")
#!/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>""")