main.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 jupyterlab_server import LabServerApp
  13. from jupyter_server.base.handlers import JupyterHandler
  14. from jupyter_server.extension.handler import ExtensionHandlerMixin, ExtensionHandlerJinjaMixin
  15. from jupyter_server.utils import url_path_join as ujoin
  16. HERE = os.path.dirname(__file__)
  17. with open(os.path.join(HERE, 'package.json')) as fid:
  18. version = json.load(fid)['version']
  19. def _jupyter_server_extension_points():
  20. return [
  21. {
  22. 'module': __name__,
  23. 'app': ExampleApp
  24. }
  25. ]
  26. class ExampleHandler(
  27. ExtensionHandlerJinjaMixin,
  28. ExtensionHandlerMixin,
  29. JupyterHandler
  30. ):
  31. """
  32. Serve a notebook file from the filesystem in the notebook interface
  33. """
  34. def get(self):
  35. """Get the main page for the application's interface."""
  36. # Options set here can be read with PageConfig.getOption
  37. config_data = {
  38. # Use camelCase here, since that's what the lab components expect
  39. 'baseUrl': self.base_url,
  40. 'token': self.settings['token'],
  41. 'notebookPath': 'test.ipynb',
  42. 'fullStaticUrl': ujoin(self.base_url, 'static', self.name),
  43. 'frontendUrl': ujoin(self.base_url, 'example/'),
  44. # FIXME: Don't use a CDN here
  45. 'mathjaxUrl': "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js",
  46. 'mathjaxConfig': "TeX-AMS_CHTML-full,Safeee"
  47. }
  48. return self.write(
  49. self.render_template(
  50. 'index.html',
  51. static=self.static_url,
  52. base_url=self.base_url,
  53. token=self.settings['token'],
  54. page_config=config_data
  55. )
  56. )
  57. class ExampleApp(LabServerApp):
  58. extension_url = '/example'
  59. default_url = '/example'
  60. app_url = "/example"
  61. name = __name__
  62. load_other_extensions = False
  63. app_name = 'JupyterLab Example Notebook'
  64. app_settings_dir = os.path.join(HERE, 'build', 'application_settings')
  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()