main.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 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_points():
  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. 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. load_other_extensions = False
  56. name = __name__
  57. app_name = 'JupyterLab Example Console'
  58. app_settings_dir = os.path.join(HERE, 'build', 'application_settings')
  59. schemas_dir = os.path.join(HERE, 'build', 'schemas')
  60. static_dir = os.path.join(HERE, 'build')
  61. templates_dir = os.path.join(HERE, 'templates')
  62. themes_dir = os.path.join(HERE, 'build', 'themes')
  63. user_settings_dir = os.path.join(HERE, 'build', 'user_settings')
  64. workspaces_dir = os.path.join(HERE, 'build', 'workspaces')
  65. def initialize_handlers(self):
  66. """Initialize JupyterLab handlers."""
  67. self.handlers.append(
  68. ('/example', ExampleHandler)
  69. )
  70. if __name__ == '__main__':
  71. ExampleApp.launch_instance()