main.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. """
  2. Copyright (c) Jupyter Development Team.
  3. Distributed under the terms of the Modified BSD License.
  4. """
  5. import os
  6. from jinja2 import FileSystemLoader
  7. from notebook.base.handlers import IPythonHandler, FileFindHandler
  8. from notebook.utils import url_path_join as ujoin
  9. from notebook.notebookapp import NotebookApp
  10. from traitlets import Unicode
  11. from jupyterlab_launcher.handlers import SettingsHandler, ThemesHandler
  12. HERE = os.path.dirname(__file__)
  13. class ExampleHandler(IPythonHandler):
  14. """Handle requests between the main app page and notebook server."""
  15. def get(self):
  16. """Get the main page for the application's interface."""
  17. return self.write(self.render_template("index.html",
  18. static=self.static_url, base_url=self.base_url,
  19. terminals_available=self.settings['terminals_available'],
  20. page_config=self.settings['page_config_data']))
  21. def get_template(self, name):
  22. loader = FileSystemLoader(os.getcwd())
  23. return loader.load(self.settings['jinja2_env'], name)
  24. class ExampleApp(NotebookApp):
  25. default_url = Unicode('/example')
  26. def init_webapp(self):
  27. """initialize tornado webapp and httpserver.
  28. """
  29. super(ExampleApp, self).init_webapp()
  30. wsettings = self.web_app.settings
  31. base_url = wsettings['base_url']
  32. page_url = self.default_url
  33. settings_path = ujoin(
  34. base_url, page_url, 'api', 'settings', '(?P<section_name>.+)'
  35. )
  36. themes_path = ujoin(
  37. base_url, page_url, 'api', 'themes'
  38. )
  39. wsettings.setdefault('page_config_data', dict())
  40. wsettings['page_config_data']['token'] = self.token
  41. wsettings['page_config_data']['pageUrl'] = page_url
  42. wsettings['page_config_data']['themesUrl'] = themes_path
  43. default_handlers = [
  44. (ujoin(base_url, '/example?'), ExampleHandler),
  45. ((settings_path, SettingsHandler, {
  46. 'schemas_dir': os.path.join(HERE, 'build', 'schemas'),
  47. 'settings_dir': '',
  48. 'app_settings_dir': ''
  49. })),
  50. ((ujoin(themes_path, "(.*)"), ThemesHandler, {
  51. 'themes_url': themes_path,
  52. 'path': os.path.join(HERE, 'build', 'themes')
  53. })),
  54. (ujoin(base_url, '/example/static/(.*)'), FileFindHandler,
  55. {'path': 'build'}),
  56. # Let the lab handler act as the fallthrough option instead
  57. # of a 404.
  58. (ujoin(base_url, page_url, r'/?.*'), ExampleHandler)
  59. ]
  60. self.web_app.add_handlers(".*$", default_handlers)
  61. if __name__ == '__main__':
  62. ExampleApp.launch_instance()