-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_report.py
55 lines (41 loc) · 1.64 KB
/
generate_report.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
import os
from jinja2 import Environment, FileSystemLoader
import shutil
from datetime import datetime
import json
from utils import readcsv, readjson
def generate_report(date):
script_path = os.path.dirname(os.path.abspath(__file__))
csv_file = f'watch-{date}.csv'
if not os.path.exists( os.path.join(script_path, 'csv_data', csv_file ) ):
return False
shutil.copy2(os.path.join( script_path, 'csv_data', csv_file), os.path.join( script_path , 'web') )
report_date = datetime.strptime(date, '%m-%d-%Y').strftime("%A %-d %B %Y")
generation_date = datetime.now().strftime('%d %B %Y %H:%M')
csv_data = readcsv(os.path.join(script_path, 'csv_data', csv_file ) )
colors_data = readjson(os.path.join(script_path, 'web', 'colors.json' ))
json_data = json.dumps(
{
'report_date': report_date,
'generation_date': generation_date,
'csv_file': csv_file,
'csv_data': csv_data,
'colors_data': colors_data
}
)
rendered = _render_template(json_data)
report_filename = os.path.join(script_path, 'web',f'report-{date}.html')
with open(os.path.join(report_filename), 'w') as f:
f.write(rendered)
return report_filename
def _render_template(json_data):
script_path = os.path.dirname(os.path.abspath(__file__))
file_loader = FileSystemLoader(os.path.join(script_path, 'web'))
env = Environment(
loader=file_loader,
trim_blocks=True,
lstrip_blocks=True,
keep_trailing_newline=True,
)
template = env.get_template('index.html')
return template.render(json_data=json_data)