forked from sungsooha/multisciview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
190 lines (155 loc) · 5.45 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
"""
Web Server for MultiSciView
NOTES:
1.
author: Sungsoo Ha ([email protected])
"""
import json
from flask import Flask, Response, request, render_template
from model.dataModel import DataHandler
from config import CONFIG
# ----------------------------------------------------------------------------
# Global variables
# ----------------------------------------------------------------------------
# web application
app = Flask(__name__)
# data handler including
# - managing MongoDB
# - sync files in folders user selected with MongoDB
# - monitoring folders, updating some events, and broadcasting the results
Data = DataHandler(
rootDir=CONFIG['DATA_DIR'],
fsMapFn=CONFIG['FSMAP'],
db_host=CONFIG['DB']['HOST'],
db_port=CONFIG['DB']['PORT'],
xml_config=CONFIG['XML']
)
# ----------------------------------------------------------------------------
# db route
# ----------------------------------------------------------------------------
@app.route('/api/db/fsmap', methods=['POST'])
def db_treemap():
"""
This is to communicate file system information.
User will use this route to
1. get the latest file system information
2. update db field in the file system information
In case, multiple clients attempt to update the db field for the same
folder, ther server will take the first attempt and ignore the others.
Server will always return the latest file system information with any
updates from the clients. This also includes any changes in the file
system itself, e.g. creating new folders or deleteing some folders. For
this, server will do following operations:
1. update fsmap with information a client provided, if any
2. scan file system and update fsmap
Note that thses operations will be serialized with Lock.
Returns:
updated fsmap
"""
nodeList = request.get_json()['nodeList']
if len(nodeList):
Data.set_fsmap(nodeList)
return json.dumps(Data.get_fsmap_as_list())
# ----------------------------------------------------------------------------
# syncer route
# ----------------------------------------------------------------------------
@app.route('/api/syncer/init', methods=['POST'])
def syncer_init():
"""
This route is invoked when new directory is selected by clients.
Returns:
The server returns sync information related to the selected folder.
"""
data = request.get_json()
wdir = data['wdir']
recursive = data['recursive']
return json.dumps(Data.get_sync_info(wdir, recursive))
@app.route('/api/syncer/start', methods=['POST'])
def syncer_start():
info = request.get_json()
info = Data.update_sync_info(info)
return json.dumps(info)
@app.route('/api/syncer/progress', methods=['POST'])
def syncer_progress():
info = request.get_json()
info = Data.update_sync_info(info)
return json.dumps(info)
# ----------------------------------------------------------------------------
# stream
# ----------------------------------------------------------------------------
def gen(dataframe):
try:
while True:
frame = dataframe.get_frame()
yield "data: %s\n\n" % frame
except GeneratorExit:
pass
@app.route('/stream')
def stream():
return Response(
gen(Data.get_dataframe()),
mimetype='text/event-stream',
headers={'Cache-Control': 'no-cache'}
)
# ----------------------------------------------------------------------------
# Data
# ----------------------------------------------------------------------------
@app.route('/api/data/samplelist', methods=['POST'])
def get_db_samplelist():
data = request.get_json()
path = data['path']
recursive = data['recursive']
return json.dumps(Data.get_samplelist(path, recursive))
@app.route('/api/data/sample', methods=['POST'])
def get_sample():
data = request.get_json()
sampleNames = data['sampleNames']
path = data['path']
recursive = data['recursive']
data = Data.get_samples(sampleNames, path, recursive)
# for key, value in data.items():
# print(key, len(value), value[0])
return json.dumps({
'sampleList': sampleNames,
'sampleData': data
})
@app.route('/api/data/tiff', methods=['POST'])
def get_tiff():
data = request.get_json()
id = data['id']
path = data['path']
return json.dumps(Data.get_tiff(id, path))
# ----------------------------------------------------------------------------
# main
# ----------------------------------------------------------------------------
@app.route('/')
def start():
"""
Render index page
"""
return render_template('index.html')
def finalize():
"""
Finalize web server before exiting the program
"""
pass
def main(host, port):
try:
app.run(host=host, port=port, threaded=True)
except KeyboardInterrupt:
pass
finally:
finalize()
if __name__ == '__main__':
import argparse
argparser = argparse.ArgumentParser(description="MultiSciView")
argparser.add_argument("-s", "--serverhost",
type=str,
default='0.0.0.0',
help="Web server host address")
argparser.add_argument("-p", "--serverport",
type=int,
default=8001,
help="Web server port number")
args = argparser.parse_args()
main(host=args.serverhost, port=args.serverport)