1
1

WebServer.py 13 KB

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