Skip to content

Commit c6d890a

Browse files
authored
Merge pull request #2 from gjbex/development
Add jinja2 example
2 parents 9144fb8 + 165a0c0 commit c6d890a

File tree

6 files changed

+80
-0
lines changed

6 files changed

+80
-0
lines changed

source-code/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ to create it. There is some material not covered in the presentation as well.
2929
1. `data-formats`: illustrates how to deal with data formats such as CSV
3030
files, binary data and XML.
3131
1. `hydra`: Facebook Hydra application framework illustration.
32+
1. `jinja`: illustration of how to use the jinja2 template library.
3233
1. `logging`: illustration of Python's logging facilities.
3334
1. `file-system`: illustrations of interacting with the operating system
3435
and the file system.

source-code/jinja/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Jinja
2+
3+
Jinja2 is a templating library for Python that can be used for reporting,
4+
or for any coding task that boils down to filling out templates.
5+
6+
## What is it?
7+
8+
1. `reporting.py`: Python script that creates either a MarkDown or HTML format
9+
report on randomly generated people. The data reported is a person's ID,
10+
year of birth and number of friends.
11+
1. `population/templates/report.html`: template for the HTML report.
12+
1. `population/templates/report.md`: template for the MarkDown report.

source-code/jinja/population/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<html>
2+
<head>
3+
<title>Report</title>
4+
</head>
5+
<body>
6+
<h1>Report on people</h1>
7+
8+
<table>
9+
<tr>
10+
<th>Person ID</th>
11+
<th>year of birth</th>
12+
<th>number of friends</th>
13+
</tr>
14+
{% for person in people %}
15+
<tr>
16+
<td> {{ person['id'] }} </td>
17+
<td> {{ person['birthyear'] }} </td>
18+
<td> {{ person['nr_friends'] }} </td>
19+
<tr>
20+
{% endfor %}
21+
</table>
22+
</body>
23+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Report
2+
3+
| person ID | year of birth | number of friends |
4+
|-----------|---------------|-------------------|
5+
{% for person in people %}
6+
| {{ '%-9s'|format(person['id']) }} | {{ '%13d'|format(person['birthyear']) }} | {{ '%17d'|format(person['nr_friends']) }} |
7+
{% endfor %}

source-code/jinja/reporting.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env python
2+
3+
from argparse import ArgumentParser
4+
from jinja2 import Environment, PackageLoader
5+
import population
6+
import random
7+
import string
8+
import sys
9+
10+
11+
def generate_person():
12+
person = {
13+
'id': ''.join(random.choices(string.ascii_letters, k=5)),
14+
'birthyear': random.randint(1950, 2015),
15+
'nr_friends': random.randint(0, 50),
16+
}
17+
return person
18+
19+
def main():
20+
arg_parser = ArgumentParser(description='generate random people')
21+
arg_parser.add_argument('n', type=int, default=5, nargs='?',
22+
help='number of people to generate')
23+
arg_parser.add_argument('--format', choices=['html', 'md'],
24+
default='md', help='output format, html or md')
25+
arg_parser.add_argument('--seed', type=int, default=1234,
26+
help='seed for random number generator')
27+
options = arg_parser.parse_args()
28+
random.seed(options.seed)
29+
people = [generate_person() for _ in range(options.n)]
30+
environment = Environment(loader=PackageLoader('population', 'templates'),
31+
trim_blocks=True, lstrip_blocks=True)
32+
template = environment.get_template('report.' + options.format)
33+
print(template.render(people=people))
34+
35+
if __name__ == '__main__':
36+
status = main()
37+
sys.exit(status)

0 commit comments

Comments
 (0)