-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
200 lines (170 loc) · 6.98 KB
/
app.py
File metadata and controls
200 lines (170 loc) · 6.98 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
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.express as px
import pandas as pd
from wordcloud import WordCloud
import base64
from io import BytesIO
import geopandas
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
import json
from word_cloud_colors import Freq_colormap_color_func
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
f_country = open('data/country_options.json')
drop_down_options = json.load(f_country)
f_colours = open('data/color_options.json')
colorscales = json.load(f_colours)
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
server = app.server
colors = {
'background': '#111111',
'text': '#275f6b'
}
left_style = {
'color': colors['text'],
'textAlign': 'left'
}
filename = "data/country_geo_topic_counts.gpkg"
reut_country_geo_topic = geopandas.read_file(filename)
reut_country_geo_topic.set_index('index', inplace=True)
reut_country_geo_topic['topiccounts'] = reut_country_geo_topic['topiccounts'].apply(
eval)
app.layout = html.Div(children=[
html.H1(id='title', style={
'textAlign': 'center',
'color': colors['text']
}),
html.H4(id='docs', style={
'textAlign': 'center',
'color': colors['text']
}),
html.Div([
html.Div(children=[
html.Table([
html.Tr(
[
html.Td(html.Label('Choose the country',
style={
'color': colors['text'],
'textAlign': 'left',
'width': '60%'
}), style={'border': 'none'}),
html.Td(html.Label('Choose the colour scale',
style=left_style), style={'border': 'none'}),
html.Td(html.A('Info', href='https://github.com/apndx/ReutersVisualizer',
style=left_style, target='_blank'), style={'border': 'none'}),
]
),
html.Tr(
[
html.Td(
dcc.Dropdown(
id='dropdown',
options=drop_down_options,
value='WORLD', style=left_style), style={'border': 'none', 'width': '60%'}),
html.Td(
dcc.Dropdown(
id='colorscale',
options=colorscales,
value='viridis', style=left_style), style={'border': 'none'})
]),
])
]),
html.Div([
html.Div(dcc.Graph(id='country_topic'),
style={
'backgroundColor': 'white',
'margin-left': '10px',
'width': '45%',
'text-align': 'center',
'display': 'inline-block',
'vertical-align': 'top'
}),
html.Div(html.Img(id='cloud'),
style={
'backgroundColor': 'white',
'margin-left': '10px',
'width': '45%',
'text-align': 'center',
'display': 'inline-block',
'padding-top': '30px'
}),
]),
])])
@app.callback(
Output(component_id='cloud', component_property='src'),
Input('dropdown', 'value'),
Input('colorscale', 'value'))
def update_cloud(selected_country, scale):
country_dict = reut_country_geo_topic.loc[selected_country]['topiccounts']
country_fig = plt.figure(figsize=(7, 7))
ax = country_fig.add_axes([0, 0, 1, 1])
ax.axis('off')
ax.margins(0)
own_colour_func = Freq_colormap_color_func(scale, country_dict)
country_cloud = None
has_geometry = reut_country_geo_topic.loc[selected_country]['geometry'] != None
if has_geometry:
reut_country_geo_topic.loc[[selected_country]].plot('count', ax=ax)
plt.savefig(f'pics/{selected_country}.png',
bbox_inches="tight", pad_inches=0)
country_mask = np.array(Image.open(f'pics/{selected_country}.png'))
country_cloud = WordCloud(background_color="white", mask=country_mask,
width=900, height=900, contour_width=0.5, color_func=own_colour_func)
elif selected_country == 'WORLD':
reut_country_geo_topic.plot('count', ax=ax)
plt.savefig(f'pics/{selected_country}.png',
bbox_inches="tight", pad_inches=0)
country_mask = np.array(Image.open(f'pics/{selected_country}.png'))
country_cloud = WordCloud(background_color="white", mask=country_mask,
width=900, height=900, contour_width=0.5, color_func=own_colour_func)
else:
country_cloud = WordCloud(
background_color="white", width=700, height=700, color_func=own_colour_func)
country_cloud.generate_from_frequencies(country_dict)
wc_img = country_cloud.to_image()
with BytesIO() as buffer:
wc_img.save(buffer, 'png')
cloud_fig = base64.b64encode(buffer.getvalue()).decode()
src = "data:image/png;base64," + cloud_fig
plt.clf()
plt.close(country_fig)
return src
@app.callback(
Output(component_id='country_topic', component_property='figure'),
Input('dropdown', 'value'),
Input('colorscale', 'value'))
def update_country_topics(selected_country, scale):
scale = scale.lower()
country_dict = reut_country_geo_topic.loc[selected_country]['topiccounts']
topic_keys = list(country_dict.keys())
topic_values = list(country_dict.values())
topic_fig = px.bar(x=topic_keys, y=topic_values, color=topic_values, height=800, labels={
'x': 'Topic', 'y': 'Times used', 'color': 'Times used'}, color_continuous_scale=scale)
topic_fig.update_layout(transition_duration=500)
return topic_fig
@app.callback(
Output(component_id='title', component_property='children'),
Input('dropdown', 'value')
)
def update_title(selected_country):
if selected_country == 'WORLD':
return 'Reuters topics for the World'
else:
return 'Reuters topics for {}'.format(selected_country.title())
@app.callback(
Output(component_id='docs', component_property='children'),
Input('dropdown', 'value')
)
def update_subtitle(selected_country):
country_dict = reut_country_geo_topic.loc[selected_country]['topiccounts']
topic_count = sum(country_dict.values())
doc_count = int(reut_country_geo_topic.loc[selected_country]['count'])
unique_topics = len(country_dict)
return 'Total of {} documents with {} topics, {} of topics unique'.format(doc_count, topic_count, unique_topics)
if __name__ == '__main__':
app.run_server(debug=True)