Skip to content

Instantly share code, notes, and snippets.

@vehrka
Last active February 10, 2016 12:39
Show Gist options
  • Save vehrka/d8a30efed3ccf0a8f38b to your computer and use it in GitHub Desktop.
Save vehrka/d8a30efed3ccf0a8f38b to your computer and use it in GitHub Desktop.
Quick jinja2 report
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import csv
from jinja2 import Environment, FileSystemLoader
from collections import namedtuple
env = Environment(loader=FileSystemLoader("."))
def render_template(template_file, **kwargs):
template = env.get_template(template_file)
return template.render(**kwargs)
if __name__ == '__main__':
User = namedtuple('User', ['id', 'first', 'last', 'user', 'pswd'])
usuarios = map(User._make, csv.reader(open("users.csv", "r")))
html = render_template("report.html", users=usuarios)
print(html)
"""
<html>
<body>
{% for user in users %}
<p>Hi {{ user.first }},</p>
<p>Your user is:</p>
<p>{{ user.user }}</p>
<p>and your password</p>
{{ user.pswd }}</p>
<p>Have a nice day</p>
<hr>
{% endfor %}
</body>
</html>
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment