main.py 2.6 KB

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