main.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. mathjax_config = self.settings.get('mathjax_config',
  38. 'TeX-AMS_HTML-full,Safe')
  39. mathjax_url = self.settings.get('mathjax_url', 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js')
  40. config_data = {
  41. # Use camelCase here, since that's what the lab components expect
  42. 'baseUrl': self.base_url,
  43. 'token': self.settings['token'],
  44. 'notebookPath': 'test.ipynb',
  45. 'fullStaticUrl': ujoin(self.base_url, 'static', self.name),
  46. 'frontendUrl': ujoin(self.base_url, 'example/'),
  47. 'mathjaxUrl': mathjax_url,
  48. 'mathjaxConfig': mathjax_config
  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. default_url = '/example'
  62. app_url = "/example"
  63. name = __name__
  64. app_name = 'JupyterLab Example Notebook'
  65. app_settings_dir = os.path.join(HERE, 'build', 'application_settings')
  66. schemas_dir = os.path.join(HERE, 'build', 'schemas')
  67. static_dir = os.path.join(HERE, 'build')
  68. templates_dir = os.path.join(HERE, 'templates')
  69. themes_dir = os.path.join(HERE, 'build', 'themes')
  70. user_settings_dir = os.path.join(HERE, 'build', 'user_settings')
  71. workspaces_dir = os.path.join(HERE, 'build', 'workspaces')
  72. serverapp_config = {
  73. "jpserver_extensions": {
  74. "nbclassic": True
  75. }
  76. }
  77. def initialize_handlers(self):
  78. """Add example handler to Lab Server's handler list.
  79. """
  80. self.handlers.append(
  81. ('/example', ExampleHandler)
  82. )
  83. if __name__ == '__main__':
  84. ExampleApp.launch_instance()