main.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. """
  2. Copyright (c) Jupyter Development Team.
  3. Distributed under the terms of the Modified BSD License.
  4. """
  5. import os
  6. import json
  7. import os.path as osp
  8. from jupyter_server.base.handlers import JupyterHandler, FileFindHandler
  9. from jupyter_server.extension.handler import ExtensionHandlerMixin, ExtensionHandlerJinjaMixin
  10. from jupyterlab_server import LabServerApp, LabConfig
  11. from jupyter_server.utils import url_path_join as ujoin
  12. from traitlets import Unicode
  13. HERE = osp.dirname(__file__)
  14. with open(os.path.join(HERE, 'package.json')) as fid:
  15. version = json.load(fid)['version']
  16. def _jupyter_server_extension_paths():
  17. return [
  18. {
  19. 'module': 'main',
  20. 'app': ExampleApp
  21. }
  22. ]
  23. class ExampleHandler(
  24. ExtensionHandlerJinjaMixin,
  25. ExtensionHandlerMixin,
  26. JupyterHandler
  27. ):
  28. """Handle requests between the main app page and notebook server."""
  29. def get(self):
  30. """Get the main page for the application's interface."""
  31. config_data = {
  32. # Use camelCase here, since that's what the lab components expect
  33. "appVersion": version,
  34. 'baseUrl': self.base_url,
  35. 'token': self.settings['token'],
  36. 'fullStaticUrl': ujoin(self.base_url, 'static', 'main'),
  37. 'frontendUrl': ujoin(self.base_url, 'example/'),
  38. }
  39. return self.write(
  40. self.render_template(
  41. 'index.html',
  42. static=self.static_url,
  43. base_url=self.base_url,
  44. token=self.settings['token'],
  45. page_config=config_data
  46. )
  47. )
  48. class ExampleApp(LabServerApp):
  49. extension_url = '/lab'
  50. extension_name = 'main'
  51. app_name = 'JupyterLab Example Service'
  52. app_url = '/example_app'
  53. static_dir = os.path.join(HERE, 'build')
  54. templates_dir = os.path.join(HERE, 'templates')
  55. app_version = version
  56. app_settings_dir = os.path.join(HERE, 'build', 'application_settings')
  57. schemas_dir = os.path.join(HERE, 'build', 'schemas')
  58. themes_dir = os.path.join(HERE, 'build', 'themes')
  59. user_settings_dir = os.path.join(HERE, 'build', 'user_settings')
  60. workspaces_dir = os.path.join(HERE, 'build', 'workspaces')
  61. def initialize_handlers(self):
  62. """Add example handler to Lab Server's handler list.
  63. """
  64. self.handlers.append(
  65. ('/example', ExampleHandler)
  66. )
  67. if __name__ == '__main__':
  68. ExampleApp.launch_instance()