main.py 2.8 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 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. app_url = "/example"
  60. name = __name__
  61. load_other_extensions = False
  62. app_name = 'JupyterLab Example Notebook'
  63. app_settings_dir = os.path.join(HERE, 'build', 'application_settings')
  64. schemas_dir = os.path.join(HERE, 'build', 'schemas')
  65. static_dir = os.path.join(HERE, 'build')
  66. templates_dir = os.path.join(HERE, 'templates')
  67. themes_dir = os.path.join(HERE, 'build', 'themes')
  68. user_settings_dir = os.path.join(HERE, 'build', 'user_settings')
  69. workspaces_dir = os.path.join(HERE, 'build', 'workspaces')
  70. def initialize_handlers(self):
  71. """Add example handler to Lab Server's handler list.
  72. """
  73. self.handlers.append(
  74. ('/example', ExampleHandler)
  75. )
  76. if __name__ == '__main__':
  77. ExampleApp.launch_instance()