main.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. def _jupyter_server_extension_points():
  15. return [
  16. {
  17. 'module': __name__,
  18. 'app': ExampleApp
  19. }
  20. ]
  21. class ExampleHandler(
  22. ExtensionHandlerJinjaMixin,
  23. ExtensionHandlerMixin,
  24. JupyterHandler
  25. ):
  26. """Handle requests between the main app page and notebook server."""
  27. def get(self):
  28. """Get the main page for the application's interface."""
  29. config_data = {
  30. # Use camelCase here, since that's what the lab components expect
  31. 'baseUrl': self.base_url,
  32. 'token': self.settings['token'],
  33. 'fullStaticUrl': ujoin(self.base_url, 'static', self.name),
  34. 'frontendUrl': ujoin(self.base_url, 'example/'),
  35. }
  36. return self.write(
  37. self.render_template(
  38. 'index.html',
  39. static=self.static_url,
  40. base_url=self.base_url,
  41. token=self.settings['token'],
  42. page_config=config_data
  43. )
  44. )
  45. class ExampleApp(LabServerApp):
  46. extension_url = '/example'
  47. app_url = "/example"
  48. name = __name__
  49. load_other_extensions = False
  50. app_name = 'JupyterLab Example Service'
  51. static_dir = os.path.join(HERE, 'static')
  52. templates_dir = os.path.join(HERE)
  53. app_settings_dir = os.path.join(HERE, 'build', 'application_settings')
  54. schemas_dir = os.path.join(HERE, 'build', 'schemas')
  55. themes_dir = os.path.join(HERE, 'build', 'themes')
  56. user_settings_dir = os.path.join(HERE, 'build', 'user_settings')
  57. workspaces_dir = os.path.join(HERE, 'build', 'workspaces')
  58. def initialize_handlers(self):
  59. """Add example handler to Lab Server's handler list.
  60. """
  61. self.handlers.append(
  62. ('/example', ExampleHandler)
  63. )
  64. if __name__ == '__main__':
  65. ExampleApp.launch_instance()