-
Notifications
You must be signed in to change notification settings - Fork 7
/
line-visualize.py
85 lines (66 loc) · 2.45 KB
/
line-visualize.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import argparse
import jinja2
import os
import base64
import shutil
import line_utils
import matplotlib.pyplot as plt
def generator(input, output):
if os.path.exists(input) == False:
print("File {} not exist".format(input))
quit()
directory = 'tmp'
if not os.path.exists(directory):
os.makedirs(directory)
chats_dict = line_utils.read_line_chat(input)
# Chat per day
chat_per_day_path = directory + "/chat_per_day.png"
plt.figure(figsize=(10, 5))
line_utils.plot_chat_per_day(chats_dict, chat_per_day_path)
# User chat per day
user_chat_per_day_path = directory + "/user_chat_per_day.png"
plt.figure(figsize=(10, 5))
line_utils.plot_chat_users_per_day(chats_dict, user_chat_per_day_path)
# Punch card activities
punch_card_activities_path = directory + "/punch_card_activities.png"
plt.figure(figsize=(15, 10))
line_utils.plot_punch_card_activities(chats_dict, punch_card_activities_path)
# Response rate
response_rate_path = directory + "/response_rate.png"
plt.figure(figsize=(15, 10))
response_rate_users = line_utils.plot_response_rate(chats_dict, response_rate_path)
data = {
"chat_per_day": convert_img_to_base64(chat_per_day_path),
"user_chat_per_day": convert_img_to_base64(user_chat_per_day_path),
"punch_card_activities": convert_img_to_base64(punch_card_activities_path),
"response_rate": convert_img_to_base64(response_rate_path),
"response_rate_users": response_rate_users,
}
result = render('./template/visualize.html', data)
with open(output, "w") as html_file:
html_file.write(result)
# Remove genrate image directory
shutil.rmtree(directory)
def render(tpl_path, context):
path, filename = os.path.split(tpl_path)
return jinja2.Environment(
loader=jinja2.FileSystemLoader(path or './')
).get_template(filename).render(context)
def convert_img_to_base64(file):
with open(file, "rb") as image_file:
encoded = base64.b64encode(image_file.read())
return encoded.decode("utf-8")
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'-i',
'--input',
help='input chat file to visualize')
parser.add_argument(
'-o',
'--output',
help='HTML output file',
default='output.html')
args = parser.parse_args()
arguments = args.__dict__
generator(**arguments)