forked from aidrd/nanbyodata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
246 lines (210 loc) · 8.39 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
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
# -*- coding: utf-8 -*-
from flask import Flask, session, render_template, request, redirect, url_for, jsonify, make_response, send_from_directory
import os
import re
import MySQLdb
import json
import sys
import datetime
import copy
import pronto
#import mojimoji
#from werkzeug import secure_filename
from io import StringIO, BytesIO
import csv
# https://blog.capilano-fw.com/?p=398
from flask_babel import gettext,Babel
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
app.secret_key = 'nanbyodata0824'
# https://github.com/shibacow/flask_babel_sample/blob/master/srv.py
babel = Babel(app)
@babel.localeselector
def get_locale():
if 'lang' not in session:
session['lang'] = request.accept_languages.best_match(['ja', 'ja_JP', 'en'])
if request.args.get('lang'):
session['lang'] = request.args.get('lang')
return session.get('lang', 'en')
app.jinja_env.globals.update(get_locale=get_locale)
# debug
app.debug = True
#####
# DB設定
app.config.from_pyfile('config.cfg')
db_sock = app.config['DBSOCK']
db_name = app.config['DBNAME']
db_user = app.config['DBUSER']
db_pw = app.config['DBPW']
#####
# Routing
# http://qiita.com/Morinikki/items/c2af4ffa180856d1bf30
# http://flask.pocoo.org/docs/0.12/quickstart/
#####
#####
# index page
## GET: display top page
@app.route('/')
def index():
return render_template('index.html')
#####
# NANDOについて
## GET:
@app.route('/about_nando')
def about_nando():
return render_template('about_nando.html')
#####
# NANDOについて
## GET:
@app.route('/epidemiology')
def epidemiology():
return render_template('epidemiology.html')
#####
# NANDO語彙一覧
## GET:
@app.route('/ontology/nando')
def nando(id_nando=""):
return render_template('nando.html')
#####
# NANDO URI 転送
## GET:
#@app.route('/ontology/NANDO_<string:id_nando>')
#def nando_uri(id_nando=""):
# return redirect(url_for('REST_API_disease', id_nando=id_nando))
#####
# ontology files ダウンロード
## GET:
@app.route('/ontology/<path:filename>')
def nando_file(filename):
# https://www.kite.com/python/docs/flask.send_from_directory
return send_from_directory('ontology',
filename, as_attachment=True)
#####
# annotation files ダウンロード
## GET:
@app.route('/annotation/<path:filename>')
def annotation_file(filename):
# https://www.kite.com/python/docs/flask.send_from_directory
return send_from_directory('annotation',
filename, as_attachment=True)
#####
# 疾患ページ
## GET:
@app.route('/disease/NANDO:<string:id_nando>', methods=['GET'])
def REST_API_disease(id_nando=""):
# if request.method == 'GET':
# # parse nando.ttl
# onto = pronto.Ontology('/opt/services/case/app/nanbyodata/ontology/nando.obo')
# breadcrumb_list_html = ""
# sup = onto[id_nando].superclasses()
# pre_term = next(sup)
# subclass_list_html = make_selector_subclasses(onto, pre_term.id, "")
# breadcrumb_list_html = ' (' + subclass_list_html + ') ' if subclass_list_html != "" else ""
# while True:
# try:
# term = next(sup)
# subclass_list_html = make_selector_subclasses(onto, term.id, pre_term.id)
# breadcrumb_list_html = ' > ' + subclass_list_html + breadcrumb_list_html
# pre_term = term
# except StopIteration:
# break
# breadcrumb_list_html = '<font size="2">難病</font>' + breadcrumb_list_html
# return render_template('disease.html',
# id_nando=id_nando,
# breadcrumb_list_html=breadcrumb_list_html
# )
# else:
# return render_template('index.html')
# return
if request.method == 'GET':
# parse nando.ttl
onto = pronto.Ontology('/opt/services/case/app/nanbyodata/ontology/nando.obo')
breadcrumb_list_html = ""
sup = onto[id_nando].superclasses()
pre_term = next(sup)
subclass_list_html = make_selector_subclasses_new(onto, pre_term.id, "")
breadcrumb_list_html = '<ul><li>' + subclass_list_html + '</li></ul>' if subclass_list_html != "" else ""
while True:
try:
term = next(sup)
subclass_list_html = make_selector_subclasses_new(onto, term.id, pre_term.id)
breadcrumb_list_html = '<ul><li>' + subclass_list_html + breadcrumb_list_html + '</li></ul>'
pre_term = term
except StopIteration:
break
breadcrumb_list_html = re.sub(r'^\<ul\>', '<ul class="tree">', breadcrumb_list_html)
breadcrumb_list_html = '<h3>難病</h3>' + breadcrumb_list_html
return render_template('disease.html',
id_nando=id_nando,
breadcrumb_list_html=breadcrumb_list_html
)
else:
return render_template('index.html')
return
#####
# 疾患ページ new
## GET:
@app.route('/disease_old/NANDO:<string:id_nando>', methods=['GET'])
def REST_API_disease_old(id_nando=""):
if request.method == 'GET':
# parse nando.ttl
onto = pronto.Ontology('/opt/services/case/app/nanbyodata/ontology/nando.obo')
breadcrumb_list_html = ""
sup = onto[id_nando].superclasses()
pre_term = next(sup)
subclass_list_html = make_selector_subclasses_new(onto, pre_term.id, "")
breadcrumb_list_html = '<ul><li>' + subclass_list_html + '</li></ul>' if subclass_list_html != "" else ""
while True:
try:
term = next(sup)
subclass_list_html = make_selector_subclasses_new(onto, term.id, pre_term.id)
breadcrumb_list_html = '<ul><li>' + subclass_list_html + breadcrumb_list_html + '</li></ul>'
pre_term = term
except StopIteration:
break
breadcrumb_list_html = re.sub(r'^\<ul\>', '<ul class="tree">', breadcrumb_list_html)
breadcrumb_list_html = '<h3>難病</h3>' + breadcrumb_list_html
return render_template('disease.html.20230823',
id_nando=id_nando,
breadcrumb_list_html=breadcrumb_list_html
)
else:
return render_template('index.html')
return
def make_selector_subclasses(onto, id_nando, pre_id_nando):
html_selector = ""
flag_subclass = 0
sub = onto[id_nando].subclasses(with_self=False, distance=1)
html_selector = '<select name="' + pre_id_nando + '" style="width:150px;" onChange="location.href=value;"><br>'
html_selector = html_selector + '<option>下位疾患</option><br>' if pre_id_nando == "" else html_selector
while True:
try:
term = next(sub)
html_selected = " selected" if term.id == pre_id_nando else ""
html_selector = html_selector + '<option value="https://nanbyodata.jp/disease/NANDO:' + term.id + '"' + html_selected + '>' + term.name + '</option><br>'
#html_selector = html_selector + '<option value="https://nanbyodata.jp/disease/' + term.id + '"' + html_selected + '>' + term.name + '</option><br>'
flag_subclass = 1
except StopIteration:
break
html_selector = html_selector + '</select>'
html_selector = html_selector if flag_subclass == 1 else ""
return html_selector
def make_selector_subclasses_new(onto, id_nando, pre_id_nando):
html_selector = ""
flag_subclass = 0
sub = onto[id_nando].subclasses(with_self=False, distance=1)
html_selector = '<div class="cp_ipselect cp_sl03"><select name="' + pre_id_nando + '" style="width:150px;" onChange="location.href=value;"><br>'
html_selector = html_selector + '<option>下位疾患</option><br>' if pre_id_nando == "" else html_selector
while True:
try:
term = next(sub)
html_selected = " selected" if term.id == pre_id_nando else ""
html_selector = html_selector + '<option value="https://nanbyodata.jp/disease/NANDO:' + term.id + '"' + html_selected + '>' + term.name + '</option><br>'
#html_selector = html_selector + '<option value="https://nanbyodata.jp/disease/' + term.id + '"' + html_selected + '>' + term.name + '</option><br>'
flag_subclass = 1
except StopIteration:
break
html_selector = html_selector + '</select></div>'
html_selector = html_selector if flag_subclass == 1 else ""
return html_selector