WebServer.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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, host, port, 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. self.__sio = socketio.Server(async_mode='eventlet')
  66. self.__flask = Flask(__name__)
  67. self.__app = socketio.WSGIApp(self.__sio, self.__flask, static_files=static_files)
  68. # overwrite root path to serve index.html
  69. @self.__flask.route('/', methods=['GET'])
  70. def index():
  71. # pylint: disable=unused-variable
  72. return send_from_directory(path.join(getcwd(), 'webui'), 'index.html')
  73. else:
  74. print('development build')
  75. # create service objects
  76. self.__sio = socketio.Server(cors_allowed_origins='*', async_mode='eventlet')
  77. self.__flask = Flask(__name__)
  78. self.__app = socketio.WSGIApp(self.__sio, self.__flask)
  79. # set access control header to allow requests from Vue.js development server
  80. @self.__flask.after_request
  81. def after_request(response):
  82. # pylint: disable=unused-variable
  83. response.headers['Access-Control-Allow-Origin'] = '*'
  84. return response
  85. # set json encoder so database objects are serialized correctly
  86. self.__flask.json_encoder = JSONEncoder
  87. # create notification manager
  88. notifications = NotificationManager(self.__sio)
  89. jobs.on_create(notifications.create_job)
  90. jobs.on_start(notifications.edit_job)
  91. jobs.on_progress(notifications.edit_job)
  92. jobs.on_finish(notifications.edit_job)
  93. jobs.on_remove(notifications.remove_job)
  94. # jobs
  95. self.__flask.add_url_rule(
  96. '/jobs',
  97. view_func=ListJobs.as_view('list_jobs', jobs)
  98. )
  99. self.__flask.add_url_rule(
  100. '/jobs/<identifier>/remove',
  101. view_func=RemoveJob.as_view('remove_job', jobs)
  102. )
  103. # models
  104. self.__flask.add_url_rule(
  105. '/models',
  106. view_func=ListModels.as_view('list_models', database)
  107. )
  108. self.__flask.add_url_rule(
  109. '/projects/<int:identifier>/model',
  110. view_func=GetProjectModel.as_view('get_project_model', database)
  111. )
  112. # labels
  113. self.__flask.add_url_rule(
  114. '/label_providers',
  115. view_func=ListLabelProviders.as_view('label_providers', database)
  116. )
  117. self.__flask.add_url_rule(
  118. '/projects/<int:identifier>/labels',
  119. view_func=ListLabels.as_view('list_labels', database)
  120. )
  121. self.__flask.add_url_rule(
  122. '/projects/<int:identifier>/labels',
  123. view_func=CreateLabel.as_view('create_label', database, notifications)
  124. )
  125. self.__flask.add_url_rule(
  126. '/projects/<int:project_id>/labels/<int:label_id>/remove',
  127. view_func=RemoveLabel.as_view('remove_label', database, notifications)
  128. )
  129. self.__flask.add_url_rule(
  130. '/projects/<int:project_id>/labels/<int:label_id>/name',
  131. view_func=EditLabelName.as_view('edit_label_name', database, notifications)
  132. )
  133. self.__flask.add_url_rule(
  134. '/projects/<int:project_id>/labels/<int:label_id>/parent',
  135. view_func=EditLabelParent.as_view('edit_label_parent', database, notifications)
  136. )
  137. # collections
  138. self.__flask.add_url_rule(
  139. '/projects/<int:project_id>/collections',
  140. view_func=ListCollections.as_view('list_collections', database)
  141. )
  142. self.__flask.add_url_rule(
  143. '/projects/<int:project_id>/data/<int:collection_id>/<int:start>/<int:length>',
  144. view_func=ListFiles.as_view('list_collection_files', database)
  145. )
  146. # data
  147. self.__flask.add_url_rule(
  148. '/projects/<int:identifier>/data',
  149. view_func=UploadFile.as_view('upload_file', database, notifications)
  150. )
  151. self.__flask.add_url_rule(
  152. '/projects/<int:project_id>/data/<int:start>/<int:length>',
  153. view_func=ListFiles.as_view('list_files', database)
  154. )
  155. self.__flask.add_url_rule(
  156. '/data/<int:identifier>/remove',
  157. view_func=RemoveFile.as_view('remove_file', database, notifications)
  158. )
  159. self.__flask.add_url_rule(
  160. '/data/<int:file_id>',
  161. view_func=GetFile.as_view('get_file', database)
  162. )
  163. self.__flask.add_url_rule(
  164. '/data/<int:file_id>/<resolution>',
  165. view_func=GetResizedFile.as_view('get_resized_file', database)
  166. )
  167. self.__flask.add_url_rule(
  168. '/data/<int:file_id>/previous_next',
  169. view_func=GetPreviousAndNextFile.as_view('get_previous_and_next_file', database)
  170. )
  171. # results
  172. self.__flask.add_url_rule(
  173. '/projects/<int:project_id>/results',
  174. view_func=GetProjectResults.as_view('get_project_results', database)
  175. )
  176. self.__flask.add_url_rule(
  177. '/data/<int:file_id>/results',
  178. view_func=GetResults.as_view('get_results', database)
  179. )
  180. self.__flask.add_url_rule(
  181. '/data/<int:file_id>/results',
  182. view_func=CreateResult.as_view('create_result', database, notifications)
  183. )
  184. self.__flask.add_url_rule(
  185. '/data/<int:file_id>/reset',
  186. view_func=ResetResults.as_view('reset_results', database, notifications)
  187. )
  188. self.__flask.add_url_rule(
  189. '/results/<int:result_id>/remove',
  190. view_func=RemoveResult.as_view('remove_result', database, notifications)
  191. )
  192. self.__flask.add_url_rule(
  193. '/results/<int:result_id>/confirm',
  194. view_func=ConfirmResult.as_view('confirm_result', database, notifications)
  195. )
  196. self.__flask.add_url_rule(
  197. '/results/<int:result_id>/label',
  198. view_func=EditResultLabel.as_view('edit_result_label', database, notifications)
  199. )
  200. self.__flask.add_url_rule(
  201. '/results/<int:result_id>/data',
  202. view_func=EditResultData.as_view('edit_result_data', database, notifications)
  203. )
  204. # projects
  205. self.__flask.add_url_rule(
  206. '/projects',
  207. view_func=ListProjects.as_view('list_projects', database)
  208. )
  209. self.__flask.add_url_rule(
  210. '/projects',
  211. view_func=CreateProject.as_view('create_project', database, notifications, jobs)
  212. )
  213. self.__flask.add_url_rule(
  214. '/projects/<int:identifier>/label_provider',
  215. view_func=ExecuteLabelProvider.as_view('execute_label_provider', database, notifications, jobs)
  216. )
  217. self.__flask.add_url_rule(
  218. '/projects/<int:identifier>/external_storage',
  219. view_func=ExecuteExternalStorage.as_view('execute_external_storage', database, notifications, jobs)
  220. )
  221. self.__flask.add_url_rule(
  222. '/projects/<int:identifier>/remove',
  223. view_func=RemoveProject.as_view('remove_project', database, notifications)
  224. )
  225. self.__flask.add_url_rule(
  226. '/projects/<int:identifier>/name',
  227. view_func=EditProjectName.as_view('edit_project_name', database, notifications)
  228. )
  229. self.__flask.add_url_rule(
  230. '/projects/<int:identifier>/description',
  231. view_func=EditProjectDescription.as_view('edit_project_description', database, notifications)
  232. )
  233. # pipelines
  234. self.__flask.add_url_rule(
  235. '/projects/<int:project_id>/pipelines/fit',
  236. view_func=FitModel.as_view('fit_model', database, jobs)
  237. )
  238. self.__flask.add_url_rule(
  239. '/projects/<int:project_id>/pipelines/predict',
  240. view_func=PredictModel.as_view('predict_model', database, notifications, jobs)
  241. )
  242. self.__flask.add_url_rule(
  243. '/data/<int:file_id>/predict',
  244. view_func=PredictFile.as_view('predict_file', database, notifications, jobs)
  245. )
  246. # finally start web server
  247. eventlet.wsgi.server(eventlet.listen((host, port)), self.__app)