-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
276 lines (217 loc) · 9.05 KB
/
app.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
import os
import jsonpickle
import pandas as pd
from flask import Flask, flash, render_template, session, request, redirect, url_for
from werkzeug.utils import secure_filename
from configuration import const_types
from configuration import key
from configuration import paths
from controllers.data_summary import DataSummary
from controllers.data_univariate import DataUnivariate
from controllers.data_bivariate import DataBivariate
from model.dataset import DataSet
from model.datasets import DataSets
app = Flask(__name__)
# Path and upload information
app.config['UPLOAD_FOLDER'] = paths.UPLOAD_FOLDER
app.config['EXAMPLES_FOLDER'] = paths.EXAMPLES_FOLDER
app.secret_key = key.SECRET_KEY
@app.before_first_request
def getmenu():
if not os.path.isfile(paths.DATASETS_JSON):
dataset_info = []
# Read in CSV
datasets_csv = pd.read_csv(paths.DATASETS)
# Get title, filename, id, and label for each data set and add it to the collection
for i, row in datasets_csv.iterrows():
dataset_title = row["Title"]
dataset_filename = row["FileName"]
dataset_id = row["ID"]
dataset_label = row["Label"]
dataset = DataSet(dataset_filename, dataset_title, dataset_id, dataset_label)
dataset_info.append(dataset)
# Save the collection as JSON and return it
datasets = DataSets(dataset_info=dataset_info)
datasets_json = jsonpickle.encode(datasets)
# Save the serialized JSON to a file
with open(paths.DATASETS_JSON, 'w') as file:
file.write(datasets_json)
else:
with open(paths.DATASETS_JSON, 'r') as serialized_file:
json_str = serialized_file.read()
datasets_json = jsonpickle.decode(json_str)
return datasets_json
def selecteddataset():
data_file = None
data_title = None
data_id = None
data_label = None
data_uploaded = False
# Check if the values are already in session
if "data_file" in session:
data_file = session["data_file"]
if "data_title" in session:
data_title = session["data_title"]
if "data_id" in session:
data_id = session["data_id"]
if "data_label" in session:
data_label = session["data_label"]
if "data_file_uploaded" in session and session["data_file_uploaded"] == data_file:
data_uploaded = True
# Make sure that at least the file and title are populated, or else get it from the page
if data_file is None or data_title is None:
# Get the current selected values
data_file = "iris.csv"
data_title = "Iris"
data_id = "ID"
data_label = "Species"
data_uploaded = False
# Save values in session for future requests
session["data_file"] = data_file
session["data_title"] = data_title
session["data_id"] = data_id
session["data_label"] = data_label
return [data_file, data_title, data_id, data_label, data_uploaded]
@app.route('/dataset_selection_changed', methods=['POST'])
def dataset_selection_changed():
# Get the selected data set's name
new_selection = str(request.form["data_set_field"])
# Look up the Title, ID, Label (for existing data sets)
datasets = pd.read_csv(paths.DATASETS)
dataset = datasets.loc[datasets["FileName"] == new_selection]
if dataset is not None:
new_title = dataset["Title"].values[0]
new_index = dataset["ID"].values[0]
new_label = dataset["Label"].values[0]
# Save the selection in session
session["data_file"] = new_selection
session["data_title"] = new_title
session["data_id"] = new_index
session["data_label"] = new_label
# Redirect and reload the appropriate page
if request.referrer is not None:
return redirect(request.referrer)
else:
return redirect(url_for('index'))
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in const_types.ALLOWED_EXTENSIONS
@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
# Check if a file was passed into the request
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
# Get the uploaded file
file = request.files['file']
# Check if a file was not selected
if file.filename == '':
flash('No selected file')
return redirect(request.url)
# If the file was uploaded and is an allowed type, proceed with upload
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
filepath = os.path.join(paths.UPLOAD_FOLDER, filename)
file.save(filepath)
data_title = ""
data_id = ""
data_label = ""
if "label" in request.form:
data_title = request.form["title"]
if "id" in request.form:
data_id = request.form["id"]
if "label" in request.form:
data_label = request.form["label"]
# Move the file and set metadata
datasetuploaded(uploaded_file_path=str(filepath),
data_title=data_title,
data_id=data_id,
data_label=data_label)
# Return to the summary page and show the new data set info
return redirect(url_for('index'))
else:
dataset_options = getmenu()
return render_template('upload.html', dataset_options=dataset_options)
def datasetuploaded(uploaded_file_path, data_title, data_id=None, data_label=None):
# Create folder with graphs sub folder
data_path = os.path.join(paths.UPLOAD_FOLDER, data_title)
if not os.path.exists(data_path):
os.makedirs(data_path)
os.makedirs(os.path.join(data_path, "graphs"))
# Move the uploaded file to this directory
file_name = uploaded_file_path.split("/")[-1]
os.rename(uploaded_file_path, str(data_path + "/" + file_name))
# Update the list of options to select from
session["data_file"] = file_name
session["data_title"] = data_title
session["data_id"] = data_id
session["data_label"] = data_label
# Set the uploaded one
session["data_file_uploaded"] = file_name
session["data_title_uploaded"] = data_title
session["data_id_uploaded"] = data_id
session["data_label_uploaded"] = data_label
def getuploadeddataset():
data_file = None
data_title = None
data_id = None
data_label = None
if "data_file_uploaded" in session:
data_file = session["data_file_uploaded"]
if "data_title_uploaded" in session:
data_title = session["data_title_uploaded"]
if "data_id_uploaded" in session:
data_id = session["data_id_uploaded"]
if "data_label_uploaded" in session:
data_label = session["data_label_uploaded"]
if data_file is not None and data_title is not None:
return [data_file, data_title, data_id, data_label, True]
return None
@app.route('/')
@app.route('/index')
def index():
# data_file, data_title, data_id, data_label = selecteddataset()
selected_dataset = selecteddataset()
dataset_options = getmenu()
uploaded_dataset = getuploadeddataset()
driver = DataSummary(selected_dataset)
# Get the JSON for the summary data
summary_json = driver.load_summary_json()
error_msg = driver.get_error_msg()
return render_template('index.html',
data=summary_json,
data_file=selected_dataset[0],
dataset_options=dataset_options,
uploaded_dataset=uploaded_dataset,
error_msg=error_msg)
@app.route('/univariate')
def univariate():
selected_dataset = selecteddataset()
dataset_options = getmenu()
uploaded_dataset = getuploadeddataset()
driver = DataUnivariate(selected_dataset)
# Get the JSON for the summary data
features_json = driver.load_features_json()
error_msg = driver.get_error_msg()
return render_template('univariate.html',
mydata=features_json,
data_file=selected_dataset[0],
dataset_options=dataset_options,
uploaded_dataset=uploaded_dataset,
error_msg=error_msg)
@app.route('/bivariate')
def bivariate():
selected_dataset = selecteddataset()
dataset_options = getmenu()
uploaded_dataset = getuploadeddataset()
driver = DataBivariate(selected_dataset)
# Get the JSON for the summary data
interactions_json = driver.load_interactions_json()
error_msg = driver.get_error_msg()
return render_template('bivariate.html',
data=interactions_json,
data_file=selected_dataset[0],
dataset_options=dataset_options,
uploaded_dataset=uploaded_dataset,
error_msg=error_msg)