main.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. """
  2. An example demonstrating a stand-alone "console".
  3. Copyright (c) Jupyter Development Team.
  4. Distributed under the terms of the Modified BSD License.
  5. Example
  6. -------
  7. To run the example, see the instructions in the README to build it. Then
  8. run ``python main.py``.
  9. """
  10. import json
  11. import os
  12. from jupyter_server.base.handlers import JupyterHandler
  13. from jupyter_server.extension.handler import (
  14. ExtensionHandlerJinjaMixin,
  15. ExtensionHandlerMixin,
  16. )
  17. from jupyter_server.utils import url_path_join as ujoin
  18. from jupyterlab_server import LabServerApp
  19. HERE = os.path.dirname(__file__)
  20. with open(os.path.join(HERE, "package.json")) as fid:
  21. version = json.load(fid)["version"]
  22. def _jupyter_server_extension_points():
  23. return [{"module": __name__, "app": ExampleApp}]
  24. class ExampleHandler(ExtensionHandlerJinjaMixin, ExtensionHandlerMixin, JupyterHandler):
  25. """Handle requests between the main app page and notebook server."""
  26. def get(self):
  27. config_data = {
  28. # Use camelCase here, since that's what the lab components expect
  29. "appVersion": version,
  30. "baseUrl": self.base_url,
  31. "token": self.settings["token"],
  32. "fullStaticUrl": ujoin(self.base_url, "static", self.name),
  33. "frontendUrl": ujoin(self.base_url, "example/"),
  34. }
  35. return self.write(
  36. self.render_template(
  37. "index.html",
  38. static=self.static_url,
  39. base_url=self.base_url,
  40. token=self.settings["token"],
  41. page_config=config_data,
  42. )
  43. )
  44. class ExampleApp(LabServerApp):
  45. extension_url = "/example"
  46. default_url = "/example"
  47. app_url = "/example"
  48. load_other_extensions = False
  49. name = __name__
  50. app_name = "JupyterLab Example Console"
  51. app_settings_dir = os.path.join(HERE, "build", "application_settings")
  52. schemas_dir = os.path.join(HERE, "build", "schemas")
  53. static_dir = os.path.join(HERE, "build")
  54. templates_dir = os.path.join(HERE, "templates")
  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. """Initialize JupyterLab handlers."""
  60. super().initialize_handlers()
  61. self.handlers.append(("/example", ExampleHandler))
  62. if __name__ == "__main__":
  63. ExampleApp.launch_instance()