Skip to content

Add jinja2 example #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions source-code/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ to create it. There is some material not covered in the presentation as well.
1. `data-formats`: illustrates how to deal with data formats such as CSV
files, binary data and XML.
1. `hydra`: Facebook Hydra application framework illustration.
1. `jinja`: illustration of how to use the jinja2 template library.
1. `logging`: illustration of Python's logging facilities.
1. `file-system`: illustrations of interacting with the operating system
and the file system.
Expand Down
12 changes: 12 additions & 0 deletions source-code/jinja/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Jinja

Jinja2 is a templating library for Python that can be used for reporting,
or for any coding task that boils down to filling out templates.

## What is it?

1. `reporting.py`: Python script that creates either a MarkDown or HTML format
report on randomly generated people. The data reported is a person's ID,
year of birth and number of friends.
1. `population/templates/report.html`: template for the HTML report.
1. `population/templates/report.md`: template for the MarkDown report.
Empty file.
23 changes: 23 additions & 0 deletions source-code/jinja/population/templates/report.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<html>
<head>
<title>Report</title>
</head>
<body>
<h1>Report on people</h1>

<table>
<tr>
<th>Person ID</th>
<th>year of birth</th>
<th>number of friends</th>
</tr>
{% for person in people %}
<tr>
<td> {{ person['id'] }} </td>
<td> {{ person['birthyear'] }} </td>
<td> {{ person['nr_friends'] }} </td>
<tr>
{% endfor %}
</table>
</body>
</html>
7 changes: 7 additions & 0 deletions source-code/jinja/population/templates/report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Report

| person ID | year of birth | number of friends |
|-----------|---------------|-------------------|
{% for person in people %}
| {{ '%-9s'|format(person['id']) }} | {{ '%13d'|format(person['birthyear']) }} | {{ '%17d'|format(person['nr_friends']) }} |
{% endfor %}
37 changes: 37 additions & 0 deletions source-code/jinja/reporting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env python

from argparse import ArgumentParser
from jinja2 import Environment, PackageLoader
import population
import random
import string
import sys


def generate_person():
person = {
'id': ''.join(random.choices(string.ascii_letters, k=5)),
'birthyear': random.randint(1950, 2015),
'nr_friends': random.randint(0, 50),
}
return person

def main():
arg_parser = ArgumentParser(description='generate random people')
arg_parser.add_argument('n', type=int, default=5, nargs='?',
help='number of people to generate')
arg_parser.add_argument('--format', choices=['html', 'md'],
default='md', help='output format, html or md')
arg_parser.add_argument('--seed', type=int, default=1234,
help='seed for random number generator')
options = arg_parser.parse_args()
random.seed(options.seed)
people = [generate_person() for _ in range(options.n)]
environment = Environment(loader=PackageLoader('population', 'templates'),
trim_blocks=True, lstrip_blocks=True)
template = environment.get_template('report.' + options.format)
print(template.render(people=people))

if __name__ == '__main__':
status = main()
sys.exit(status)