-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
212 lines (157 loc) · 5.99 KB
/
main.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
#Flask Packages
from flask import Flask, request, render_template, url_for, send_file, Response
#from flask import redirect, make_response, send_from_directory
#mongoMethods Dependencies
import pymongo
import tellurium
import dns
import oscillatorDB.mongoMethods as mm
#Miscellaneous Packages
from zipfile import ZipFile
from io import BytesIO
import os
import json #to convert string to dictionary
import random #for filename
# import base64
#Initializing Flask app and mongoMethods Startup
app = Flask(__name__, static_folder = 'static')
# mm.startup()
#Index Page
@app.route("/")
def index():
types = mm.get_model_types()
return render_template('index.html', types=types)
# return render_template('index_relationalop.html', types=types)
#Download Transition Page
#
# This route will take form data and convert parameters into a query dictionary.
#
@app.route("/download", methods = ['GET', 'POST'])
def download():
if request.method == 'GET':
error = "No query submitted. Please submit a query from the home page."
return render_template('error.html', error_msg=error)
elif request.method == 'POST':
#Retrieve Form Data
form_data = request.form #this is a dictionary w/ keys & values
modelType = form_data.get("mtype")
numSpecies = form_data.get("numSpecies")
numReactions = form_data.get("numReactions")
autocatalysis = form_data.get("autocat")
degradation = form_data.get("degrade")
telSimulatable = "isSimulatable" in form_data
if numSpecies != "": #numSpecies is not None
numSpecies = int(numSpecies)
else:
numSpecies = None
if numReactions != "": #why doesn't it default to None if no values found?!
numReactions = int(numReactions)
else:
numReactions = None
#IF AUTOCATALYSIS LEFT EMPTY IT DEFAULTS TO NONE
if autocatalysis == "autocat_yes":
autocatalysis = True
elif autocatalysis == "autocat_no":
autocatalysis = False
else:
autocatalysis = None
#IF DEGRADATION LEFT EMPTY IT DEFAULTS TO NONE
if degradation == "degrade_yes":
degradation = True
elif degradation == "degrade_no":
degradation = False
else:
degradation = None
queryParam = {
'type' : modelType,
'species' : numSpecies,
'reac' : numReactions,
'autocat' : autocatalysis,
'degrade' : degradation,
'simulatable' : telSimulatable
}
#DEBUG CODE: TO LOAD PAGE WITH QUERY VALUES UNDERNEATH
if cesiumQueryExists(queryParam) > 0:
return render_template('download.html', query=queryParam, queryJSON=json.dumps(queryParam), numModels=cesiumQueryExists(queryParam))
elif cesiumQueryExists(queryParam) == 0:
error = "No models found. Please try a different query."
return render_template('error.html', error_msg=error)
else:
error = "Invalid inputs. Please try again."
return render_template('error.html', error_msg=error)
#DEBUG CODE: TO CHECK THAT VALUES ARE ACTUALLY GOING THROUGH
# return form_data
#Download File Page
@app.route("/download/<query>")
def download_file(query):
query = json.loads(query)
numModels = cesiumQueryExists(query)
if numModels == 1:
filename = singleID(query)
else:
randInt = random.randint(1000000, 9999999)
filename = "{}.zip".format("cesium-models-" + str(numModels) + "-" + str(randInt))
cesiumZip = cesiumQuery(query)
return send_file(cesiumZip, download_name=filename, as_attachment=True)
# return send_file(cesiumZip, attachment_filename=filename, as_attachment=True)
#About Page
@app.route("/about")
def about():
return render_template('about.html')
# #FAQ Page
# @app.route("/faq")
# def faq():
# return render_template('faq.html')
#Returns number of models in query if it exists. Otherwise it returns 0.
def cesiumQueryExists(query):
dbquery = { "modelType" : query["type"], "numSpecies" : query["species"], "numReactions" : query["reac"], "Autcatalysis Present": query["autocat"], "Autodegradation Present": query["degrade"] }
for key in list(dbquery.keys()):
if dbquery[key] is None:
del dbquery[key]
model_IDS = mm.get_ids(dbquery)
if model_IDS:
return len(model_IDS)
else:
return 0
def cesiumQuery(query):
dbquery = { "modelType" : query["type"], "numSpecies" : query["species"], "numReactions" : query["reac"], "Autcatalysis Present": query["autocat"], "Autodegradation Present": query["degrade"] }
for key in list(dbquery.keys()):
if dbquery[key] is None:
del dbquery[key]
model_IDS = mm.get_ids(dbquery)
# Generate Single Text File
if len(model_IDS) == 1:
antStr = mm.get_antimony({ "ID" : model_IDS[0] })
# filename = str(model_IDS[0]) + ".txt"
if query["simulatable"]:
telStr = "import tellurium as te\n\nr = te.loada('''\n" + antStr + "\n''')\n\nm = r.simulate(0, 2, 400)\nr.plot()"
memTxtFile = BytesIO(bytes(telStr, encoding='utf-8'))
else:
memTxtFile = BytesIO(bytes(antStr, encoding='utf-8'))
memTxtFile.seek(0)
return memTxtFile
# Generate Zip File with all Text Files
elif model_IDS:
memZipFile = BytesIO()
with ZipFile(memZipFile, "w") as zipF:
for ID in model_IDS:
antStr = mm.get_antimony({ "ID" : ID })
txtFilename = str(ID) + ".txt"
if query["simulatable"]:
telStr = "import tellurium as te\n\nr = te.loada('''\n" + antStr + "\n''')\n\nm = r.simulate(0, 2, 400)\nr.plot()"
zipF.writestr(txtFilename, telStr)
else:
zipF.writestr(txtFilename, antStr)
memZipFile.seek(0)
return memZipFile
else:
return None
# Returns ID for queries with only one model
def singleID(query):
dbquery = { "modelType" : query["type"], "numSpecies" : query["species"], "numReactions" : query["reac"], "Autcatalysis Present": query["autocat"], "Autodegradation Present": query["degrade"] }
model_IDS = mm.get_ids(dbquery)
filename = str(model_IDS[0]) + ".txt"
return filename
#Flask Development Server
if __name__ == "__main__":
app.run(host="127.0.0.1", port=8080, debug=True)