main.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. def _jupyter_server_extension_paths():
  22. return [
  23. {
  24. 'module': 'main',
  25. 'app': ExampleApp
  26. }
  27. ]
  28. class ExampleHandler(
  29. ExtensionHandlerJinjaMixin,
  30. ExtensionHandlerMixin,
  31. JupyterHandler
  32. ):
  33. """
  34. Serve a notebook file from the filesystem in the notebook interface
  35. """
  36. def get(self):
  37. """Get the main page for the application's interface."""
  38. # Options set here can be read with PageConfig.getOption
  39. config_data = {
  40. # Use camelCase here, since that's what the lab components expect
  41. 'baseUrl': self.base_url,
  42. 'token': self.settings['token'],
  43. 'notebookPath': 'test.ipynb',
  44. 'fullStaticUrl': ujoin(self.base_url, 'static', self.extension_name),
  45. 'frontendUrl': ujoin(self.base_url, 'example/'),
  46. # FIXME: Don't use a CDN here
  47. 'mathjaxUrl': "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js",
  48. 'mathjaxConfig': "TeX-AMS_CHTML-full,Safeee"
  49. }
  50. return self.write(
  51. self.render_template(
  52. 'index.html',
  53. static=self.static_url,
  54. base_url=self.base_url,
  55. token=self.settings['token'],
  56. page_config=config_data
  57. )
  58. )
  59. class ExampleApp(LabServerApp):
  60. extension_url = '/example'
  61. extension_name = 'main'
  62. app_name = 'JupyterLab Example Notebook'
  63. app_settings_dir = os.path.join(HERE, 'build', 'application_settings')
  64. app_url = '/example_app'
  65. schemas_dir = os.path.join(HERE, 'build', 'schemas')
  66. static_dir = os.path.join(HERE, 'build')
  67. templates_dir = os.path.join(HERE, 'templates')
  68. themes_dir = os.path.join(HERE, 'build', 'themes')
  69. user_settings_dir = os.path.join(HERE, 'build', 'user_settings')
  70. workspaces_dir = os.path.join(HERE, 'build', 'workspaces')
  71. def initialize_handlers(self):
  72. """Add example handler to Lab Server's handler list.
  73. """
  74. self.handlers.append(
  75. ('/example', ExampleHandler)
  76. )
  77. if __name__ == '__main__':
  78. ExampleApp.launch_instance()