def load_jupyter_server_extension(nbapp): """Load the JupyterLab server extension. """ # Delay imports to speed up jlpmapp from json import dumps from jupyterlab_server import add_handlers from notebook.utils import url_path_join as ujoin, url_escape from notebook._version import version_info from tornado.ioloop import IOLoop from markupsafe import Markup from .handlers.build_handler import build_path, Builder, BuildHandler from .handlers.extension_manager_handler import (extensions_handler_path, ExtensionManager, ExtensionHandler) from .handlers.error_handler import ErrorHandler from .commands import (DEV_DIR, HERE, ensure_app, ensure_core, ensure_dev, watch, watch_dev, get_app_dir, AppOptions) web_app = nbapp.web_app logger = nbapp.log base_url = nbapp.base_url # Handle the app_dir app_dir = getattr(nbapp, 'app_dir', get_app_dir()) build_handler_options = AppOptions(logger=logger, app_dir=app_dir) # Check for core mode. core_mode = False if getattr(nbapp, 'core_mode', False) or app_dir.startswith(HERE): app_dir = HERE core_mode = True logger.info('Running JupyterLab in core mode') # Check for dev mode. dev_mode = False if getattr(nbapp, 'dev_mode', False) or app_dir.startswith(DEV_DIR): app_dir = DEV_DIR dev_mode = True logger.info('Running JupyterLab in dev mode') # Set the value on nbapp so it will get picked up in load_config nbapp.app_dir = app_dir config = load_config(nbapp) config.app_name = 'JupyterLab' config.app_namespace = 'jupyterlab' config.app_url = '/lab' config.cache_files = True # Check for watch. watch_mode = getattr(nbapp, 'watch', False) if watch_mode and core_mode: logger.warn('Cannot watch in core mode, did you mean --dev-mode?') watch_mode = False if core_mode and dev_mode: logger.warn('Conflicting modes, choosing dev_mode over core_mode') core_mode = False page_config = web_app.settings.setdefault('page_config_data', dict()) page_config['buildAvailable'] = not core_mode and not dev_mode page_config['buildCheck'] = not core_mode and not dev_mode page_config['devMode'] = dev_mode page_config['token'] = nbapp.token # Handle quit button with support for Notebook < 5.6 page_config['quitButton'] = getattr(nbapp, 'quit_button', False) # Client-side code assumes notebookVersion is a JSON-encoded string # TODO: fix this when we can make such a change page_config['notebookVersion'] = dumps(version_info) page_config['exposeAppInBrowser'] = getattr(nbapp, 'expose_app_in_browser', False) if nbapp.file_to_run and type(nbapp).__name__ == "LabApp": relpath = os.path.relpath(nbapp.file_to_run, nbapp.notebook_dir) uri = url_escape(ujoin('/lab/tree', *relpath.split(os.sep))) nbapp.default_url = uri nbapp.file_to_run = '' # Print messages. logger.info('JupyterLab extension loaded from %s' % HERE) logger.info('JupyterLab application directory is %s' % app_dir) build_url = ujoin(base_url, build_path) builder = Builder(None, core_mode, None, app_options=build_handler_options) build_handler = (build_url, BuildHandler, {'builder': builder}) handlers = [build_handler] errored = False if core_mode: logger.info(CORE_NOTE.strip()) ensure_core(logger) elif dev_mode: if not watch_mode: ensure_dev(logger) logger.info(DEV_NOTE) # Make sure the app dir exists. elif not watch_mode: msgs = ensure_app(app_dir) if msgs: [logger.error(msg) for msg in msgs] handler = (ujoin(base_url, '/lab'), ErrorHandler, { 'messages': msgs }) handlers.append(handler) errored = True if watch_mode: logger.info('Starting JupyterLab watch mode...') # Set the ioloop in case the watch fails. nbapp.ioloop = IOLoop.current() if dev_mode: watch_dev(logger) else: watch(app_options=build_handler_options) page_config['buildAvailable'] = False config.cache_files = False if not core_mode and not errored: ext_url = ujoin(base_url, extensions_handler_path) ext_manager = ExtensionManager(app_options=build_handler_options) ext_handler = (ext_url, ExtensionHandler, {'manager': ext_manager}) handlers.append(ext_handler) # Must add before the root server handlers to avoid shadowing. web_app.add_handlers('.*$', handlers) # If running under JupyterHub, add more metadata. if hasattr(nbapp, 'hub_prefix'): page_config['hubPrefix'] = nbapp.hub_prefix page_config['hubHost'] = nbapp.hub_host page_config['hubUser'] = nbapp.user # Assume the server_name property indicates running JupyterHub 1.0. if hasattr(nbapp, 'server_name'): page_config['hubServerName'] = nbapp.server_name api_token = os.getenv('JUPYTERHUB_API_TOKEN', '') page_config['token'] = api_token # Add the root handlers if we have not errored. if not errored: add_handlers(web_app, config)
def load_jupyter_server_extension(nbapp): """Load the JupyterLab server extension. """ # Delay imports to speed up jlpmapp from json import dumps from jupyterlab_server import add_handlers from notebook.utils import url_path_join as ujoin, url_escape from notebook._version import version_info from tornado.ioloop import IOLoop from markupsafe import Markup from .handlers.build_handler import build_path, Builder, BuildHandler from .handlers.extension_manager_handler import ( extensions_handler_path, ExtensionManager, ExtensionHandler ) from .handlers.error_handler import ErrorHandler from .commands import ( DEV_DIR, HERE, ensure_app, ensure_core, ensure_dev, watch, watch_dev, get_app_dir ) web_app = nbapp.web_app logger = nbapp.log base_url = nbapp.base_url # Handle the app_dir app_dir = getattr(nbapp, 'app_dir', get_app_dir()) # Check for core mode. core_mode = False if getattr(nbapp, 'core_mode', False) or app_dir.startswith(HERE): app_dir = HERE core_mode = True logger.info('Running JupyterLab in core mode') # Check for dev mode. dev_mode = False if getattr(nbapp, 'dev_mode', False) or app_dir.startswith(DEV_DIR): app_dir = DEV_DIR dev_mode = True logger.info('Running JupyterLab in dev mode') # Set the value on nbapp so it will get picked up in load_config nbapp.app_dir = app_dir config = load_config(nbapp) config.app_name = 'JupyterLab' config.app_namespace = 'jupyterlab' config.app_url = '/lab' config.cache_files = True # Check for watch. watch_mode = getattr(nbapp, 'watch', False) if watch_mode and core_mode: logger.warn('Cannot watch in core mode, did you mean --dev-mode?') watch_mode = False if core_mode and dev_mode: logger.warn('Conflicting modes, choosing dev_mode over core_mode') core_mode = False page_config = web_app.settings.setdefault('page_config_data', dict()) page_config['buildAvailable'] = not core_mode and not dev_mode page_config['buildCheck'] = not core_mode and not dev_mode page_config['devMode'] = dev_mode page_config['token'] = nbapp.token # Export the version info tuple to a JSON array. This gets printed # inside double quote marks, so we render it to a JSON string of the # JSON data (so that we can call JSON.parse on the frontend on it). # We also have to wrap it in `Markup` so that it isn't escaped # by Jinja. Otherwise, if the version has string parts these will be # escaped and then will have to be unescaped on the frontend. page_config['notebookVersion'] = Markup(dumps(dumps(version_info))[1:-1]) if nbapp.file_to_run and type(nbapp).__name__ == "LabApp": relpath = os.path.relpath(nbapp.file_to_run, nbapp.notebook_dir) uri = url_escape(ujoin('/lab/tree', *relpath.split(os.sep))) nbapp.default_url = uri nbapp.file_to_run = '' # Print messages. logger.info('JupyterLab extension loaded from %s' % HERE) logger.info('JupyterLab application directory is %s' % app_dir) build_url = ujoin(base_url, build_path) builder = Builder(logger, core_mode, app_dir) build_handler = (build_url, BuildHandler, {'builder': builder}) handlers = [build_handler] errored = False if core_mode: logger.info(CORE_NOTE.strip()) ensure_core(logger) elif dev_mode: if not watch_mode: ensure_dev(logger) logger.info(DEV_NOTE) # Make sure the app dir exists. else: msgs = ensure_app(app_dir) if msgs: [logger.error(msg) for msg in msgs] handler = (ujoin(base_url, '/lab'), ErrorHandler, { 'messages': msgs }) handlers.append(handler) errored = True if watch_mode: logger.info('Starting JupyterLab watch mode...') # Set the ioloop in case the watch fails. nbapp.ioloop = IOLoop.current() if dev_mode: watch_dev(logger) else: watch(app_dir, logger) page_config['buildAvailable'] = False config.cache_files = False if not core_mode and not errored: ext_url = ujoin(base_url, extensions_handler_path) ext_manager = ExtensionManager(logger, app_dir) ext_handler = (ext_url, ExtensionHandler, {'manager': ext_manager}) handlers.append(ext_handler) # Must add before the root server handlers to avoid shadowing. web_app.add_handlers('.*$', handlers) # If running under JupyterHub, add more metadata. if hasattr(nbapp, 'hub_prefix'): page_config['hubPrefix'] = nbapp.hub_prefix page_config['hubHost'] = nbapp.hub_host page_config['hubUser'] = nbapp.user api_token = os.getenv('JUPYTERHUB_API_TOKEN', '') page_config['token'] = api_token # Add the root handlers if we have not errored. if not errored: add_handlers(web_app, config)
def load_jupyter_server_extension(nbapp): """Load the JupyterLab server extension. """ # Delay imports to speed up jlpmapp from json import dumps from jupyterlab_server import add_handlers from notebook.utils import url_path_join as ujoin, url_escape from notebook._version import version_info from tornado.ioloop import IOLoop from markupsafe import Markup from .build_handler import build_path, Builder, BuildHandler from .extension_manager_handler import ( extensions_handler_path, ExtensionManager, ExtensionHandler ) from .commands import ( DEV_DIR, HERE, ensure_core, ensure_dev, watch, watch_dev, get_app_dir ) web_app = nbapp.web_app logger = nbapp.log # Handle the app_dir app_dir = getattr(nbapp, 'app_dir', get_app_dir()) # Check for core mode. core_mode = False if getattr(nbapp, 'core_mode', False) or app_dir.startswith(HERE): app_dir = HERE core_mode = True logger.info('Running JupyterLab in core mode') # Check for dev mode. dev_mode = False if getattr(nbapp, 'dev_mode', False) or app_dir.startswith(DEV_DIR): app_dir = DEV_DIR dev_mode = True logger.info('Running JupyterLab in dev mode') # Set the value on nbapp so it will get picked up in load_config nbapp.app_dir = app_dir config = load_config(nbapp) config.app_name = 'JupyterLab' config.app_namespace = 'jupyterlab' config.page_url = '/lab' config.cache_files = True # Check for watch. watch_mode = getattr(nbapp, 'watch', False) if watch_mode and core_mode: logger.warn('Cannot watch in core mode, did you mean --dev-mode?') watch_mode = False if core_mode and dev_mode: logger.warn('Conflicting modes, choosing dev_mode over core_mode') core_mode = False page_config = web_app.settings.setdefault('page_config_data', dict()) page_config['buildAvailable'] = not core_mode and not dev_mode page_config['buildCheck'] = not core_mode and not dev_mode page_config['token'] = nbapp.token page_config['devMode'] = dev_mode # Export the version info tuple to a JSON array. This gets printed # inside double quote marks, so we render it to a JSON string of the # JSON data (so that we can call JSON.parse on the frontend on it). # We also have to wrap it in `Markup` so that it isn't escaped # by Jinja. Otherwise, if the version has string parts these will be # escaped and then will have to be unescaped on the frontend. page_config['notebookVersion'] = Markup(dumps(dumps(version_info))[1:-1]) if nbapp.file_to_run and type(nbapp).__name__ == "LabApp": relpath = os.path.relpath(nbapp.file_to_run, nbapp.notebook_dir) uri = url_escape(ujoin('/lab/tree', *relpath.split(os.sep))) nbapp.default_url = uri nbapp.file_to_run = '' if core_mode: logger.info(CORE_NOTE.strip()) ensure_core(logger) elif dev_mode: ensure_dev(logger) if not watch_mode: logger.info(DEV_NOTE) # Print messages. logger.info('JupyterLab extension loaded from %s' % HERE) logger.info('JupyterLab application directory is %s' % app_dir) if watch_mode: logger.info('Starting JupyterLab watch mode...') # Set the ioloop in case the watch fails. nbapp.ioloop = IOLoop.current() if dev_mode: watch_dev(logger) else: watch(app_dir, logger) page_config['buildAvailable'] = False config.cache_files = False base_url = web_app.settings['base_url'] build_url = ujoin(base_url, build_path) builder = Builder(logger, core_mode, app_dir) build_handler = (build_url, BuildHandler, {'builder': builder}) handlers = [build_handler] if not core_mode: ext_url = ujoin(base_url, extensions_handler_path) ext_manager = ExtensionManager(logger, app_dir) ext_handler = (ext_url, ExtensionHandler, {'manager': ext_manager}) handlers.append(ext_handler) # Must add before the launcher handlers to avoid shadowing. web_app.add_handlers('.*$', handlers) add_handlers(web_app, config)