-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp.py
More file actions
93 lines (73 loc) · 3.59 KB
/
app.py
File metadata and controls
93 lines (73 loc) · 3.59 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
from flask import Flask, request, abort
import os
import sys
import traceback
import requests
app = Flask(__name__)
@app.route("/status")
def status():
return("The Visualisation Test Plugin Flask Server is up and running")
@app.route("/evaluate", methods=["POST"])
def evaluate():
data = request.get_json(force=True)
rdf_type = data['type']
# ~~~~~~~~~~~~ REPLACE THIS SECTION WITH OWN RUN CODE ~~~~~~~~~~~~~~~~~~~
# uses rdf types
accepted_types = {'Activity', 'Agent', 'Association', 'Attachment',
'Collection', 'CombinatorialDerivation', 'Component',
'ComponentDefinition', 'Cut', 'Experiment',
'ExperimentalData', 'FunctionalComponent',
'GenericLocation', 'Implementation', 'Interaction',
'Location', 'MapsTo', 'Measure', 'Model', 'Module',
'ModuleDefinition', 'Participation', 'Plan', 'Range',
'Sequence', 'SequenceAnnotation', 'SequenceConstraint',
'Usage', 'VariableComponent'}
acceptable = rdf_type in accepted_types
# # to ensure it shows up on all pages
# acceptable = True
# ~~~~~~~~~~~~~~~~~~~~~~~~~~ END SECTION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if acceptable:
return f'The type sent ({rdf_type}) is an accepted type', 200
else:
return f'The type sent ({rdf_type}) is NOT an accepted type', 415
@app.route("/run", methods=["POST"])
def run():
data = request.get_json(force=True)
top_level_url = data['top_level']
complete_sbol = data['complete_sbol']
instance_url = data['instanceUrl']
size = data['size']
rdf_type = data['type']
shallow_sbol = data['shallow_sbol']
token = data['token']
if token is None:
token = 'null'
url = complete_sbol.replace('/sbol', '')
cwd = os.getcwd()
filename = os.path.join(cwd, "Test.html")
try:
# ~~~~~~~~~~~~ REPLACE THIS SECTION WITH OWN RUN CODE ~~~~~~~~~~~~~~~~~~~
with open(filename, 'r') as htmlfile:
result = htmlfile.read()
if token != 'null':
response = requests.get(url, headers={'Accept': 'text/plain', 'X-authorization': token })
else:
response = requests.get(url, headers={'Accept': 'text/plain' })
# put in the url, uri, and instance given by synbiohub
result = result.replace("URL_REPLACE", url)
result = result.replace("URI_REPLACE", top_level_url)
result = result.replace("TOKEN_REPLACE", token)
result = result.replace("INSTANCE_REPLACE", instance_url)
result = result.replace("SIZE_REPLACE", str(size))
result = result.replace("RDFTYPE_REPLACE", rdf_type)
result = result.replace("SHALLOWSBOL_REPLACE", shallow_sbol)
result = result.replace("RESPONSE_CODE_REPLACE", str(response.status_code))
result = result.replace("REQUEST_REPLACE", str(data))
# ~~~~~~~~~~~~~~~~~~~~~~~~~~ END SECTION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
return result
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
lnum = exc_tb.tb_lineno
print(f'Exception is: {e}, exc_type: {exc_type}, exc_obj: {exc_obj}, fname: {fname}, line_number: {lnum}, traceback: {traceback.format_exc()}', flush=True)
abort(400, f'Exception is: {e}, exc_type: {exc_type}, exc_obj: {exc_obj}, fname: {fname}, line_number: {lnum}, traceback: {traceback.format_exc()}')