6
0

WebServer.py 11 KB

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