WebServer.py 12 KB

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