-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathappconcatenating.py
291 lines (239 loc) · 10.5 KB
/
appconcatenating.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
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
288
289
290
# Data Libraries
import pandas as pd
import numpy as np
from pandas_profiling import ProfileReport
# Visualization Libraries
#import plotly as py
import plotly.graph_objs as go
import plotly.tools as tls
import plotly.express as px
from plotly.offline import iplot
import dash
from dash import Input, Output, html, dcc, dash_table
import dash_bootstrap_components as dbc
import os
data_path = 'data/winemag-data-130k-v2.csv'
## Importing the dataset
df_train = pd.read_csv(data_path, index_col=0)
# pandarallel.iniatilize()
target_path = "assets/profiling1.html"
if os.path.exists(target_path):
pass
else:
profile = ProfileReport(df_train, title='Pandas Profiling Report')
#profile = df_train.parallel_apply(profiling) #html={'style':{'full_width':True}}
profile.to_file(output_file="assets/profiling1.html")
## CSS EXTERNAL FILE
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css',
'https://codepen.io/chriddyp/pen/brPBPO.css']
## Defining the instance of dash
app = dash.Dash(__name__,
external_stylesheets=external_stylesheets)
# server instance to run map when deploying
server = app.server
# Since I am adding callbacks to elements that don’t ~
# exist in the app.layout as they are spread throughout files
app.config.suppress_callback_exceptions = True
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
import sklearn
from wordcloud import WordCloud, STOPWORDS
stopwords = set(STOPWORDS)
newStopWords = ['fruit', "Drink", "black", 'wine', 'drink']
stopwords.update(newStopWords)
def _wordcoud():
wordcloud = WordCloud(
background_color='white',
stopwords=stopwords,
max_words=300,
max_font_size=200,
width=1000, height=800,
random_state=42,
).generate(" ".join(df_wine1['description'].astype(str)))
print(wordcloud)
fig = plt.figure(figsize = (12,14))
plt.imshow(wordcloud)
# plt.title("WORD CLOUD - DESCRIPTION",fontsize=25)
# plt.axis('off')
# plt.show()
app.layout = html.Div([
html.H1('Dash Tabs component demo'),
dcc.Tabs(id="tabs-example",
children=[
dcc.Tab(label='Home', value='tab-1-example'),
dcc.Tab(label='Profilling data', value='tab-2-example'),
dcc.Tab(label='Tab Three', value='tab-3-example'),
dcc.Tab(label='Tab Four', value='tab-4-example'),
dcc.Tab(label='Tab Five', value='tab-5-example')
],
value='tab-1-example',
className='ten columns offset-by-one'),
html.Div(id='tabs-content-example')
], style={'margin':'0 auto'})
@app.callback(Output('tabs-content-example', 'children'),
[Input('tabs-example', 'value')])
def render_content(tab):
if tab == 'tab-1-example':
return html.Div([
html.Div([
dcc.Upload([
'Drag and Drop or ',
html.A('Select a File')
], style={
'width': '100%',
'height': '60px',
'lineHeight': '60px',
'borderWidth': '1px',
'borderStyle': 'dashed',
'borderRadius': '5px',
'textAlign': 'center'
})
], className='row'),
html.Div(
dash_table.DataTable(
id='table-sorting-filtering',
columns=[
{'name': i, 'id': i, 'deletable': True} for i in df_train.columns
],
#className='ten columns offset-by-one',
style_table={'overflowX': 'scroll'},
style_cell={
'height': '90',
# all three widths are needed
'minWidth': '140px',
'width': '140px',
'maxWidth': '140px',
'whiteSpace': 'normal'
},
page_current= 0,
page_size= 20,
page_action='custom',
filter_action='custom',
filter_query='',
sort_action='custom',
sort_mode='multi',
sort_by=[]
), className='ten columns offset-by-one')
])
elif tab == 'tab-2-example':
return html.Div([
html.H3('Data Profiling: '),
html.Iframe(
id='graph-1-tabs',
src=app.get_asset_url('profiling1.html'),
#src=app.get_asset_url('trich-dash.jpg'),
#className='ten columns offset-by-one',
style={ 'margin': '0 auto',
'width': '85%',
'height': '700px'}
)
], className='ten columns offset-by-one')
elif tab == 'tab-3-example':
return html.Div(dash_table.DataTable(
id='table-sorting-filtering',
columns=[
{'name': i, 'id': i, 'deletable': True} for i in df_train.columns
],
#className='ten columns offset-by-one',
style_table={'overflowX': 'scroll'},
style_cell={
'height': '90',
# all three widths are needed
'minWidth': '140px',
'width': '140px',
'maxWidth': '140px',
'whiteSpace': 'normal'
},
page_current= 0,
page_size= 10,
page_action='custom',
filter_action='custom',
filter_query='',
sort_action='custom',
sort_mode='multi',
sort_by=[]
), className='six columns')
elif tab == 'tab-4-example':
return html.Div([
dcc.Dropdown(
id='dropdown',
options=[{'label': i, 'value': i} for i in df_train['category'].unique()],
value='US'
),
dcc.Graph(
id='graph-4-tabs',
figure=tf_idf_words(), style={'margin':'0 auto'}
)
], className='ten columns offset-by-one', style={'margin':'0 auto'})
operators = [['ge ', '>='], ['le ', '<='], ['lt ', '<'],
['gt ', '>'], ['ne ', '!='], ['eq ', '='],
['contains '], ['datestartswith ']]
def split_filter_part(filter_part):
for operator_type in operators:
for operator in operator_type:
if operator in filter_part:
name_part, value_part = filter_part.split(operator, 1)
name = name_part[name_part.find('{') + 1: name_part.rfind('}')]
value_part = value_part.strip()
v0 = value_part[0]
if (v0 == value_part[-1] and v0 in ("'", '"', '`')):
value = value_part[1: -1].replace('\\' + v0, v0)
else:
try:
value = float(value_part)
except ValueError:
value = value_part
# word operators need spaces after them in the filter string,
# but we don't want these later
return name, operator_type[0].strip(), value
return [None] * 3
@app.callback(Output('table-sorting-filtering', 'data'),
[Input('table-sorting-filtering', "page_current"),
Input('table-sorting-filtering', "page_size"),
Input('table-sorting-filtering', 'sort_by'),
Input('table-sorting-filtering', 'filter_query')])
def update_table(page_current, page_size, sort_by, filter):
filtering_expressions = filter.split(' && ')
dff = df_train.copy()
for filter_part in filtering_expressions:
col_name, operator, filter_value = split_filter_part(filter_part)
if operator in ('eq', 'ne', 'lt', 'le', 'gt', 'ge'):
# these operators match pandas series operator method names
dff = dff.loc[getattr(dff[col_name], operator)(filter_value)]
elif operator == 'contains':
dff = dff.loc[dff[col_name].str.contains(filter_value)]
elif operator == 'datestartswith':
# this is a simplification of the front-end filtering logic,
# only works with complete fields in standard format
dff = dff.loc[dff[col_name].str.startswith(filter_value)]
if len(sort_by):
dff = dff.sort_values(
[col['column_id'] for col in sort_by],
ascending=[
col['direction'] == 'asc'
for col in sort_by
],
inplace=False
)
page = page_current
size = page_size
return dff.iloc[page * size: (page + 1) * size].to_dict('records')
@app.callback(Output('table-sorting-filtering', 'data'),
[Input('country-buttons', "page_current")])
def tf_idf_words(country_var):
vectorizer = TfidfVectorizer(ngram_range = (3, 3), min_df=2,
stop_words='english', max_features=20,
max_df=.5)
X2 = vectorizer.fit_transform(df_train.loc[(df_train.country == country_var)]['description'])
features = (vectorizer.get_feature_names())
scores = (X2.toarray())
# Getting top ranking features
sums = X2.sum(axis = 0)
data1 = []
for col, term in enumerate(features):
data1.append( (term, sums[0,col] ))
ranking = pd.DataFrame(data1, columns = ['term','rank'])
words = (ranking.sort_values('rank', ascending = False))[:15]
fig = px.bar(words, x='term', y='rank')
return fig
if __name__ == '__main__':
app.run_server(debug=True)