6
0

WebServer.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. from glob import glob
  2. from os import path, getcwd
  3. from os.path import exists
  4. import eventlet
  5. import socketio
  6. from flask import Flask, send_from_directory
  7. from pycs.database.Database import Database
  8. from pycs.frontend.endpoints.ListJobs import ListJobs
  9. from pycs.frontend.endpoints.ListLabelProviders import ListLabelProviders
  10. from pycs.frontend.endpoints.ListModels import ListModels
  11. from pycs.frontend.endpoints.ListProjects import ListProjects
  12. from pycs.frontend.endpoints.additional.FolderInformation import FolderInformation
  13. from pycs.frontend.endpoints.data.GetCroppedFile import GetCroppedFile
  14. from pycs.frontend.endpoints.data.GetFile import GetFile
  15. from pycs.frontend.endpoints.data.GetPreviousAndNextFile import GetPreviousAndNextFile
  16. from pycs.frontend.endpoints.data.GetResizedFile import GetResizedFile
  17. from pycs.frontend.endpoints.data.RemoveFile import RemoveFile
  18. from pycs.frontend.endpoints.data.UploadFile import UploadFile
  19. from pycs.frontend.endpoints.jobs.RemoveJob import RemoveJob
  20. from pycs.frontend.endpoints.labels.CreateLabel import CreateLabel
  21. from pycs.frontend.endpoints.labels.EditLabelName import EditLabelName
  22. from pycs.frontend.endpoints.labels.EditLabelParent import EditLabelParent
  23. from pycs.frontend.endpoints.labels.ListLabelTree import ListLabelTree
  24. from pycs.frontend.endpoints.labels.ListLabels import ListLabels
  25. from pycs.frontend.endpoints.labels.RemoveLabel import RemoveLabel
  26. from pycs.frontend.endpoints.pipelines.FitModel import FitModel
  27. from pycs.frontend.endpoints.pipelines.PredictFile import PredictFile
  28. from pycs.frontend.endpoints.pipelines.PredictModel import PredictModel
  29. from pycs.frontend.endpoints.projects.CreateProject import CreateProject
  30. from pycs.frontend.endpoints.projects.EditProjectDescription import EditProjectDescription
  31. from pycs.frontend.endpoints.projects.EditProjectName import EditProjectName
  32. from pycs.frontend.endpoints.projects.ExecuteExternalStorage import ExecuteExternalStorage
  33. from pycs.frontend.endpoints.projects.ExecuteLabelProvider import ExecuteLabelProvider
  34. from pycs.frontend.endpoints.projects.GetProjectModel import GetProjectModel
  35. from pycs.frontend.endpoints.projects.ListCollections import ListCollections
  36. from pycs.frontend.endpoints.projects.ListFiles import ListFiles
  37. from pycs.frontend.endpoints.projects.RemoveProject import RemoveProject
  38. from pycs.frontend.endpoints.results.ConfirmResult import ConfirmResult
  39. from pycs.frontend.endpoints.results.CreateResult import CreateResult
  40. from pycs.frontend.endpoints.results.EditResultData import EditResultData
  41. from pycs.frontend.endpoints.results.EditResultLabel import EditResultLabel
  42. from pycs.frontend.endpoints.results.GetProjectResults import GetProjectResults
  43. from pycs.frontend.endpoints.results.GetResults import GetResults
  44. from pycs.frontend.endpoints.results.RemoveResult import RemoveResult
  45. from pycs.frontend.endpoints.results.ResetResults import ResetResults
  46. from pycs.frontend.notifications.NotificationManager import NotificationManager
  47. from pycs.frontend.util.JSONEncoder import JSONEncoder
  48. from pycs.jobs.JobRunner import JobRunner
  49. from pycs.util.PipelineCache import PipelineCache
  50. class WebServer:
  51. """
  52. wrapper class for flask and socket.io which initializes most networking
  53. """
  54. # pylint: disable=line-too-long
  55. # pylint: disable=too-many-statements
  56. def __init__(self, settings: dict, database: Database, jobs: JobRunner, pipelines: PipelineCache):
  57. # initialize web server
  58. if exists('webui/index.html'):
  59. print('production build')
  60. # find static files and folders
  61. static_files = {}
  62. for file_path in glob('webui/*'):
  63. file_path = file_path.replace('\\', '/')
  64. static_files[file_path[5:]] = file_path
  65. # separately add svg files and set their correct mime type
  66. for svg_path in glob('webui/img/*.svg'):
  67. svg_path = svg_path.replace('\\', '/')
  68. static_files[svg_path[5:]] = {'content_type': 'image/svg+xml', 'filename': svg_path}
  69. # create service objects
  70. if len(settings['allowedOrigins']) > 0:
  71. origins = settings['allowedOrigins']
  72. self.__sio = socketio.Server(cors_allowed_origins=origins, async_mode='eventlet')
  73. else:
  74. self.__sio = socketio.Server(async_mode='eventlet')
  75. self.__flask = Flask(__name__)
  76. self.__app = socketio.WSGIApp(self.__sio, self.__flask, static_files=static_files)
  77. # overwrite root path to serve index.html
  78. @self.__flask.route('/', methods=['GET'])
  79. def index():
  80. # pylint: disable=unused-variable
  81. return send_from_directory(path.join(getcwd(), 'webui'), 'index.html')
  82. else:
  83. print('development build')
  84. # create service objects
  85. self.__sio = socketio.Server(cors_allowed_origins='*', async_mode='eventlet')
  86. self.__flask = Flask(__name__)
  87. self.__app = socketio.WSGIApp(self.__sio, self.__flask)
  88. # set access control header to allow requests from Vue.js development server
  89. @self.__flask.after_request
  90. def after_request(response):
  91. # pylint: disable=unused-variable
  92. response.headers['Access-Control-Allow-Origin'] = '*'
  93. return response
  94. # set json encoder so database objects are serialized correctly
  95. self.__flask.json_encoder = JSONEncoder
  96. # create notification manager
  97. notifications = NotificationManager(self.__sio)
  98. jobs.on_create(notifications.create_job)
  99. jobs.on_start(notifications.edit_job)
  100. jobs.on_progress(notifications.edit_job)
  101. jobs.on_finish(notifications.edit_job)
  102. jobs.on_remove(notifications.remove_job)
  103. # additional
  104. self.__flask.add_url_rule(
  105. '/folder',
  106. view_func=FolderInformation.as_view('folder_information')
  107. )
  108. # jobs
  109. self.__flask.add_url_rule(
  110. '/jobs',
  111. view_func=ListJobs.as_view('list_jobs', jobs)
  112. )
  113. self.__flask.add_url_rule(
  114. '/jobs/<identifier>/remove',
  115. view_func=RemoveJob.as_view('remove_job', jobs)
  116. )
  117. # models
  118. self.__flask.add_url_rule(
  119. '/models',
  120. view_func=ListModels.as_view('list_models', database)
  121. )
  122. self.__flask.add_url_rule(
  123. '/projects/<int:identifier>/model',
  124. view_func=GetProjectModel.as_view('get_project_model', database)
  125. )
  126. # labels
  127. self.__flask.add_url_rule(
  128. '/label_providers',
  129. view_func=ListLabelProviders.as_view('label_providers', database)
  130. )
  131. self.__flask.add_url_rule(
  132. '/projects/<int:identifier>/labels',
  133. view_func=ListLabels.as_view('list_labels', database)
  134. )
  135. self.__flask.add_url_rule(
  136. '/projects/<int:identifier>/labels/tree',
  137. view_func=ListLabelTree.as_view('list_label_tree', database)
  138. )
  139. self.__flask.add_url_rule(
  140. '/projects/<int:identifier>/labels',
  141. view_func=CreateLabel.as_view('create_label', database, notifications)
  142. )
  143. self.__flask.add_url_rule(
  144. '/projects/<int:project_id>/labels/<int:label_id>/remove',
  145. view_func=RemoveLabel.as_view('remove_label', database, notifications)
  146. )
  147. self.__flask.add_url_rule(
  148. '/projects/<int:project_id>/labels/<int:label_id>/name',
  149. view_func=EditLabelName.as_view('edit_label_name', database, notifications)
  150. )
  151. self.__flask.add_url_rule(
  152. '/projects/<int:project_id>/labels/<int:label_id>/parent',
  153. view_func=EditLabelParent.as_view('edit_label_parent', database, notifications)
  154. )
  155. # collections
  156. self.__flask.add_url_rule(
  157. '/projects/<int:project_id>/collections',
  158. view_func=ListCollections.as_view('list_collections', database)
  159. )
  160. self.__flask.add_url_rule(
  161. '/projects/<int:project_id>/data/<int:collection_id>/<int:start>/<int:length>',
  162. view_func=ListFiles.as_view('list_collection_files', database)
  163. )
  164. # data
  165. self.__flask.add_url_rule(
  166. '/projects/<int:identifier>/data',
  167. view_func=UploadFile.as_view('upload_file', database, notifications)
  168. )
  169. self.__flask.add_url_rule(
  170. '/projects/<int:project_id>/data/<int:start>/<int:length>',
  171. view_func=ListFiles.as_view('list_files', database)
  172. )
  173. self.__flask.add_url_rule(
  174. '/data/<int:identifier>/remove',
  175. view_func=RemoveFile.as_view('remove_file', database, notifications)
  176. )
  177. self.__flask.add_url_rule(
  178. '/data/<int:file_id>',
  179. view_func=GetFile.as_view('get_file', database)
  180. )
  181. self.__flask.add_url_rule(
  182. '/data/<int:file_id>/<resolution>',
  183. view_func=GetResizedFile.as_view('get_resized_file', database)
  184. )
  185. self.__flask.add_url_rule(
  186. '/data/<int:file_id>/<resolution>/<crop_box>',
  187. view_func=GetCroppedFile.as_view('crop_result', database)
  188. )
  189. self.__flask.add_url_rule(
  190. '/data/<int:file_id>/previous_next',
  191. view_func=GetPreviousAndNextFile.as_view('get_previous_and_next_file', database)
  192. )
  193. # results
  194. self.__flask.add_url_rule(
  195. '/projects/<int:project_id>/results',
  196. view_func=GetProjectResults.as_view('get_project_results', database)
  197. )
  198. self.__flask.add_url_rule(
  199. '/data/<int:file_id>/results',
  200. view_func=GetResults.as_view('get_results', database)
  201. )
  202. self.__flask.add_url_rule(
  203. '/data/<int:file_id>/results',
  204. view_func=CreateResult.as_view('create_result', database, notifications)
  205. )
  206. self.__flask.add_url_rule(
  207. '/data/<int:file_id>/reset',
  208. view_func=ResetResults.as_view('reset_results', database, notifications)
  209. )
  210. self.__flask.add_url_rule(
  211. '/results/<int:result_id>/remove',
  212. view_func=RemoveResult.as_view('remove_result', database, notifications)
  213. )
  214. self.__flask.add_url_rule(
  215. '/results/<int:result_id>/confirm',
  216. view_func=ConfirmResult.as_view('confirm_result', database, notifications)
  217. )
  218. self.__flask.add_url_rule(
  219. '/results/<int:result_id>/label',
  220. view_func=EditResultLabel.as_view('edit_result_label', database, notifications)
  221. )
  222. self.__flask.add_url_rule(
  223. '/results/<int:result_id>/data',
  224. view_func=EditResultData.as_view('edit_result_data', database, notifications)
  225. )
  226. # projects
  227. self.__flask.add_url_rule(
  228. '/projects',
  229. view_func=ListProjects.as_view('list_projects', database)
  230. )
  231. self.__flask.add_url_rule(
  232. '/projects',
  233. view_func=CreateProject.as_view('create_project', database, notifications, jobs)
  234. )
  235. self.__flask.add_url_rule(
  236. '/projects/<int:identifier>/label_provider',
  237. view_func=ExecuteLabelProvider.as_view('execute_label_provider', database,
  238. notifications, jobs)
  239. )
  240. self.__flask.add_url_rule(
  241. '/projects/<int:identifier>/external_storage',
  242. view_func=ExecuteExternalStorage.as_view('execute_external_storage', database,
  243. notifications, jobs)
  244. )
  245. self.__flask.add_url_rule(
  246. '/projects/<int:identifier>/remove',
  247. view_func=RemoveProject.as_view('remove_project', database, notifications)
  248. )
  249. self.__flask.add_url_rule(
  250. '/projects/<int:identifier>/name',
  251. view_func=EditProjectName.as_view('edit_project_name', database, notifications)
  252. )
  253. self.__flask.add_url_rule(
  254. '/projects/<int:identifier>/description',
  255. view_func=EditProjectDescription.as_view('edit_project_description', database,
  256. notifications)
  257. )
  258. # pipelines
  259. self.__flask.add_url_rule(
  260. '/projects/<int:project_id>/pipelines/fit',
  261. view_func=FitModel.as_view('fit_model', database, jobs, pipelines)
  262. )
  263. self.__flask.add_url_rule(
  264. '/projects/<int:project_id>/pipelines/predict',
  265. view_func=PredictModel.as_view('predict_model', database, notifications, jobs,
  266. pipelines)
  267. )
  268. self.__flask.add_url_rule(
  269. '/data/<int:file_id>/predict',
  270. view_func=PredictFile.as_view('predict_file', database, notifications, jobs, pipelines)
  271. )
  272. # finally start web server
  273. eventlet.wsgi.server(eventlet.listen((settings['host'], settings['port'])), self.__app)