main.py 2.6 KB

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