main.py 3.0 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 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. """
  23. Serve a notebook file from the filesystem in the notebook interface
  24. """
  25. def initialize(self):
  26. super().initialize('lab')
  27. def get(self):
  28. """Get the main page for the application's interface."""
  29. # Options set here can be read with PageConfig.getOption
  30. config_data = {
  31. # Use camelCase here, since that's what the lab components expect
  32. 'baseUrl': self.base_url,
  33. 'token': self.settings['token'],
  34. 'notebookPath': 'test.ipynb',
  35. 'fullStaticUrl': ujoin(self.base_url, 'static', 'example'),
  36. 'frontendUrl': ujoin(self.base_url, 'example/'),
  37. # FIXME: Don't use a CDN here
  38. 'mathjaxUrl': "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js",
  39. 'mathjaxConfig': "TeX-AMS_CHTML-full,Safe"
  40. }
  41. return self.write(
  42. self.render_template(
  43. 'index.html',
  44. static=self.static_url,
  45. base_url=self.base_url,
  46. token=self.settings['token'],
  47. page_config=config_data
  48. )
  49. )
  50. class ExampleApp(LabServerApp):
  51. default_url = Unicode('/example')
  52. lab_config = LabConfig(
  53. app_name = 'JupyterLab Example Notebook',
  54. app_settings_dir = os.path.join(HERE, 'build', 'application_settings'),
  55. app_url = '/example',
  56. schemas_dir = os.path.join(HERE, 'build', 'schemas'),
  57. static_dir = os.path.join(HERE, 'build'),
  58. templates_dir = os.path.join(HERE, 'templates'),
  59. themes_dir = os.path.join(HERE, 'build', 'themes'),
  60. user_settings_dir = os.path.join(HERE, 'build', 'user_settings'),
  61. workspaces_dir = os.path.join(HERE, 'build', 'workspaces'),
  62. )
  63. def initialize_handlers(self):
  64. """initialize tornado webapp and httpserver.
  65. """
  66. default_handlers = [
  67. (ujoin(self.serverapp.base_url, 'example'), ExampleHandler),
  68. ]
  69. self.serverapp.web_app.add_handlers('.*$', default_handlers)
  70. super().initialize_handlers()
  71. if __name__ == '__main__':
  72. ExampleApp.launch_instance()