forked from irfanchahyadi/Whatsapp-Chat-Analyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
287 lines (276 loc) · 17.8 KB
/
app.py
File metadata and controls
287 lines (276 loc) · 17.8 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import base64, json
from datetime import datetime, date
from collections import Counter
import pandas as pd
import dash
from dash import dcc
from dash import html
from dash.dependencies import Input, Output, State
import dash_bootstrap_components as dbc
from src import charts, chat_parser, layouts, settings
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP, settings.FONT_AWESOME])
server = app.server
app.config.suppress_callback_exceptions = True
app.title = settings.APP_NAME
app.index_string = """
<!DOCTYPE html>
<html>
<head>
{%metas%}
<title>{%title%}</title>
{%favicon%}
{%css%}
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=""" + settings.GA_TRACKING_ID + """"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '""" + settings.GA_TRACKING_ID + """');
</script>
</head>
<body>
{%app_entry%}
<footer>
{%config%}
{%scripts%}
{%renderer%}
</footer>
</body>
</html>"""
app.layout = layouts.base
@app.callback([Output('page-content', 'children'), Output('container-data-store', 'children')], [Input('url', 'pathname')])
def display_page(pathname):
"""Handle what layout should be display."""
container_data_store = dash.no_update
if pathname == '/':
page_content = layouts.home
elif pathname.startswith('/groupchat/') or pathname.startswith('/personalchat/'):
chat_type, url_key = pathname[1:].split('/')
page_content = layouts.groupchat if chat_type == 'groupchat' else layouts.personalchat
if not pathname.endswith(settings.FROM_LANDING_PAGE):
url, datasets = chat_parser.load_parsed_data(url_key, 'url')
container_data_store = dcc.Store(id='data-store', data=datasets)
if url == 'not_found':
page_content = layouts.not_found
elif pathname.endswith('not_found'):
page_content = layouts.not_found
else:
page_content = layouts.page_404
return page_content, container_data_store
@app.callback(
[Output('url', 'pathname'), Output('data-store', 'data'), Output('alert-container', 'children')],
[Input('upload-data', 'contents'), Input('url-submit', 'n_clicks')],
[State('save-switch', 'on'), State('url-input', 'value')])
def upload_data(contents, n_click, save, url_input):
"""Change url and load data into datastore when upload chat or go to saved chat url key."""
ctx = dash.callback_context
url, datasets, alert = dash.no_update, dash.no_update, dash.no_update
if ctx.triggered[0]['prop_id'] == 'upload-data.contents':
content_type, content = contents.split(',')
if content_type == 'data:text/plain;base64':
content_decoded = base64.b64decode(content)
url, datasets = chat_parser.load_parsed_data(content_decoded, 'upload', save)
if url == 'not_supported':
supported_language = ', '.join([lang['language'] for lang in settings.LANGUAGE.values()])
alert = dbc.Alert([
'Language not supported. Currently supported languages: {}.'.format(supported_language),
html.Br(),
'Wanna contribute? add your language at ', html.A('this file', href=settings.SETTINGS_URL, target='_blank'), ' then send me PR'
], dismissable=True, color='danger')
url = dash.no_update
else:
url = url + settings.FROM_LANDING_PAGE
else:
alert = dbc.Alert('Wrong file type, please upload txt file from exported Whatsapp chat.', dismissable=True, color='danger')
elif ctx.triggered[0]['prop_id'] == 'url-submit.n_clicks':
url, datasets = chat_parser.load_parsed_data(url_input, 'url')
if url != 'not_found':
url = url + settings.FROM_LANDING_PAGE
return url, datasets, alert
@app.callback(
[Output('dropdown-users', 'options'), Output('navbar-brand', 'children'), Output('created-by', 'children'), Output('count-user', 'children')],
[Input('dropdown-users', 'value')], [State('data-store', 'data')])
def fill_dropdown_users_groupchat(dropdown_users, datasets):
"""Initialize dropdown-users options and other groupchat scope data."""
datasets = json.loads(datasets)
df = pd.read_json(datasets['data'], orient='split')
output = [
[{'label': i, 'value': i} for i in datasets['users']],
datasets['chat_name'],
'{} at {}'.format(datasets['chat_created_by'], datasets['chat_created_at']),
'{:,} active, {:,} left group'.format(len(datasets['users']), df[(df.category == 'Event') & (df.event_type == 'left')].shape[0])
]
return output
@app.callback(
[Output('dropdown-users2', 'options'), Output('navbar-brand2', 'children'), Output('created-by2', 'children'), Output('count-user2', 'children')],
[Input('dropdown-users2', 'value')], [State('data-store', 'data')])
def fill_dropdown_users_personalchat(dropdown_users, datasets):
"""Initialize dropdown-users options and other groupchat scope data."""
datasets = json.loads(datasets)
df = pd.read_json(datasets['data'], orient='split')
output = [
[{'label': i, 'value': i} for i in datasets['users']],
datasets['chat_name'],
'{} at {}'.format(datasets['chat_created_by'], datasets['chat_created_at']),
'{:,} active, {:,} left group'.format(len(datasets['users']), df[(df.category == 'Event') & (df.event_type == 'left')].shape[0])
]
return output
@app.callback([Output('date-picker', 'min_date_allowed'), Output('date-picker', 'max_date_allowed')], [Input('date-picker', 'id')], [State('data-store', 'data')])
def update_date_picker(id_date_picker, datasets):
"""Initialize date-picker min and max allowed date."""
datasets = json.loads(datasets)
return datasets['chat_min_date'], datasets['chat_max_date']
@app.callback(
[Output('help-' + key, 'style') for key in settings.TOOLTIPS] +
[Output('tt-' + key, 'style') for key in settings.TOOLTIPS] +
[Output('tt-' + key, 'hide_arrow') for key in settings.TOOLTIPS],
[Input('help-switch', 'on')])
def update_help_switch(show):
"""show/hide help tooltips."""
visibility = 'visible' if show else 'hidden'
output = [{'visibility': visibility}] * len(settings.TOOLTIPS) * 2 + [not show] * len(settings.TOOLTIPS)
return output
# TODO: split this big callback to some smaller callback, for better performance when deploy on server with >1 worker
@app.callback(
[Output('counter', 'children'), Output('count-message', 'children'), Output('count-word', 'children'), Output('count-emoji', 'children'), Output('count-mention', 'children'),
Output('count-media', 'children'), Output('count-location', 'children'), Output('count-link', 'children'), Output('count-contact', 'children'),
Output('chart-1', 'figure'), Output('chart-2', 'figure'), Output('chart-3', 'figure'),
Output('avg-user', 'children'), Output('avg-message', 'children'), Output('avg-day', 'children'), Output('avg-month', 'children'),
Output('most-busy', 'children'), Output('most-active', 'children'), Output('most-silent', 'children'), Output('most-typer', 'children'),
Output('most-emoji', 'children'), Output('most-media', 'children'), Output('most-location', 'children'), Output('most-link', 'children'),
Output('most-contact', 'children'), Output('most-mention', 'children'), Output('most-add', 'children'), Output('most-deleted', 'children'),
Output('most-domain', 'children'),
Output('chart-4', 'figure'), Output('chart-5', 'figure'), Output('chart-6', 'figure'), Output('chart-7', 'src'), Output('chart-8', 'elements')],
[Input('dropdown-users', 'value'), Input('date-picker', 'start_date'), Input('date-picker', 'end_date'), Input('time-interval1', 'value'), Input('time-interval2', 'value')],
[State('data-store', 'data')])
def update_groupchat(dropdown_users, start_date_str, end_date_str, interval1, interval2, datasets):
"""Update displayed data and chart at first and when filter apply."""
datasets = json.loads(datasets)
df = pd.read_json(datasets['data'], orient='split')
lang = datasets['lang']
start_date = date.min if start_date_str is None else datetime.strptime(start_date_str, '%Y-%m-%d').date()
end_date = date.max if end_date_str is None else datetime.strptime(end_date_str, '%Y-%m-%d').date()
filtered_df = df[
((df.contact.isin(dropdown_users)) | (len(dropdown_users) == 0)) &
(df.datetime.dt.date >= start_date) & (df.datetime.dt.date <= end_date)
]
output = [dash.no_update] * 34
ctx = dash.callback_context
if ctx.triggered[0]['prop_id'] == 'time-interval1.value':
output[9] = charts.chart1(filtered_df, interval1)
elif ctx.triggered[0]['prop_id'] == 'time-interval2.value':
output[29] = charts.chart4(filtered_df, interval2, 5)
else:
by_category = filtered_df[['contact', 'category']].pivot_table(index='contact', columns='category', aggfunc=len, fill_value=0).reindex(columns=settings.CATEGORIES, fill_value=0)
by_column = filtered_df.groupby('contact').sum(numeric_only=True)
by_domain = Counter(df[df.count_link > 0].list_link.sum())
output = [
ctx.triggered[0]['prop_id'],
'{:,} sent, {:,} deleted'.format(by_category.sum().sum() - by_category['Event'].sum(), by_category['Deleted'].sum()),
'{:,} sent'.format(by_column['count_words'].sum()),
'{:,} sent'.format(by_column['count_emoji'].sum()),
'{:,} sent'.format(by_column['count_mention'].sum()),
'{:,} shared'.format(by_category['Media'].sum()),
'{:,} shared'.format(by_category['Location'].sum()),
'{:,} shared'.format(by_column['count_link'].sum()),
'{:,} shared'.format(by_category['Contact'].sum()),
charts.chart1(filtered_df, interval1),
charts.chart2(filtered_df),
charts.chart3(filtered_df),
'{:,.2f} messages'.format((by_category.sum().sum() - by_category['Event'].sum())/filtered_df.contact.nunique(dropna=True)),
'{:,.2f} words, {:.2f} emoji'.format(by_column['count_words'].sum()/by_category['Text'].sum(), by_column['count_emoji'].sum()/by_category['Text'].sum()),
'{:,.2f} text, {:.2f} media'.format(by_category['Text'].sum()/filtered_df.date.nunique(), by_category['Media'].sum()/filtered_df.date.nunique()),
'{:,.2f} text, {:.2f} media'.format(by_category['Text'].sum()/filtered_df.month.nunique(), by_category['Media'].sum()/filtered_df.month.nunique()),
layouts.award_list(filtered_df.groupby('date').size().sort_values(ascending=False)),
layouts.award_list((by_category.sum(axis=1) - by_category['Event']).sort_values(ascending=False)),
layouts.award_list((by_category.sum(axis=1) - by_category['Event'])[lambda x: x > 0].sort_values(ascending=True)),
layouts.award_list(by_column['count_character'].sort_values(ascending=False)),
layouts.award_list(by_column['count_emoji'].sort_values(ascending=False)),
layouts.award_list(by_category['Media'].sort_values(ascending=False)),
layouts.award_list(by_category['Location'].sort_values(ascending=False)),
layouts.award_list(by_column['count_link'].sort_values(ascending=False)),
layouts.award_list(by_category['Contact'].sort_values(ascending=False)),
layouts.award_list(by_column['count_mention'].sort_values(ascending=False)),
layouts.award_list(filtered_df[(filtered_df.category == 'Event') & (filtered_df.event_type == 'added')].groupby('contact').size().sort_values(ascending=False)),
layouts.award_list(by_category['Deleted'].sort_values(ascending=False)),
layouts.award_list(by_domain),
charts.chart4(filtered_df, interval2, 5),
charts.chart5(filtered_df, 5),
charts.chart6(filtered_df, 10),
charts.chart7(filtered_df),
charts.chart8(filtered_df, lang)
]
return output
@app.callback(
[Output('counter2', 'children'), Output('count-message2', 'children'), Output('count-word2', 'children'), Output('count-emoji2', 'children'), Output('count-mention2', 'children'),
Output('count-media2', 'children'), Output('count-location2', 'children'), Output('count-link2', 'children'), Output('count-contact2', 'children'),
Output('chart-1p', 'figure'), Output('chart-2p', 'figure'), Output('chart-3p', 'figure'),
Output('avg-user2', 'children'), Output('avg-message2', 'children'), Output('avg-day2', 'children'), Output('avg-month2', 'children'),
Output('most-busy2', 'children'), Output('most-active2', 'children'), Output('most-silent2', 'children'), Output('most-typer2', 'children'),
Output('most-emoji2', 'children'), Output('most-media2', 'children'), Output('most-location2', 'children'), Output('most-link2', 'children'),
Output('most-contact2', 'children'), Output('most-mention2', 'children'), Output('most-add2', 'children'), Output('most-deleted2', 'children'),
Output('most-domain2', 'children'),
Output('chart-4p', 'figure'), Output('chart-5p', 'figure'), Output('chart-6p', 'figure'), Output('chart-7p', 'src'), Output('chart-8p', 'elements')],
[Input('dropdown-users2', 'value'), Input('date-picker2', 'start_date'), Input('date-picker2', 'end_date'), Input('time-interval1p', 'value'), Input('time-interval2p', 'value')],
[State('data-store', 'data')])
def update_personalchat(dropdown_users, start_date_str, end_date_str, interval1, interval2, datasets):
"""Update displayed data and chart at first and when filter apply."""
datasets = json.loads(datasets)
df = pd.read_json(datasets['data'], orient='split')
lang = datasets['lang']
start_date = date.min if start_date_str is None else datetime.strptime(start_date_str, '%Y-%m-%d').date()
end_date = date.max if end_date_str is None else datetime.strptime(end_date_str, '%Y-%m-%d').date()
filtered_df = df[
((df.contact.isin(dropdown_users)) | (len(dropdown_users) == 0)) &
(df.datetime.dt.date >= start_date) & (df.datetime.dt.date <= end_date)
]
output = [dash.no_update] * 34
ctx = dash.callback_context
if ctx.triggered[0]['prop_id'] == 'time-interval1.value':
output[9] = charts.chart1(filtered_df, interval1)
elif ctx.triggered[0]['prop_id'] == 'time-interval2.value':
output[29] = charts.chart4(filtered_df, interval2, 5)
else:
by_category = filtered_df[['contact', 'category']].pivot_table(index='contact', columns='category', aggfunc=len, fill_value=0).reindex(columns=settings.CATEGORIES, fill_value=0)
by_column = filtered_df.groupby('contact').sum(numeric_only=True)
by_domain = Counter(df[df.count_link > 0].list_link.sum())
output = [
ctx.triggered[0]['prop_id'],
'{:,} sent, {:,} deleted'.format(by_category.sum().sum() - by_category['Event'].sum(), by_category['Deleted'].sum()),
'{:,} sent'.format(by_column['count_words'].sum()),
'{:,} sent'.format(by_column['count_emoji'].sum()),
'{:,} sent'.format(by_column['count_mention'].sum()),
'{:,} shared'.format(by_category['Media'].sum()),
'{:,} shared'.format(by_category['Location'].sum()),
'{:,} shared'.format(by_column['count_link'].sum()),
'{:,} shared'.format(by_category['Contact'].sum()),
charts.chart1(filtered_df, interval1),
charts.chart2(filtered_df),
charts.chart3(filtered_df),
'{:,.2f} messages'.format((by_category.sum().sum() - by_category['Event'].sum())/filtered_df.contact.nunique(dropna=True)),
'{:,.2f} words, {:.2f} emoji'.format(by_column['count_words'].sum()/by_category['Text'].sum(), by_column['count_emoji'].sum()/by_category['Text'].sum()),
'{:,.2f} text, {:.2f} media'.format(by_category['Text'].sum()/filtered_df.date.nunique(), by_category['Media'].sum()/filtered_df.date.nunique()),
'{:,.2f} text, {:.2f} media'.format(by_category['Text'].sum()/filtered_df.month.nunique(), by_category['Media'].sum()/filtered_df.month.nunique()),
layouts.award_list(filtered_df.groupby('date').size().sort_values(ascending=False)),
layouts.award_list((by_category.sum(axis=1) - by_category['Event']).sort_values(ascending=False)),
layouts.award_list((by_category.sum(axis=1) - by_category['Event'])[lambda x: x > 0].sort_values(ascending=True)),
layouts.award_list(by_column['count_character'].sort_values(ascending=False)),
layouts.award_list(by_column['count_emoji'].sort_values(ascending=False)),
layouts.award_list(by_category['Media'].sort_values(ascending=False)),
layouts.award_list(by_category['Location'].sort_values(ascending=False)),
layouts.award_list(by_column['count_link'].sort_values(ascending=False)),
layouts.award_list(by_category['Contact'].sort_values(ascending=False)),
layouts.award_list(by_column['count_mention'].sort_values(ascending=False)),
layouts.award_list(filtered_df[(filtered_df.category == 'Event') & (filtered_df.event_type == 'added')].groupby('contact').size().sort_values(ascending=False)),
layouts.award_list(by_category['Deleted'].sort_values(ascending=False)),
layouts.award_list(by_domain),
charts.chart4(filtered_df, interval2, 5),
charts.chart5(filtered_df, 5),
charts.chart6(filtered_df, 10),
charts.chart7(filtered_df),
charts.chart8(filtered_df, lang)
]
return output
if __name__ == "__main__":
app.run_server(debug=True)