main.py 2.2 KB

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