WebServer.py 12 KB

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