-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathst.py
executable file
·56 lines (48 loc) · 1.56 KB
/
st.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 python
# -*- coding: utf-8 -*-
from sqlite3 import dbapi2 as s
import jinja2
def getData():
print("1. getting data")
with s.connect("data.db") as conn:
cur = conn.cursor()
cur.execute("SELECT * FROM languages")
result = cur.fetchall()
cur.close()
return result
def saveJinjaTemplate(data):
print ("2. saving templates")
template_loader = jinja2.FileSystemLoader(".")
template_env = jinja2.Environment(loader=template_loader)
TEMPLATE_FILE = "templates/simple.html"
template = template_env.get_template(TEMPLATE_FILE)
for i in data:
vars = {
"id": i[0],
"name": i[1],
"description": i[2]
}
out = template.render(vars)
filename = "output/id_" + ("%s" % i[0]) + ".html"
file = open(filename, 'w')
file.write(out.encode('utf-8'))
file.close()
print("2.%s saved file for [%s]" % (i[0], i[1]))
def createIndex(data):
print("3. creating index")
template_loader = jinja2.FileSystemLoader(".")
template_env = jinja2.Environment(loader=template_loader)
urls = []
for i in data:
urls.append("id_" + ("%s" % i[0]) + ".html")
TEMPLATE_FILE = "templates/simple_index.html"
template = template_env.get_template(TEMPLATE_FILE)
out = template.render({"urls": urls})
filename = "output/index.html"
file = open(filename, 'w')
file.write(out.encode('utf-8'))
file.close()
if __name__ == "__main__":
data = getData()
saveJinjaTemplate(data)
createIndex(data)