extension.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # coding: utf-8
  2. """A tornado based Jupyter lab server."""
  3. # Copyright (c) Jupyter Development Team.
  4. # Distributed under the terms of the Modified BSD License.
  5. #-----------------------------------------------------------------------------
  6. # Module globals
  7. #-----------------------------------------------------------------------------
  8. DEV_NOTE_NPM = """You're running JupyterLab from source.
  9. If you're working on the TypeScript sources of JupyterLab, try running
  10. jlpm run --dev-mode watch
  11. to have the system incrementally watch and build JupyterLab for you, as you
  12. make changes.
  13. """
  14. CORE_NOTE = """
  15. Running the core application with no additional extensions or settings
  16. """
  17. def load_jupyter_server_extension(nbapp):
  18. """Load the JupyterLab server extension.
  19. """
  20. # Delay imports to speed up jlpmapp
  21. import os
  22. from jupyterlab_launcher import add_handlers, LabConfig
  23. from notebook.utils import url_path_join as ujoin
  24. from tornado.ioloop import IOLoop
  25. from .build_handler import build_path, Builder, BuildHandler
  26. from .commands import (
  27. get_app_dir, get_user_settings_dir, watch, watch_dev
  28. )
  29. from ._version import __version__
  30. # Print messages.
  31. here = os.path.dirname(__file__)
  32. nbapp.log.info('JupyterLab alpha preview extension loaded from %s' % here)
  33. if hasattr(nbapp, 'app_dir'):
  34. app_dir = nbapp.app_dir or get_app_dir()
  35. else:
  36. app_dir = get_app_dir()
  37. web_app = nbapp.web_app
  38. config = LabConfig()
  39. config.name = 'JupyterLab'
  40. config.assets_dir = os.path.join(app_dir, 'static')
  41. config.settings_dir = os.path.join(app_dir, 'settings')
  42. config.page_title = 'JupyterLab Alpha Preview'
  43. config.page_url = '/lab'
  44. config.dev_mode = False
  45. # Check for core mode.
  46. core_mode = ''
  47. if hasattr(nbapp, 'core_mode'):
  48. core_mode = nbapp.core_mode
  49. # Check for watch.
  50. watch_mode = False
  51. if hasattr(nbapp, 'watch'):
  52. watch_mode = nbapp.watch
  53. # Check for an app dir that is local.
  54. if app_dir == here or app_dir == os.path.join(here, 'build'):
  55. core_mode = True
  56. config.settings_dir = ''
  57. page_config = web_app.settings.setdefault('page_config_data', dict())
  58. page_config['buildAvailable'] = not core_mode
  59. page_config['buildCheck'] = not core_mode
  60. page_config['token'] = nbapp.token
  61. if core_mode:
  62. config.assets_dir = os.path.join(here, 'build')
  63. config.version = __version__
  64. if not os.path.exists(config.assets_dir) and not watch_mode:
  65. msg = 'Static assets not built, please see CONTRIBUTING.md'
  66. nbapp.log.error(msg)
  67. else:
  68. sentinel = os.path.join(here, 'build', 'release_data.json')
  69. config.dev_mode = not os.path.exists(sentinel)
  70. if config.dev_mode and not watch_mode:
  71. nbapp.log.info(DEV_NOTE_NPM)
  72. elif core_mode:
  73. nbapp.log.info(CORE_NOTE.strip())
  74. if core_mode:
  75. schemas_dir = os.path.join(here, 'schemas')
  76. config.themes_dir = os.path.join(here, 'themes')
  77. else:
  78. schemas_dir = os.path.join(app_dir, 'schemas')
  79. config.themes_dir = os.path.join(app_dir, 'themes')
  80. config.schemas_dir = schemas_dir
  81. config.user_settings_dir = get_user_settings_dir()
  82. if watch_mode:
  83. # Set the ioloop in case the watch fails.
  84. nbapp.ioloop = IOLoop.current()
  85. if config.dev_mode:
  86. watch_dev(nbapp.log)
  87. else:
  88. watch(app_dir, nbapp.log)
  89. page_config['buildAvailable'] = False
  90. add_handlers(web_app, config)
  91. base_url = web_app.settings['base_url']
  92. build_url = ujoin(base_url, build_path)
  93. builder = Builder(nbapp.log, core_mode, app_dir)
  94. build_handler = (build_url, BuildHandler, {'builder': builder})
  95. web_app.add_handlers(".*$", [build_handler])