-
Notifications
You must be signed in to change notification settings - Fork 3
/
flask_app.py
198 lines (168 loc) · 6.42 KB
/
flask_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
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 29 11:28:57 2016
@author: Kirby Urner
"""
from flask import Flask, request, render_template
from connectors2 import Connector, elemsDB, glossaryDB, shapesDB
# from jsonrpc.backend.flask import api
from connectors2 import DB1, DB2, DB3
import json
import collections
app = Flask(__name__)
#app.register_blueprint(api.as_blueprint())
#app.add_url_rule('/', 'api', api.as_view(), methods=['POST'])
#========= TESTING json-rpc ==============
#@api.dispatcher.add_method
def foobar(arg):
if request.method == "POST":
print("RPC API!")
return json.dumps({"message":"success", "procedure":"foobar"})
#@api.dispatcher.add_method
def tetrahedron(*arg):
if request.method == "POST":
print("RPC API!")
return json.dumps({"message":"success", "procedure":"tetrahedron"})
#========= END TESTING ==============
@app.route("/")
def index():
return render_template("home.html")
@app.route("/elements")
def elements():
return render_template("elements.html")
@app.route("/links")
def links():
return render_template("links.html")
@app.route("/elements/<symbol>", methods=['GET', 'POST'])
def element_page(symbol):
if request.method == "POST":
with Connector(elemsDB(DB1)) as dbx:
result = dbx.update(request.form)
print(result)
with Connector(elemsDB(DB1)) as dbx:
result = dbx.seek(symbol)
if "NOT FOUND" != result:
if symbol != 'all':
data = json.loads(result)
return render_template("elem_page.html",
protons=data[0],
elem=data[1],
long_name=data[2],
mass=data[3],
series=data[4])
else:
data = json.loads(result)
data = collections.OrderedDict(sorted(data.items(),
key=lambda k: k[1][0]))
return render_template("all_elems.html", the_data=data)
return render_template("elements.html")
@app.route("/api/elements", methods=['GET', 'POST'])
def get_elements():
if request.method == "GET":
elem = request.args.get('elem')
result="NOT FOUND"
with Connector(elemsDB(DB1)) as dbx:
result = dbx.seek(elem)
if result != "NOT FOUND":
return result
return render_template("elements.html")
if request.method == "POST":
if request.form["secret"]=="DADA":
with Connector(elemsDB(DB1)) as dbx:
result = dbx.save(request.form)
return result
return "Oooo, you tried to post!"
#--------------------------------------------------
@app.route("/glossary")
def glossary():
return render_template("glossary.html")
@app.route("/glossary/<term>", methods=['GET', 'POST'])
def get_glossary(term):
if request.method == "POST":
print("POSTING DATA!!")
with Connector(glossaryDB(DB2)) as dbx:
result = dbx.update(request.form)
print(result)
with Connector(glossaryDB(DB2)) as dbx:
result = dbx.seek(term)
if "NOT FOUND" != result:
if term != 'all':
data = json.loads(result)
return render_template("term_page.html",
the_term = data[0],
the_definition = data[1])
else:
# print(result)
data = json.loads(result)
data = dict(sorted(data.items(), key=lambda k: k[0]))
return render_template("all_terms.html", the_data=data)
return render_template("glossary.html")
@app.route("/api/glossary", methods=['GET', 'POST'])
def get_terms():
if request.method == "GET":
term = request.args.get('term')
result="NOT FOUND"
with Connector(glossaryDB(DB2)) as dbx:
result = dbx.seek(term)
if result != "NOT FOUND":
return result
return render_template("terms.html")
if request.method == "POST":
print("Posting to glossary")
if request.form["secret"]=="DADA":
with Connector(glossaryDB(DB2)) as dbx:
result = dbx.save(request.form)
return result
return "Oooo, you tried to post!"
#--------------------------------------------------
@app.route("/shapes")
def shapes():
return render_template("shapes.html")
@app.route("/shapes/<abbrev>", methods=['GET', 'POST'])
def get_polyhedrons(abbrev):
if request.method == "POST":
print("POSTING DATA!!")
with Connector(shapesDB(DB3)) as dbx:
result = dbx.update(request.form)
print(result)
with Connector(shapesDB(DB3)) as dbx:
result = dbx.seek(abbrev)
if "NOT FOUND" != result:
if abbrev != 'all':
data = json.loads(result)
return render_template("shape_page.html",
shape_id = data[0],
shape = data[1],
abbrev = data[2],
shape_v = data[3],
shape_f = data[4],
shape_e = data[5],
shape_dual_id = data[6],
shape_volume = data[7])
else:
data = json.loads(result)
data = collections.OrderedDict(sorted(data.items(),
key=lambda k: k[1][0]))
return render_template("all_shapes.html", the_data=data)
return render_template("shapes.html")
@app.route("/api/shapes", methods=['GET', 'POST'])
def get_shapes():
if request.method == "GET":
term = request.args.get('shape')
result="NOT FOUND"
with Connector(shapesDB(DB3)) as dbx:
result = dbx.seek(term)
if result != "NOT FOUND":
return result
return render_template("shapes.html")
if request.method == "POST":
if request.form["secret"]=="DADA":
with Connector(shapesDB(DB3)) as dbx:
result = dbx.save(request.form)
return result
return "Oooo, you tried to post!"
@app.errorhandler(404)
def page_not_found(e):
return render_template("home.html")
if __name__ == '__main__':
app.run(port=5000, debug=True)