main.py 2.4 KB

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