-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
327 lines (296 loc) · 13.7 KB
/
Copy pathmain.py
File metadata and controls
327 lines (296 loc) · 13.7 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
from flask import Flask, render_template, request, redirect, session, make_response, jsonify, send_from_directory, abort
from decimal import Decimal
from werkzeug.utils import secure_filename
import matplotlib.pyplot as plt
import os
import os.path
import dataframe as df
app = Flask(__name__)
app.config['FILE_UPLOADS'] = os.getcwd() + '/static/uploads'
app.secret_key = 'kldjlkmxcvioermklxjos90873489*&86*&I09'
dataframe_object = df.Dataframe()
@app.route('/')
def dash_board() -> 'html':
return render_template('dash_board.html', the_title='Dashboard')
@app.route('/upload')
def upload() -> 'html':
return render_template('file_upload.html', the_title='Upload File')
@app.route('/__verify_upload__', methods=['POST'])
def verify_upload() -> 'json':
uploaded_file = request.files['file']
file_name = secure_filename(uploaded_file.filename)
try:
uploaded_file.save(os.path.join(app.config['FILE_UPLOADS'], file_name))
session['filename'] = file_name
dataframe_object.set_filename(file_name)
dataframe_object.set_dataframe()
dataframe = dataframe_object.get_dataframe(False)
(row, col) = dataframe.shape
titles = list(dataframe.columns)
data = [list(dataframe[col])
for col in dataframe.columns]
response = {
'titles': titles,
'data': data,
'col': col,
'row': row
}
except Exception as e:
print(e)
return make_response(jsonify(response), 200)
@app.route('/fileuploadcomplete')
def fileuploadcomplete() -> 'html':
if 'filename' in session:
return render_template('file_upload_complete.html', the_title='Filter Here')
else:
return redirect('/upload')
@app.route('/__return_titles__', methods=['POST'])
def return_titles() -> 'json':
req = request.get_json()
if req['data'] == 'Send Titles':
titles = dataframe_object.get_titles()
data = {
'titles': titles
}
return make_response(data, 200)
@app.route('/__return_data__', methods=['POST'])
def return_data() -> 'json':
req = request.get_json()
col_val = []
titles = []
if req['data'] == 'Send Data':
col_val = dataframe_object.get_unique_col_val()
titles = dataframe_object.get_titles()
i = 0
data = {}
for items in col_val:
data[titles[i]] = items
i += 1
return make_response(data, 200)
def generate_query(packet) -> 'query':
data_type = dataframe_object.get_data_type()
if (packet['filter'] == 'no-comparing'):
data = packet['data']
titles = packet['titles']
columns = []
for items in titles:
columns.append(f"{items}")
query = ""
for keys, values in data.items():
filter = values
for item in filter:
if item == keys:
filter.remove(keys)
if len(filter) != 0:
if data_type[keys] == 'object':
query += ' or '.join(keys + ' == "' + items +
'"' for items in filter)
query += " and "
else:
query += ' or '.join(keys + " == " + items
for items in filter)
query += " and "
query = query[:-5]
dataframe = dataframe_object.get_original_dataframe()
return dataframe.query(query).filter(columns)
else:
data = packet['data']
titles = packet['titles']
columns = []
for items in titles:
columns.append(f"{items}")
query = ""
for keys, values in data.items():
operator = values[0][0]
condition = values[1]
for item in condition:
if item == keys:
condition.remove(keys)
if len(condition) != 0:
if data_type[keys] == 'object':
if operator == 'Greater than (>)':
query += ' and '.join(keys + " > '" + items +
"'" for items in condition)
query += " and "
if operator == 'Greater than equal to (>=)':
query += ' and '.join(keys + " >= '" + items +
"'" for items in condition)
query += " and "
if operator == 'Less than (<)':
query += ' and '.join(keys + " < '" + items +
"'" for items in condition)
query += " and "
if operator == 'Less than equal to (<=)':
query += ' and '.join(keys + " <= '" + items +
"'" for items in condition)
query += " and "
if operator == 'Not equal to (!=)':
query += ' and '.join(keys + " != '" + items +
"'" for items in condition)
query += " and "
if operator == 'Between':
if len(condition) < 2:
continue
else:
a = max(condition[0], condition[1])
b = min(condition[0], condition[1])
query += keys + " >= '" + \
b + "' and " + keys + \
" <= '" + a + "'"
query += " and "
else:
if operator == 'Greater than (>)':
query += ' and '.join(keys + " > " +
items for items in condition)
query += " and "
if operator == 'Greater than equal to (>=)':
query += ' and '.join(keys + " >= " +
items for items in condition)
query += " and "
if operator == 'Less than (<)':
query += ' and '.join(keys + " < " +
items for items in condition)
query += " and "
if operator == 'Less than equal to (<=)':
query += ' and '.join(keys + " <= " +
items for items in condition)
query += " and "
if operator == 'Not equal to (!=)':
query += ' and '.join(keys + " != " +
items for items in condition)
query += " and "
if operator == 'Between':
if len(condition) < 2:
continue
else:
a = str(
max(Decimal(condition[0]), Decimal(condition[1])))
b = str(
min(Decimal(condition[0]), Decimal(condition[1])))
query += keys + " >= " + \
b + " and " + keys + \
" <= " + a
query += " and "
query = query[:-5]
dataframe = dataframe_object.get_original_dataframe()
return dataframe.query(query).filter(columns)
@app.route('/__return_filter_data__', methods=['POST'])
def filter_data() -> 'json':
packet = request.get_json()
dataframe = generate_query(packet)
dataframe.insert(0, 'S.no', range(1, len(dataframe) + 1))
(row, col) = dataframe.shape
titles = list(dataframe.columns)
data = [list(dataframe[col])
for col in dataframe.columns]
return_data = {
'data': data,
'row': row,
'col': col,
'titles': titles
}
return make_response(return_data, 200)
@app.route('/results')
def results() -> 'html':
if 'filename' in session:
return render_template('results.html', the_title='Results')
else:
return redirect('/upload')
@app.route('/graphs')
def graphs() -> 'html':
if 'filename' in session:
return render_template('graphs.html', the_title='Visualize')
else:
return redirect('/upload')
@app.route('/__histogram__', methods=['POST'])
def histogram() -> 'json':
req = request.get_json()
titles = dataframe_object.get_titles()
data = {
'titles': titles
}
return make_response(jsonify(data), 200)
@app.route('/__draw_histogram__', methods=['POST'])
def draw_histogram() -> 'json':
req = request.get_json()
path = os.getcwd() + '/static/graphs'
intpart = len(os.listdir(path))
filename = 'output' + str(intpart + 1)
path = os.getcwd() + '/static/uploads/' + session['filename']
if req['graph_type'] == 'Histogram':
contents = f"import pandas as pd\nimport matplotlib.pyplot as plt\ndf=pd.read_csv('{path}')\n\n\nplt.figure(num=None, figsize=(20, 10), dpi=80, facecolor='w', edgecolor='k')\ndf['{req['x_selected']}'].sort_values().hist(bins=100)\nplt.xlabel('{req['x_label']}', fontsize=20)\nplt.ylabel('{req['y_label']}', fontsize=20)\nplt.title('Graph for {req['x_selected']}', fontsize=20)\nplt.xticks(rotation=45)\n\n\nplt.savefig('static/graphs/{filename}.png')"
hist = open("static/scripts/hist.py", "w")
hist.write(contents)
hist.close()
try:
os.system("static/scripts/eg.sh")
except Exception as e:
print(e)
elif req['graph_type'] == 'Box Plot':
contents = f"import pandas as pd\nimport matplotlib.pyplot as plt\ndf=pd.read_csv('{path}')\n\n\ndf['{req['x_selected']}'].sort_values().plot.box(figsize=(20, 10))\nplt.xlabel('{req['x_label']}', fontsize=20)\nplt.ylabel('{req['y_label']}', fontsize=20)\nplt.title('Graph for {req['x_selected']}', fontsize=20)\nplt.xticks(rotation=0)\n\n\nplt.savefig('static/graphs/{filename}.png')"
hist = open("static/scripts/hist.py", "w")
hist.write(contents)
hist.close()
try:
os.system("static/scripts/eg.sh")
except Exception as e:
print(e)
else:
contents = f"import pandas as pd\nimport matplotlib.pyplot as plt\ndf=pd.read_csv('{path}')\n\n\ndf['{req['x_selected']}'].sort_values().plot.kde(bw_method = 0.3, figsize=(10, 20))\nplt.xlabel('{req['x_label']}', fontsize=20)\nplt.ylabel('{req['y_label']}', fontsize=20)\nplt.title('Graph for {req['x_selected']}', fontsize=20)\nplt.xticks(rotation=0)\n\n\nplt.savefig('static/graphs/{filename}.png')"
hist = open("static/scripts/hist.py", "w")
hist.write(contents)
hist.close()
try:
os.system("static/scripts/eg.sh")
except Exception as e:
print(e)
return make_response(jsonify({'name': f'{filename}.png'}), 200)
@app.route('/graph_result')
def graph_result() -> 'html':
if 'filename' in session:
return render_template('graph_result.html', the_title='Graph')
else:
return redirect('/upload')
@app.route('/__draw_other_graphs__', methods=['POST'])
def draw_other_graphs() -> 'json':
req = request.get_json()
path = os.getcwd() + '/static/graphs'
intpart = len(os.listdir(path))
filename = 'output' + str(intpart + 1)
path = os.getcwd() + '/static/uploads/' + session['filename']
if req['graph_type'] == 'Line Chart':
contents = f"import pandas as pd\nimport matplotlib.pyplot as plt\ndf=pd.read_csv('{path}')\n\n\ndf.plot.line(x='{req['x-axis']}', y='{req['y-axis']}', figsize=(20, 10))\nplt.xlabel('{req['x_label']}', fontsize=20)\nplt.ylabel('{req['y_label']}', fontsize=20)\nplt.title('Graph for {req['x-axis']} vs {req['y-axis']}', fontsize=20)\nplt.xticks(rotation=45)\n\n\nplt.savefig('static/graphs/{filename}.png')"
hist = open("static/scripts/hist.py", "w")
hist.write(contents)
hist.close()
try:
os.system("static/scripts/eg.sh")
except Exception as e:
print(e)
elif req['graph_type'] == 'Scatter Chart':
contents = f"import pandas as pd\nimport matplotlib.pyplot as plt\ndf=pd.read_csv('{path}')\n\n\ndf.plot.scatter(x='{req['x-axis']}', y='{req['y-axis']}', figsize=(20, 10))\nplt.xlabel('{req['x_label']}', fontsize=20)\nplt.ylabel('{req['y_label']}', fontsize=20)\nplt.title('Graph for {req['x-axis']} vs {req['y-axis']}', fontsize=20)\nplt.xticks(rotation=45)\n\n\nplt.savefig('static/graphs/{filename}.png')"
hist = open("static/scripts/hist.py", "w")
hist.write(contents)
hist.close()
try:
os.system("static/scripts/eg.sh")
except Exception as e:
print(e)
else:
contents = f"import pandas as pd\nimport matplotlib.pyplot as plt\ndf=pd.read_csv('{path}')\n\n\ndf.plot.hexbin(x='{req['x-axis']}', y='{req['y-axis']}', figsize=(20, 10))\nplt.xlabel('{req['x_label']}', fontsize=20)\nplt.ylabel('{req['y_label']}', fontsize=20)\nplt.title('Graph for {req['x-axis']} vs {req['y-axis']}', fontsize=20)\nplt.xticks(rotation=45)\n\n\nplt.savefig('static/graphs/{filename}.png')"
hist = open("static/scripts/hist.py", "w")
hist.write(contents)
hist.close()
try:
os.system("static/scripts/eg.sh")
except Exception as e:
print(e)
return make_response(jsonify({'name': f'{filename}.png'}), 200)
if __name__ == '__main__':
app.run(debug=True)
path = os.getcwd() + '/static/graphs/'
for file_name in os.listdir(path):
try:
os.remove(path + file_name)
except Exception as e:
print(e)