main.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. """
  2. An example demonstrating a stand-alone "filebrowser".
  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. """Get the main page for the application's interface."""
  28. config_data = {
  29. # Use camelCase here, since that's what the lab components expect
  30. "appVersion": version,
  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. default_url = "/example"
  48. app_url = "/example"
  49. load_other_extensions = False
  50. name = __name__
  51. app_name = "JupyterLab Example File Browser"
  52. static_dir = os.path.join(HERE, "build")
  53. templates_dir = os.path.join(HERE, "templates")
  54. app_version = version
  55. app_settings_dir = os.path.join(HERE, "build", "application_settings")
  56. schemas_dir = os.path.join(HERE, "build", "schemas")
  57. themes_dir = os.path.join(HERE, "build", "themes")
  58. user_settings_dir = os.path.join(HERE, "build", "user_settings")
  59. workspaces_dir = os.path.join(HERE, "build", "workspaces")
  60. def initialize_handlers(self):
  61. """Add example handler to Lab Server's handler list."""
  62. super().initialize_handlers()
  63. self.handlers.append(("/example", ExampleHandler))
  64. if __name__ == "__main__":
  65. ExampleApp.launch_instance()