-
Notifications
You must be signed in to change notification settings - Fork 5
/
j2_02.py
26 lines (19 loc) · 854 Bytes
/
j2_02.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# this file: j2_02.py
'''Render a Jinja2 template based on data
Usage:
python3 j2_02.py TEMPLATEFILE DATAFILE
Where:
TEMPLATEFILE: path to the text file containing a valid Jinja2 teamplte
DATAFILE : path to a JSON file containing the data
'''
import json
import sys
import jinja2 as j2 # load Jinja2 module, refer to its content as "j2.*"
TEMPLATE = open(sys.argv[1]).read() # load template from file given as
# 1st CLI argument
TEMPLATE = TEMPLATE.strip() # remove newlines from begin and end
DATA = json.load(open(sys.argv[2])) # load data from file given as
# 2nd CLI argument
j2_tmpl = j2.Template(TEMPLATE) # new Jinja2 template instance
out = j2_tmpl.render(**DATA) # pass unpacked data (assuming dict)
print(out)