-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
109 lines (90 loc) · 3.89 KB
/
app.py
File metadata and controls
109 lines (90 loc) · 3.89 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from flask import Flask, request, abort, jsonify
import os, flask
import requests
app = Flask(__name__)
@app.route("/status")
def status():
return("The Server Test Plugin Flask Server is up and running")
@app.route("/evaluate", methods=["POST", "GET"])
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:
hostAddr = request.headers.get('host')
# determine SEQVIZ script URL (respect SEQVIZ_HOST env override)
override_host = os.environ.get('SERVER_HOST')
if override_host:
if override_host.startswith('http://') or override_host.startswith('https://'):
hostAddr = override_host.rstrip('/')
else:
hostAddr = 'http://' + override_host.rstrip('/')
else:
hostAddr = f'http://{hostAddr}'
if token != 'null':
response = requests.get(url, headers={'Accept': 'text/plain', 'X-authorization': token })
else:
response = requests.get(url, headers={'Accept': 'text/plain' })
# this works if you can access the plugin via an exposed port on the internet.
# Note that for synbiohub it must be https
# <img src="http://${hostAddr}/public/success.jpg" alt="Success">
html_file = f"""<!doctype html>
<html>
<head><title>sequence view</title>
<script src="{hostAddr + '/seqviz.js'}"></script></head>
<body>
<p>Response from {url}: {response.status_code}</p>
<div id="reactele"></div>
<img src="{hostAddr + '/public/success.jpg'}" alt="Success">
<p>Host address: {hostAddr}</p>
</body>
</html>
"""
return html_file
except Exception as e:
print(e, flush=True)
abort(400)
@app.route("/public/<file_name>")
def success(file_name):
cwd = os.getcwd()
path = os.path.join(cwd,'public')
try:
return flask.send_from_directory(path,file_name)
except:
with open(os.path.join(cwd,"Static_File_Not_Found.html")) as file:
error_message = file.read()
error_message = error_message.replace('REPLACE_FILENAME',file_name)
return error_message, 404