forked from SUNCAT-Center/CatalysisHubBackend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·185 lines (145 loc) · 5.32 KB
/
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
#!/usr/bin/env python
# global imports
import numpy as np
import os
import json
import flask
import flask_graphql
import flask_sqlalchemy
from flask_cors import CORS
import logging
from raven.contrib.flask import Sentry
# local imports
import models
import api
import traceback
from sqlalchemy.exc import OperationalError
try:
from apps.pourbaix.run_pourbaix import pourbaix
except ImportError:
print('pourbaix diagrams not available.')
traceback.print_exc()
pourbaix = None
try:
from apps.catlearn.run_catlearn import catlearn_blueprint
except ImportError as e:
print('Catlearn not available: {e}'.format(e=e))
traceback.print_exc()
catlearn_blueprint = None
try:
from apps.activityMaps import activityMaps
except ImportError as e:
print('activityMaps not available: {e}'.format(e=e))
traceback.print_exc()
activityMaps = None
try:
from apps.prototypeSearch import app as prototypeSearch
except (ImportError, OperationalError) as e:
print('prototypeSearch not available: {e}'.format(e=e))
traceback.print_exc()
prototypeSearch = None
try:
from apps.bulkEnumerator import bulk_enumerator
except ImportError as e:
('prototypeSearch not available: {e}'.format(e=e))
print('prototypeSearch not available: {e}'.format(e=e))
traceback.print_exc()
bulk_enumerator = None
try:
from apps.catKitDemo import catKitDemo
except ImportError as e:
print('catKitDemo not available: {e}'.format(e=e))
traceback.print_exc()
catKitDemo = None
try:
from apps.upload import upload
except ImportError as e:
print('upload not available: {e}'.format(e=e))
traceback.print_exc()
upload = None
# NumpyEncoder: useful for JSON serializing
# Dictionaries that contain Numpy Arrays
class NumpyEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.floating):
return float(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
else:
return super(NumpyEncoder, self).default(obj)
app = flask.Flask(__name__)
if os.environ.get('DB_PASSWORD', ''):
app.config.update({
# 'SESSION_COOKIE_SECURE': True,
'CORS_SUPPORTS_CREDENTIALS': True,
'CORS_HEADERS': 'Content-Type, X-Pingother',
'SQLALCHEMY_DATABASE_URI': 'postgres://catvisitor:{}@catalysishub.c8gwuc8jwb7l.us-west-2.rds.amazonaws.com:5432/catalysishub'.format(os.environ["DB_PASSWORD"]), })
else:
# for Travis CI
app.config.update({
'CORS_SUPPORTS_CREDENTIALS': True,
'SQLALCHEMY_DATABASE_URI': 'postgres://postgres@localhost:5432/travis_ci_test', })
db = flask_sqlalchemy.SQLAlchemy(app)
app.debug = False
if not app.debug:
sentry = Sentry(app, logging=True, level=logging.WARNING)
app.json_encoder = NumpyEncoder
cors = CORS(app,
supports_credentials=True)
@app.after_request
def after_request(response):
response.headers.add('Access-Control-Allow-Headers',
'X-PINGOTHER, Content-Type, Authorization, Accept')
response.headers.add('Access-Control-Allow-Methods',
'GET, PUT, POST, DELETE, HEAD, OPTIONS')
return response
@app.route('/')
def index():
return flask.redirect(
"/graphql?query=%7B%0A%20%20reactions(first%3A%2010)%20%7B%0A%20%20%20%20edges%20%7B%0A%20%20%20%20%20%20node%20%7B%0A%20%20%20%20%20%20%20%20Equation%0A%20%20%20%20%20%20%20%20chemicalComposition%0A%20%20%20%20%20%20%20%20reactionEnergy%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D%0A",
code=302)
@app.route('/apps/')
def apps():
return "Apps: catlearn, pourbaix"
if bulk_enumerator is not None:
app.register_blueprint(bulk_enumerator, url_prefix='/apps/bulkEnumerator')
if catKitDemo is not None:
app.register_blueprint(catKitDemo, url_prefix='/apps/catKitDemo')
# Graphql view
app.add_url_rule('/graphql',
view_func=flask_graphql.GraphQLView.as_view(
'graphql',
schema=api.schema,
graphiql=True,
get_context=lambda: {'session': db.session}
))
if pourbaix is not None:
app.register_blueprint(pourbaix, url_prefix='/apps/pourbaix')
if activityMaps is not None:
app.register_blueprint(activityMaps, url_prefix='/apps/activityMaps')
if prototypeSearch is not None:
app.register_blueprint(prototypeSearch, url_prefix='/apps/prototypeSearch')
if catlearn_blueprint is not None:
app.register_blueprint(catlearn_blueprint, url_prefix='/apps/catlearn')
if upload is not None:
app.register_blueprint(upload, url_prefix='/apps/upload')
# Needed to set session cookies.
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
app.secret_key = os.environ.get('FLASK_SECRET_KEY', '')
if __name__ == '__main__':
import optparse
parser = optparse.OptionParser()
parser.add_option('-s',
'--debug-sql',
help="Print executed SQL statement to commandline",
dest="debug_sql",
action="store_true",
default=False)
options, args = parser.parse_args()
if options.debug_sql:
import logging
logging.basicConfig()
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
app.run()