WebServer.py 11 KB

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