-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp_lite.py
320 lines (270 loc) · 10.6 KB
/
app_lite.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
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
from flask import Flask, jsonify, request,g, render_template
from flask import Flask, flash, request, redirect, url_for
from flask import Flask, session
from werkzeug.utils import secure_filename
import click
from flask import current_app, g
from flask.cli import with_appcontext
from flask_cors import CORS, cross_origin
from werkzeug.contrib.cache import SimpleCache
app = Flask(__name__)
app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
cache = SimpleCache()
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'}
@app.route("/")
def hello():
#g.namespaces = Data.summary()
# if g.namespaces:
# g.selected_namespace = g.namespaces[0]
# g.subnetwork = {"nodes": [1, 2, 3, 4, 5], "edges": [[1, 2], [2, 4], [3, 5], [2, 5]]}
# g.subnetwork2 = {}
# g.subnetwork2["nodes"] = [{"id": str(x), "group": str(x)} for x in g.subnetwork["nodes"]]
# g.subnetwork2["links"] = [{"source": x[0], "target": x[1]} for x in g.subnetwork["edges"]]
return render_template('dashboard_demo.html', name="jen")
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
#print(request.args)
print("namespace", request.args.get("namespace"))
form = request.form.to_dict()
print(request.files)
if form["namespace"] == "New":
namespace = form["new_name"]
Upload.create_new_namespace(form["new_name"])
else:
namespace = form["existing_namespace"]
if not namespace:
return "namespace fail"
Upload.create_new_temp_namespace(namespace)
Upload.upload_to_new_namespace(namespace, request.files.getlist("layouts"))
Upload.upload_edges_to_new_namespace(namespace, request.files.getlist("links"))
Upload.upload_labels_to_new_namespace(namespace, request.files.getlist("labels"))
return "hello"
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# if user does not select file, browser also
# submit an empty part without filename
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
#file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('uploaded_file',
filename=filename))
data = Data.summary()
print(data)
return render_template('upload.html', data=data)
@app.route('/uploaded_file', methods=['GET'])
def uploaded_file():
filename = request.args.get('filename')
return render_template('uploaded_file.html', filename=filename)
@app.route('/<string:db_namespace>/search', methods=['GET'])
def old_swimmer(db_namespace):
return render_template('dd_dash.html')
@app.route('/swimmer', methods=['GET'])
def swimmer():
return render_template('dd_dash.html')
'''
########
# Data #
########
@app.route("/api/namespace/summary", methods=["GET"])
@cross_origin()
def get_summary():
return jsonify(Data.summary())
@app.route("/api/<string:db_namespace>/subgraph", methods=['GET'])
@cross_origin()
def get_subgraph(db_namespace):
nodes = request.args.getlist('node_ids')
attribute = request.args.getlist('attribute_id')
if attribute:
nodes = [node["id"] for node in Node.nodes_for_attribute(db_namespace, attribute)]
if not nodes:
return "Fail"
edges = Edge.for_nodelist(db_namespace, nodes)
nodes = Node.get(db_namespace, nodes)
return jsonify({"nodes": nodes, "edges": edges})
########
# Node #
########
@app.route("/api/<string:db_namespace>/node", methods=['GET'])
@cross_origin()
def nodes(db_namespace):
prefix = request.args.get('prefix') or ""
symbols = request.args.getlist("symbols")
external_ids = request.args.getlist("external_ids")
node_ids = request.args.getlist("id")
neighbors = request.args.getlist("neighbor")
random = request.args.get('random') or None
attribute_ids = request.args.getlist("attribute_id")
print(node_ids)
if random:
return jsonify(Node.show_random(random, db_namespace))
if node_ids:
return jsonify(Node.get(db_namespace, node_ids))
if external_ids:
return jsonify(Node.get_by_external_ids(db_namespace, external_ids))
if symbols:
return jsonify(Node.get_by_symbols(db_namespace, symbols))
if neighbors:
return jsonify(Node.get_neighbors(db_namespace, neighbors))
if attribute_ids:
return jsonify(Node.nodes_for_attribute(db_namespace, attribute_ids))
return jsonify(Node.nodes_for_autocomplete(db_namespace, prefix))
@app.route('/api/<string:db_namespace>/node/random_walk', methods=['GET', 'POST'])
@cross_origin()
def random_walk(db_namespace):
if request.method == 'POST':
#data = request.form
data =request.get_json()
else:
data = request.args
# node_ids = data.node_ids
node_ids = [int(x) for x in data['node_ids']]
print(node_ids)
#node_ids = [int(x) for x in data.getlist("node_ids")]
restart_probability = data["restart_probability"]
restart_probability = float(restart_probability or 0.9)
min_frequency = data["min_frequency"]
min_frequency = float(min_frequency or 0)
return jsonify(Node.random_walk(db_namespace, node_ids, restart_probability, min_frequency))
@app.route('/api/<string:db_namespace>/node/shortest_path', methods=['GET'])
@cross_origin()
def shortest_path(db_namespace):
from_id = request.args.get("from")
to_id = request.args.get("to")
return jsonify(Node.shortest_path(db_namespace, from_id, to_id))
@app.route("/api/<string:namespace>/node/search", methods=['GET', 'POST'])
@cross_origin()
def search(namespace):
if request.method == 'POST':
data = request.form
results = Node.search(namespace, data)
return jsonify(results)
else:
data = request.args
results = Node.search(namespace, data)
return jsonify(results)
#############
# Attribute #
#############
@app.route("/api/<string:db_namespace>/attribute/", methods=['GET'])
@cross_origin()
def attributes_for_node(db_namespace):
attr_namespace = request.args.get("namespace") or None
node_id = request.args.get("node_id")
external_ids = request.args.getlist("external_ids")
if node_id:
return jsonify(Attribute.attributes_for_node(db_namespace, node_id, attr_namespace))
prefix = request.args.get("prefix") or ""
if prefix:
return jsonify(Attribute.attributes_for_autocomplete(db_namespace, prefix, attr_namespace))
if external_ids:
return jsonify(Attribute.attributes_for_external_ids(db_namespace, external_ids))
else:
return "no arguments supplied!"
# Cache database call for attribute taxonomy to prevent repeated queries.
def get_attribute_taxonomy(db_namespace, root_node_id):
zkey = 'attribute_taxonomy_parents-%s-%d' % (db_namespace, root_node_id)
dtc = cache.get(zkey)
if dtc is None:
dtc = AttributeTaxonomy.construct_taxonomy(db_namespace, root_node_id)
cache.set(zkey, dtc, timeout=5 * 60 * 60 * 24)
return dtc
@app.route("/api/<string:db_namespace>/attribute_taxonomy/<int:root_node_id>", methods=['GET'])
@cross_origin()
def taxonomy(db_namespace, root_node_id):
return jsonify(get_attribute_taxonomy(db_namespace, root_node_id))
@app.route("/api/<string:db_namespace>/attribute_taxonomy/", methods=['GET'])
@cross_origin()
def taxonomy_from_name(db_namespace):
taxonomy_name = request.args.get("namespace")
root_node_id = AttributeTaxonomy.get_root(db_namespace, taxonomy_name)
if root_node_id:
return jsonify(get_attribute_taxonomy(db_namespace, root_node_id))
else:
return jsonify([])
@app.route("/api/<string:db_namespace>/attribute/namespaces", methods=["GET"])
@cross_origin()
def attribute_namespaces(db_namespace):
return jsonify(Attribute.get_attribute_namespaces(db_namespace))
@app.route("/api/<string:db_namespace>/selection/create", methods=['GET', 'POST'])
@cross_origin()
def create_selection(db_namespace):
if request.method == 'POST':
data = request.form
selection_name = data.get("selection_name")
node_ids = data.get("node_ids")
if not data:
data = request.get_json(force=True)
selection_name = data["selection_name"]
node_ids = data["node_ids"]
else:
data = request.args
selection_name = data.get("selection_name")
node_ids = data.get("node_ids")
if not selection_name or not node_ids:
return jsonify({"status": "FAIL", "reason": "invalid request"})
return jsonify(Attribute.create_selection(db_namespace, selection_name, node_ids))
###########
# Article #
###########
@app.route("/api/<string:namespace>/article", methods=['GET'])
@cross_origin()
def article(namespace):
pubid = request.args.get('pubid')
if pubid:
return jsonify(Article.article_for_pubid(namespace, pubid))
node_id = request.args.get("node_id") or 0
return jsonify(Article.articles_for_node(namespace, node_id))
########
# Edge #
########
@app.route("/api/<string:namespace>/edge", methods=['GET'])
@cross_origin()
def get_edge(namespace):
data = request.args
node_ids = data.get("node_id")
if node_ids:
return jsonify(Edge.for_nodelist(node_ids))
return jsonify(Edge.all(namespace))
##########
# Layout #
##########
@app.route("/api/<string:db_namespace>/layout/<string:layout_namespace>")
@cross_origin()
def get_layout(db_namespace, layout_namespace):
return jsonify(Layout.fetch(db_namespace, layout_namespace))
#########
# Label #
#########
@app.route("/api/<string:db_namespace>/label/<string:label_namespace>")
@cross_origin()
def get_label(db_namespace, label_namespace):
return jsonify(Label.fetch(db_namespace, label_namespace))
#############
# SavedView #
#############
# @app.route("/api/saved_views/<string:username>/<string:view_name>", methods=['GET'])
# @cross_origin()
# def get_saved_view(username, view_name):
# return jsonify(SavedView.get(username, view_name))
#
# @app.route("/api/saved_views/create", methods=['POST'])
# @cross_origin()
# def create_saved_view():
# data = request.form
# return jsonify(SavedView.create(data))
'''
if __name__ == "__main__":
#app.debug = True
app.run()