This repository has been archived by the owner on Dec 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreateHTML.py
70 lines (56 loc) · 1.88 KB
/
createHTML.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from jinja2 import Environment, FileSystemLoader, select_autoescape
from datetime import datetime
import psycopg2
import math
import os
#open initial connection
conn = psycopg2.connect("")
#open initial cursor
cur = conn.cursor()
def loadTemplate():
templates_dir = os.path.join(os.getcwd(), 'templates')
env = Environment(loader = FileSystemLoader(templates_dir),
autoescape=select_autoescape(['html', 'xml']))
template = env.get_template('base.html')
return template
def loadData():
# Let's grab some data from postgres
try:
q_select = '''
SELECT
article_title,
article_url,
to_char(article_date, 'DD Mon YYYY'),
article_favicon,
article_host
FROM posts ORDER BY article_date DESC;
'''
cur.execute(q_select)
origin_data = [cur.fetchall()]
except:
print("EXCEPTION THROWN")
print("ORIGIN DATA", origin_data)
for row in origin_data:
processed_data = [x for x in row]
return processed_data
def makeHTML(template):
origin_data = loadData()
entries_per_page = 100
filename = os.path.join(os.getcwd(), 'page/end.html')
#pagination
entries = [origin_data[page_offset:page_offset+entries_per_page]
for page_offset in range(0, len(origin_data), entries_per_page)]
pnum = []
for page_number, page_entries in enumerate(entries):
pnum.append(str(page_number))
for page_number, page_entries in enumerate(entries):
with open(filename, 'w+') as fw:
fw.write(template.render(data=page_entries, pg=pnum))
fw.close()
fw = open('page/%s.html' % page_number, 'w')
fw.write(template.render(data=page_entries, pg=pnum))
print(pnum)
def main():
makeHTML(loadTemplate())
if __name__ == '__main__':
main()