-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusing_jinja2.py
executable file
·56 lines (39 loc) · 1.22 KB
/
using_jinja2.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import logging
import sys
import pytest
import os
import jinja2
# find absolute path of the running file
# https://pythonadventures.wordpress.com/2014/02/25/jinja2-example-for-generating-a-local-file-using-a-template/
PATH = os.path.dirname( os.path.abspath( __file__ ) )
env = jinja2.Environment(
loader = jinja2.FileSystemLoader( os.path.join( PATH, 'templates' ) )
)
def render_template(template_filename, stash):
return env.get_template(template_filename).render(stash)
def main():
output_filename = 'output.html'
template_filename = 'test.html'
title = u'Hello Jijna2!'
sample = u'これは日本語です。'
stash = {
'title': title,
'sample': sample,
}
with open(output_filename, 'w') as f:
html = render_template(template_filename, stash)
f.write(html)
def test():
pass
if __name__=='__main__':
# logger setup
logfile = str(sys.argv[0])[:-3] + '.log'
logging.basicConfig(
filename = logfile,
format = '%(asctime)s - %(filename)s: %(lineno)s: %(funcName)s - %(levelname)s: %(message)s',
# level = logging.DEBUG,
level = logging.ERROR,
)
main()