extension.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. import os
  6. from jinja2 import FileSystemLoader
  7. from jupyterlab_launcher import add_handlers
  8. from .commands import _get_build_dir, _get_config, _get_config_dir
  9. #-----------------------------------------------------------------------------
  10. # Module globals
  11. #-----------------------------------------------------------------------------
  12. DEV_NOTE_NPM = """It looks like you're running JupyterLab from source.
  13. If you're working on the TypeScript sources of JupyterLab, try running
  14. npm run watch
  15. from the JupyterLab repo directory in another terminal window to have the
  16. system incrementally watch and build JupyterLab's TypeScript for you, as you
  17. make changes.
  18. """
  19. HERE = os.path.dirname(__file__)
  20. FILE_LOADER = FileSystemLoader(HERE)
  21. PREFIX = '/lab'
  22. def load_jupyter_server_extension(nbapp):
  23. """Load the JupyterLab server extension.
  24. """
  25. # Print messages.
  26. nbapp.log.info('JupyterLab alpha preview extension loaded from %s' % HERE)
  27. base_dir = os.path.realpath(os.path.join(HERE, '..'))
  28. dev_mode = os.path.exists(os.path.join(base_dir, '.git'))
  29. if dev_mode:
  30. nbapp.log.info(DEV_NOTE_NPM)
  31. web_app = nbapp.web_app
  32. # Handle page config data.
  33. config_dir = _get_config_dir()
  34. build_config = _get_config()
  35. page_config_file = os.path.join(config_dir, 'page_config_data.json')
  36. build_dir = _get_build_dir(build_config)
  37. # Check for dev mode.
  38. dev_mode = False
  39. if hasattr(nbapp, 'dev_mode'):
  40. dev_mode = nbapp.dev_mode
  41. if not os.path.exists(build_dir) or dev_mode:
  42. print('Serving local JupyterLab files')
  43. build_dir = os.path.join(HERE, 'build')
  44. add_handlers(
  45. web_app, page_config_file, build_dir, 'JupyterLab Alpha Preview',
  46. PREFIX
  47. )