Skip to content

Instantly share code, notes, and snippets.

@AstraLuma
Created September 16, 2015 00:33
Show Gist options
  • Select an option

  • Save AstraLuma/56e64983b4b85e35b8fd to your computer and use it in GitHub Desktop.

Select an option

Save AstraLuma/56e64983b4b85e35b8fd to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
#!C:\Python34\python.exe -u
"""
This is a CGI script to read the phone list (in YAML) and format it in HTML.
"""
import cgitb
cgitb.enable() # for troubleshooting
try:
import yaml # Docs: http://pyyaml.org/wiki/PyYAML
import jinja2 # Docs: http://jinja.pocoo.org/
except ImportError:
raise RuntimeError("PyYAML and jinja2 must be installed using pip or from PyPI.")
# Change this path to wherever it needs to be
people = yaml.safe_load(open('.../phone.yaml'))
# This is in http://jinja.pocoo.org/docs/dev/templates/
markup = jinja2.Template(r"""<!DOCTYPE html>
<html lang="en">
<head>
<title>Phone List</title>
<style>
ul.inline > li {
display: inline;
}
/* Add a dash before list items, except the first */
ul.inline > li:not(:first-child)::before {
content: "\2013"; /* En Dash */
margin-right: 0.5ch;
}
</style>
</head>
<body>
<header>
<h1>Phone List</h1>
</header>
{% for person in people %}
<section>
<h3>{{ person.first }} {{ person.last }}</h3>
<ul class="inline">
<li>{{ person.dept }}</li>
<li>{{ person.title }}</li>
</ul>
<ul class="inline">
<li>x{{ person.ext }}</li>
<li>Cell: {{ person.cell }}</li>
</ul>
</section>
{% endfor %}
<footer>
<p>For updates, contact IT.</p>
</footer>
</body>
</html>
""")
# Headers, declaring this HTML
print("Content-type: text/html")
print("")
# Spit out the markup
print(markup.render(
departments=sorted({p['dept'] for p in people}),
people=sorted(people, key=lambda p: (p['last'], p['first'])),
))
- first: John
last: Doe
ext: 1234
cell: 123-456-7890
dept: The Cubers in the Cubes
title: Cube Coordinator
- first: Jane
last: Doe
ext: 5678
cell: 098-765-4321
dept: The Cubers in the Cubes
title: Head Cuber
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment