main.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. """
  2. An example demonstrating a stand-alone "terminal".
  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. class ExampleHandler(ExtensionHandlerJinjaMixin, ExtensionHandlerMixin, JupyterHandler):
  22. """Handle requests between the main app page and notebook server."""
  23. def initialize(self):
  24. super().initialize('lab')
  25. def get(self):
  26. """Get the main page for the application's interface."""
  27. available = self.settings['terminals_available']
  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', 'example'),
  34. 'frontendUrl': ujoin(self.base_url, 'example/'),
  35. 'terminalsAvailable': available
  36. }
  37. return self.write(self.render_template('index.html',
  38. static=self.static_url,
  39. base_url=self.base_url,
  40. token=self.settings['token'],
  41. terminals_available=available,
  42. page_config=config_data))
  43. class ExampleApp(LabServerApp):
  44. default_url = Unicode('/example')
  45. lab_config = LabConfig(
  46. app_name = 'JupyterLab Example Console',
  47. app_settings_dir = os.path.join(HERE, 'build', 'application_settings'),
  48. app_url = '/example',
  49. schemas_dir = os.path.join(HERE, 'build', 'schemas'),
  50. static_dir = os.path.join(HERE, 'build'),
  51. templates_dir = os.path.join(HERE, 'templates'),
  52. themes_dir = os.path.join(HERE, 'build', 'themes'),
  53. user_settings_dir = os.path.join(HERE, 'build', 'user_settings'),
  54. workspaces_dir = os.path.join(HERE, 'build', 'workspaces'),
  55. )
  56. def initialize_handlers(self):
  57. """initialize tornado webapp and httpserver.
  58. """
  59. default_handlers = [
  60. (ujoin(self.serverapp.base_url, 'example'), ExampleHandler),
  61. ]
  62. self.serverapp.web_app.add_handlers('.*$', default_handlers)
  63. super().initialize_handlers()
  64. if __name__ == '__main__':
  65. ExampleApp.launch_instance()