main.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 jinja2 import FileSystemLoader
  13. from jupyter_server.base.handlers import JupyterHandler, FileFindHandler
  14. from jupyter_server.extension.handler import ExtensionHandlerMixin, ExtensionHandlerJinjaMixin
  15. from jupyterlab_server import LabServerApp, LabConfig
  16. from jupyter_server.utils import url_path_join as ujoin
  17. from traitlets import Unicode
  18. HERE = os.path.dirname(__file__)
  19. with open(os.path.join(HERE, 'package.json')) as fid:
  20. version = json.load(fid)['version']
  21. def _jupyter_server_extension_paths():
  22. return [
  23. {
  24. 'module': __name__,
  25. 'app': ExampleApp
  26. }
  27. ]
  28. class ExampleHandler(
  29. ExtensionHandlerJinjaMixin,
  30. ExtensionHandlerMixin,
  31. JupyterHandler
  32. ):
  33. """Handle requests between the main app page and notebook server."""
  34. def get(self):
  35. """Get the main page for the application's interface."""
  36. config_data = {
  37. # Use camelCase here, since that's what the lab components expect
  38. "appVersion": version,
  39. 'baseUrl': self.base_url,
  40. 'token': self.settings['token'],
  41. 'fullStaticUrl': ujoin(self.base_url, 'static', self.name),
  42. 'frontendUrl': ujoin(self.base_url, 'example/'),
  43. }
  44. return self.write(
  45. self.render_template(
  46. 'index.html',
  47. static=self.static_url,
  48. base_url=self.base_url,
  49. token=self.settings['token'],
  50. page_config=config_data
  51. )
  52. )
  53. class ExampleApp(LabServerApp):
  54. extension_url = '/example'
  55. name = "main"
  56. app_name = 'JupyterLab Example File Browser'
  57. app_url = '/example_app'
  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. self.handlers.append(('example', ExampleHandler))
  70. if __name__ == '__main__':
  71. ExampleApp.launch_instance()