Exemplo n.º 1
0
def load_jupyter_server_extension(nbapp):
    """Load the JupyterLab server extension.
    """
    # Print messages.
    here = os.path.dirname(__file__)
    nbapp.log.info('JupyterLab alpha preview extension loaded from %s' % here)

    app_dir = get_app_dir()
    if hasattr(nbapp, 'app_dir'):
        app_dir = get_app_dir(nbapp.app_dir)

    web_app = nbapp.web_app
    config = LabConfig()

    config.assets_dir = os.path.join(app_dir, 'static')
    config.settings_dir = os.path.join(app_dir, 'settings')
    config.page_title = 'JupyterLab Alpha Preview'
    config.page_url = '/lab'
    config.dev_mode = False

    # Check for core mode.
    core_mode = ''
    if hasattr(nbapp, 'core_mode'):
        core_mode = nbapp.core_mode

    # Check for an app dir that is local.
    if app_dir == here or app_dir == os.path.join(here, 'build'):
        core_mode = True
        config.settings_dir = ''

    # Run core mode if explicit or there is no static dir and no
    # installed extensions.
    installed = list_extensions(app_dir)
    fallback = not installed and not os.path.exists(config.assets_dir)

    web_app.settings.setdefault('page_config_data', dict())
    web_app.settings['page_config_data']['token'] = nbapp.token

    if not core_mode:
        build_needed, msg = should_build(app_dir)
        if build_needed:
            nbapp.log.warn('Build required: %s' % msg)
            web_app.settings['page_config_data']['buildRequired'] = msg

    if core_mode or fallback:
        config.assets_dir = os.path.join(here, 'build')
        if not os.path.exists(config.assets_dir):
            msg = 'Static assets not built, please see CONTRIBUTING.md'
            nbapp.log.error(msg)
        else:
            sentinel = os.path.join(here, 'build', 'release_data.json')
            config.dev_mode = not os.path.exists(sentinel)

    if config.dev_mode:
        nbapp.log.info(DEV_NOTE_NPM)
    elif core_mode or fallback:
        nbapp.log.info(CORE_NOTE.strip())

    add_handlers(web_app, config)
    def start(self):
        self.io_loop = ioloop.IOLoop.current()
        config = LabConfig()
        if self.core_mode:
            config.assets_dir = os.path.join(here, 'build')
        elif self.app_dir:
            config.assets_dir = os.path.join(self.app_dir, 'static')
        else:
            config.assets_dir = os.path.join(get_app_dir(), 'static')

        print('****Testing assets dir %s' % config.assets_dir)

        config.settings_dir = ''

        add_handlers(self.web_app, config)
        self.io_loop.call_later(1, self._run_selenium)
        super(TestApp, self).start()
Exemplo n.º 3
0
def load_jupyter_server_extension(nbapp):
    """Load the JupyterLab server extension.
    """
    # Print messages.
    here = os.path.dirname(__file__)
    nbapp.log.info('JupyterLab alpha preview extension loaded from %s' % here)

    app_dir = get_app_dir()
    if hasattr(nbapp, 'app_dir'):
        app_dir = get_app_dir(nbapp.app_dir)

    web_app = nbapp.web_app
    config = LabConfig()

    config.assets_dir = os.path.join(app_dir, 'static')
    config.settings_dir = os.path.join(app_dir, 'settings')
    config.page_title = 'JupyterLab Alpha Preview'
    config.page_url = '/lab'
    config.dev_mode = False

    # Check for core mode.
    core_mode = ''
    if hasattr(nbapp, 'core_mode'):
        core_mode = nbapp.core_mode

    # Check for an app dir that is local.
    if app_dir == here or app_dir == os.path.join(here, 'build'):
        core_mode = True
        config.settings_dir = ''

    # Run core mode if explicit or there is no static dir and no
    # installed extensions.
    installed = list_extensions(app_dir)
    fallback = not installed and not os.path.exists(config.assets_dir)

    if core_mode or fallback:
        config.assets_dir = os.path.join(here, 'build')
        if not os.path.exists(config.assets_dir):
            msg = 'Static assets not built, please see CONTRIBUTING.md'
            nbapp.log.error(msg)
        else:
            sentinel = os.path.join(here, 'build', 'release_data.json')
            config.dev_mode = not os.path.exists(sentinel)

    if config.dev_mode:
        nbapp.log.info(DEV_NOTE_NPM)
    elif core_mode or fallback:
        nbapp.log.info(CORE_NOTE.strip())

    add_handlers(web_app, config)
Exemplo n.º 4
0
    def start(self):
        self.io_loop = ioloop.IOLoop.current()
        config = LabConfig()
        if self.core_mode:
            config.assets_dir = os.path.join(here, 'build')
        elif self.app_dir:
            config.assets_dir = os.path.join(self.app_dir, 'static')
        else:
            config.assets_dir = os.path.join(get_app_dir(), 'static')

        print('****Testing assets dir %s' % config.assets_dir)

        config.settings_dir = ''

        add_handlers(self.web_app, config)
        self.io_loop.call_later(1, self._run_selenium)
        super(TestApp, self).start()
Exemplo n.º 5
0
    def setup_class(cls):
        cls.tmp_dir = TemporaryDirectory()

        def tmp(*parts):
            path = os.path.join(cls.tmp_dir.name, *parts)
            try:
                os.makedirs(path)
            except OSError as e:
                if e.errno != errno.EEXIST:
                    raise
            return path

        cls.home_dir = tmp('home')
        data_dir = cls.data_dir = tmp('data')
        config_dir = cls.config_dir = tmp('config')
        runtime_dir = cls.runtime_dir = tmp('runtime')
        cls.notebook_dir = tmp('notebooks')
        cls.env_patch = patch.dict('os.environ', {
            'HOME': cls.home_dir,
            'PYTHONPATH': os.pathsep.join(sys.path),
            'IPYTHONDIR': pjoin(cls.home_dir, '.ipython'),
            'JUPYTER_NO_CONFIG': '1',  # needed in the future
            'JUPYTER_CONFIG_DIR': config_dir,
            'JUPYTER_DATA_DIR': data_dir,
            'JUPYTER_RUNTIME_DIR': runtime_dir,
        })
        cls.env_patch.start()
        cls.path_patch = patch.multiple(
            jupyter_core.paths,
            SYSTEM_JUPYTER_PATH=[tmp('share', 'jupyter')],
            ENV_JUPYTER_PATH=[tmp('env', 'share', 'jupyter')],
            SYSTEM_CONFIG_PATH=[tmp('etc', 'jupyter')],
            ENV_CONFIG_PATH=[tmp('env', 'etc', 'jupyter')],
        )
        cls.path_patch.start()

        config = cls.config or Config()
        config.NotebookNotary.db_file = ':memory:'

        cls.token = hexlify(os.urandom(4)).decode('ascii')

        started = Event()

        lab_config = LabConfig(schemas_dir=pjoin(here, 'schemas'),
            user_settings_dir=tmp('user_settings'))
        
        def start_thread():
            app = cls.notebook = LabLauncherApp(
                port=cls.port,
                port_retries=0,
                open_browser=False,
                config_dir=cls.config_dir,
                data_dir=cls.data_dir,
                runtime_dir=cls.runtime_dir,
                notebook_dir=cls.notebook_dir,
                base_url=cls.url_prefix,
                config=config,
                allow_root=True,
                token=cls.token,
                lab_config=lab_config
            )
            # don't register signal handler during tests
            app.init_signal = lambda: None
            # clear log handlers and propagate to root for nose to capture it
            # needs to be redone after initialize, which reconfigures logging
            app.log.propagate = True
            app.log.handlers = []
            app.initialize(argv=[])
            app.log.propagate = True
            app.log.handlers = []
            loop = IOLoop.current()
            loop.add_callback(started.set)
            try:
                app.start()
            finally:
                # set the event, so failure to start doesn't cause a hang
                started.set()
                app.session_manager.close()
        cls.notebook_thread = Thread(target=start_thread)
        cls.notebook_thread.daemon = True
        cls.notebook_thread.start()
        started.wait()
        cls.wait_until_alive()
Exemplo n.º 6
0
def load_jupyter_server_extension(nbapp):
    """Load the JupyterLab server extension.
    """
    # Delay imports to speed up jlpmapp
    from json import dumps
    from jupyterlab_launcher import add_handlers, LabConfig
    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 .commands import (
        get_app_dir, get_user_settings_dir, watch, ensure_dev, watch_dev,
        pjoin, DEV_DIR, HERE, get_app_info, ensure_core, get_workspaces_dir
    )

    web_app = nbapp.web_app
    logger = nbapp.log
    config = LabConfig()
    app_dir = getattr(nbapp, 'app_dir', get_app_dir())
    user_settings_dir = getattr(
        nbapp, 'user_settings_dir', get_user_settings_dir()
    )
    workspaces_dir = getattr(
        nbapp, 'workspaces_dir', get_workspaces_dir()
    )

    # Print messages.
    logger.info('JupyterLab beta preview extension loaded from %s' % HERE)
    logger.info('JupyterLab application directory is %s' % app_dir)

    config.app_name = 'JupyterLab Beta'
    config.app_namespace = 'jupyterlab'
    config.page_url = '/lab'
    config.cache_files = True

    # Check for core mode.
    core_mode = False
    if getattr(nbapp, 'core_mode', False) or app_dir.startswith(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):
        dev_mode = True
        logger.info('Running JupyterLab in dev mode')

    # 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 get's 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:
        app_dir = HERE
        logger.info(CORE_NOTE.strip())
        ensure_core(logger)

    elif dev_mode:
        app_dir = DEV_DIR
        ensure_dev(logger)
        if not watch_mode:
            logger.info(DEV_NOTE)

    config.app_settings_dir = pjoin(app_dir, 'settings')
    config.schemas_dir = pjoin(app_dir, 'schemas')
    config.themes_dir = pjoin(app_dir, 'themes')
    config.workspaces_dir = workspaces_dir
    info = get_app_info(app_dir)
    config.app_version = info['version']
    public_url = info['publicUrl']
    if public_url:
        config.public_url = public_url
    else:
        config.static_dir = pjoin(app_dir, 'static')

    config.user_settings_dir = user_settings_dir

    # The templates end up in the built static directory.
    config.templates_dir = pjoin(app_dir, 'static')

    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})

    # Must add before the launcher handlers to avoid shadowing.
    web_app.add_handlers('.*$', [build_handler])

    add_handlers(web_app, config)
Exemplo n.º 7
0
def load_jupyter_server_extension(nbapp):
    """Load the JupyterLab server extension.
    """
    # Delay imports to speed up jlpmapp
    from jupyterlab_launcher import add_handlers, LabConfig
    from notebook.utils import url_path_join as ujoin
    from tornado.ioloop import IOLoop
    from .build_handler import build_path, Builder, BuildHandler
    from .commands import (get_app_dir, get_user_settings_dir, watch,
                           ensure_dev, watch_dev, pjoin, DEV_DIR, HERE,
                           get_app_version)
    from ._version import __version__

    web_app = nbapp.web_app
    logger = nbapp.log
    config = LabConfig()
    app_dir = getattr(nbapp, 'app_dir', get_app_dir())

    # Print messages.
    logger.info('JupyterLab alpha preview extension loaded from %s' % HERE)
    logger.info('JupyterLab application directory is %s' % app_dir)

    config.app_name = 'JupyterLab'
    config.app_namespace = 'jupyterlab'
    config.page_url = '/lab'
    config.cache_files = True

    # Check for core mode.
    core_mode = False
    if getattr(nbapp, 'core_mode', False) or app_dir.startswith(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):
        dev_mode = True
        logger.info('Running JupyterLab in dev mode')

    # 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

    if core_mode:
        config.static_dir = pjoin(HERE, 'static')
        config.schemas_dir = pjoin(HERE, 'schemas')
        config.app_settings_dir = ''
        config.themes_dir = pjoin(HERE, 'themes')
        config.app_version = get_app_version()

        logger.info(CORE_NOTE.strip())
        if not os.path.exists(config.static_dir):
            msg = 'Static assets not built, please see CONTRIBUTING.md'
            logger.error(msg)

    elif dev_mode:
        config.static_dir = pjoin(DEV_DIR, 'build')
        config.schemas_dir = pjoin(DEV_DIR, 'schemas')
        config.app_settings_dir = ''
        config.themes_dir = pjoin(DEV_DIR, 'themes')
        config.app_version = __version__

        ensure_dev(logger)
        if not watch_mode:
            logger.info(DEV_NOTE)

    else:
        config.static_dir = pjoin(app_dir, 'static')
        config.schemas_dir = pjoin(app_dir, 'schemas')
        config.app_settings_dir = pjoin(app_dir, 'settings')
        config.themes_dir = pjoin(app_dir, 'themes')
        config.app_version = get_app_version()

    page_config['devMode'] = dev_mode
    config.user_settings_dir = get_user_settings_dir()
    config.templates_dir = config.static_dir

    page_config['themePath'] = '/lab/api/themes'
    if 'rc' in __version__:
        raise ValueError('remove the theme path shim')

    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

    add_handlers(web_app, config)

    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})

    web_app.add_handlers(".*$", [build_handler])
Exemplo n.º 8
0
def load_jupyter_server_extension(nbapp):
    """Load the JupyterLab server extension.
    """
    # Print messages.
    here = os.path.dirname(__file__)
    nbapp.log.info('JupyterLab alpha preview extension loaded from %s' % here)

    app_dir = get_app_dir()
    if hasattr(nbapp, 'app_dir'):
        app_dir = get_app_dir(nbapp.app_dir)

    web_app = nbapp.web_app
    config = LabConfig()

    config.name = 'JupyterLab'
    config.assets_dir = os.path.join(app_dir, 'static')
    config.settings_dir = os.path.join(app_dir, 'settings')
    config.page_title = 'JupyterLab Alpha Preview'
    config.page_url = '/lab'
    config.dev_mode = False

    # Check for core mode.
    core_mode = ''
    if hasattr(nbapp, 'core_mode'):
        core_mode = nbapp.core_mode

    # Check for watch.
    watch_mode = False
    if hasattr(nbapp, 'watch'):
        watch_mode = nbapp.watch

    # Check for an app dir that is local.
    if app_dir == here or app_dir == os.path.join(here, 'build'):
        core_mode = True
        config.settings_dir = ''

    web_app.settings.setdefault('page_config_data', dict())
    web_app.settings['page_config_data']['buildAvailable'] = True
    web_app.settings['page_config_data']['token'] = nbapp.token

    if core_mode:
        config.assets_dir = os.path.join(here, 'build')
        config.version = __version__
        if not os.path.exists(config.assets_dir):
            msg = 'Static assets not built, please see CONTRIBUTING.md'
            nbapp.log.error(msg)
        else:
            sentinel = os.path.join(here, 'build', 'release_data.json')
            config.dev_mode = not os.path.exists(sentinel)

    if config.dev_mode:
        nbapp.log.info(DEV_NOTE_NPM)
    elif core_mode:
        nbapp.log.info(CORE_NOTE.strip())

    if core_mode:
        schemas_dir = os.path.join(here, 'schemas')
        config.themes_dir = os.path.join(here, 'themes')
    else:
        schemas_dir = os.path.join(app_dir, 'schemas')
        config.themes_dir = os.path.join(app_dir, 'themes')

    config.schemas_dir = schemas_dir
    config.user_settings_dir = get_user_settings_dir()

    if watch_mode:
        if core_mode:
            watch(here, nbapp.log)
        else:
            config.assets_dir = os.path.join(app_dir, 'staging', 'build')
            build(app_dir=app_dir, logger=nbapp.log)
            watch(os.path.join(app_dir, 'staging'), nbapp.log)

    add_handlers(web_app, config)

    base_url = web_app.settings['base_url']
    build_url = ujoin(base_url, build_path)
    builder = Builder(nbapp.log, core_mode, app_dir)
    build_handler = (build_url, BuildHandler, {'builder': builder})

    web_app.add_handlers(".*$", [build_handler])
Exemplo n.º 9
0
def load_jupyter_server_extension(nbapp):
    """Load the JupyterLab server extension.
    """
    # Print messages.
    here = os.path.dirname(__file__)
    nbapp.log.info('JupyterLab alpha preview extension loaded from %s' % here)

    app_dir = get_app_dir()
    if hasattr(nbapp, 'app_dir'):
        app_dir = get_app_dir(nbapp.app_dir)

    web_app = nbapp.web_app
    config = LabConfig()

    config.assets_dir = os.path.join(app_dir, 'static')
    config.settings_dir = os.path.join(app_dir, 'settings')
    config.page_title = 'JupyterLab Alpha Preview'
    config.page_url = '/lab'
    config.dev_mode = False

    # Check for core mode.
    core_mode = ''
    if hasattr(nbapp, 'core_mode'):
        core_mode = nbapp.core_mode

    # Check for an app dir that is local.
    if app_dir == here or app_dir == os.path.join(here, 'build'):
        core_mode = True
        config.settings_dir = ''

    # Run core mode if explicit or there is no static dir and no
    # installed extensions.
    installed = list_extensions(app_dir)
    fallback = not installed and not os.path.exists(config.assets_dir)

    web_app.settings.setdefault('page_config_data', dict())
    web_app.settings['page_config_data']['buildAvailable'] = True
    web_app.settings['page_config_data']['token'] = nbapp.token

    if core_mode or fallback:
        config.assets_dir = os.path.join(here, 'build')
        config.version = __version__
        if not os.path.exists(config.assets_dir):
            msg = 'Static assets not built, please see CONTRIBUTING.md'
            nbapp.log.error(msg)
        else:
            sentinel = os.path.join(here, 'build', 'release_data.json')
            config.dev_mode = not os.path.exists(sentinel)

    if config.dev_mode:
        nbapp.log.info(DEV_NOTE_NPM)
    elif core_mode or fallback:
        nbapp.log.info(CORE_NOTE.strip())

    if core_mode or fallback:
        schemas_dir = os.path.join(here, 'schemas')
    else:
        schemas_dir = os.path.join(app_dir, 'schemas')

    config.schemas_dir = schemas_dir
    config.user_settings_dir = get_user_settings_dir()
    config.themes_dir = os.path.join(here, 'themes')

    add_handlers(web_app, config)

    base_url = web_app.settings['base_url']
    build_url = ujoin(base_url, build_path)
    builder = Builder(nbapp.log, core_mode, app_dir)
    build_handler = (build_url, BuildHandler, {'builder': builder})

    web_app.add_handlers(".*$", [build_handler])
Exemplo n.º 10
0
def load_jupyter_server_extension(nbapp):
    """Load the JupyterLab server extension.
    """
    # Delay imports to speed up jlpmapp
    from jupyterlab_launcher import add_handlers, LabConfig
    from notebook.utils import url_path_join as ujoin, url_escape
    from tornado.ioloop import IOLoop
    from .build_handler import build_path, Builder, BuildHandler
    from .commands import (get_app_dir, get_user_settings_dir, watch,
                           ensure_dev, watch_dev, pjoin, DEV_DIR, HERE,
                           get_app_info, ensure_core)
    from ._version import __version__

    web_app = nbapp.web_app
    logger = nbapp.log
    config = LabConfig()
    app_dir = getattr(nbapp, 'app_dir', get_app_dir())

    # Print messages.
    logger.info('JupyterLab beta preview extension loaded from %s' % HERE)
    logger.info('JupyterLab application directory is %s' % app_dir)

    config.app_name = 'JupyterLab Beta'
    config.app_namespace = 'jupyterlab'
    config.page_url = '/lab'
    config.cache_files = True

    # Check for core mode.
    core_mode = False
    if getattr(nbapp, 'core_mode', False) or app_dir.startswith(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):
        dev_mode = True
        logger.info('Running JupyterLab in dev mode')

    # 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

    if nbapp.file_to_run:
        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:
        app_dir = HERE
        logger.info(CORE_NOTE.strip())
        ensure_core(logger)

    elif dev_mode:
        app_dir = DEV_DIR
        ensure_dev(logger)
        if not watch_mode:
            logger.info(DEV_NOTE)

    config.app_settings_dir = pjoin(app_dir, 'settings')
    config.schemas_dir = pjoin(app_dir, 'schemas')
    config.themes_dir = pjoin(app_dir, 'themes')
    info = get_app_info(app_dir)
    config.app_version = info['version']
    public_url = info['publicUrl']
    if public_url:
        config.public_url = public_url
    else:
        config.static_dir = pjoin(app_dir, 'static')

    config.user_settings_dir = get_user_settings_dir()
    config.templates_dir = pjoin(app_dir, 'static')

    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})

    # Must add before the launcher handlers to avoid shadowing.
    web_app.add_handlers(".*$", [build_handler])

    add_handlers(web_app, config)
Exemplo n.º 11
0
def load_jupyter_server_extension(nbapp):
    """Load the JupyterLab server extension.
    """
    # Delay imports to speed up jlpmapp
    import os
    from jupyterlab_launcher import add_handlers, LabConfig
    from notebook.utils import url_path_join as ujoin
    from tornado.ioloop import IOLoop
    from .build_handler import build_path, Builder, BuildHandler
    from .commands import (get_app_dir, get_user_settings_dir, watch,
                           watch_dev)
    from ._version import __version__

    # Print messages.
    here = os.path.dirname(__file__)
    nbapp.log.info('JupyterLab alpha preview extension loaded from %s' % here)

    if hasattr(nbapp, 'app_dir'):
        app_dir = nbapp.app_dir or get_app_dir()
    else:
        app_dir = get_app_dir()

    web_app = nbapp.web_app
    config = LabConfig()

    config.name = 'JupyterLab'
    config.assets_dir = os.path.join(app_dir, 'static')
    config.settings_dir = os.path.join(app_dir, 'settings')
    config.page_title = 'JupyterLab Alpha Preview'
    config.page_url = '/lab'
    config.dev_mode = False

    # Check for core mode.
    core_mode = ''
    if hasattr(nbapp, 'core_mode'):
        core_mode = nbapp.core_mode

    # Check for watch.
    watch_mode = False
    if hasattr(nbapp, 'watch'):
        watch_mode = nbapp.watch

    # Check for an app dir that is local.
    if app_dir == here or app_dir == os.path.join(here, 'build'):
        core_mode = True
        config.settings_dir = ''

    page_config = web_app.settings.setdefault('page_config_data', dict())
    page_config['buildAvailable'] = not core_mode
    page_config['buildCheck'] = not core_mode
    page_config['token'] = nbapp.token

    if core_mode:
        config.assets_dir = os.path.join(here, 'build')
        config.version = __version__
        if not os.path.exists(config.assets_dir) and not watch_mode:
            msg = 'Static assets not built, please see CONTRIBUTING.md'
            nbapp.log.error(msg)
        else:
            sentinel = os.path.join(here, 'build', 'release_data.json')
            config.dev_mode = not os.path.exists(sentinel)

    if config.dev_mode and not watch_mode:
        nbapp.log.info(DEV_NOTE_NPM)
    elif core_mode:
        nbapp.log.info(CORE_NOTE.strip())

    if core_mode:
        schemas_dir = os.path.join(here, 'schemas')
        config.themes_dir = os.path.join(here, 'themes')
    else:
        schemas_dir = os.path.join(app_dir, 'schemas')
        config.themes_dir = os.path.join(app_dir, 'themes')

    config.schemas_dir = schemas_dir
    config.user_settings_dir = get_user_settings_dir()

    if watch_mode:
        # Set the ioloop in case the watch fails.
        nbapp.ioloop = IOLoop.current()
        if config.dev_mode:
            watch_dev(nbapp.log)
        else:
            watch(app_dir, nbapp.log)
            page_config['buildAvailable'] = False

    add_handlers(web_app, config)

    base_url = web_app.settings['base_url']
    build_url = ujoin(base_url, build_path)
    builder = Builder(nbapp.log, core_mode, app_dir)
    build_handler = (build_url, BuildHandler, {'builder': builder})

    web_app.add_handlers(".*$", [build_handler])
Exemplo n.º 12
0
def load_jupyter_server_extension(nbapp):
    """Load the JupyterLab server extension.
    """
    # Delay imports to speed up jlpmapp
    from json import dumps
    from jupyterlab_launcher import add_handlers, LabConfig
    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 (get_app_dir, get_user_settings_dir, watch,
                           ensure_dev, watch_dev, pjoin, DEV_DIR, HERE,
                           get_app_info, ensure_core, get_workspaces_dir)

    web_app = nbapp.web_app
    logger = nbapp.log
    config = LabConfig()
    app_dir = getattr(nbapp, 'app_dir', get_app_dir())
    user_settings_dir = getattr(nbapp, 'user_settings_dir',
                                get_user_settings_dir())
    workspaces_dir = getattr(nbapp, 'workspaces_dir', get_workspaces_dir())

    # Print messages.
    logger.info('JupyterLab extension loaded from %s' % HERE)
    logger.info('JupyterLab application directory is %s' % app_dir)

    config.app_name = 'JupyterLab Beta'
    config.app_namespace = 'jupyterlab'
    config.page_url = '/lab'
    config.cache_files = True

    # Check for core mode.
    core_mode = False
    if getattr(nbapp, 'core_mode', False) or app_dir.startswith(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):
        dev_mode = True
        logger.info('Running JupyterLab in dev mode')

    # 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:
        app_dir = HERE
        logger.info(CORE_NOTE.strip())
        ensure_core(logger)

    elif dev_mode:
        app_dir = DEV_DIR
        ensure_dev(logger)
        if not watch_mode:
            logger.info(DEV_NOTE)

    config.app_settings_dir = pjoin(app_dir, 'settings')
    config.schemas_dir = pjoin(app_dir, 'schemas')
    config.themes_dir = pjoin(app_dir, 'themes')
    config.workspaces_dir = workspaces_dir
    info = get_app_info(app_dir)
    config.app_version = info['version']
    public_url = info['publicUrl']
    if public_url:
        config.public_url = public_url
    else:
        config.static_dir = pjoin(app_dir, 'static')

    config.user_settings_dir = user_settings_dir

    # The templates end up in the built static directory.
    config.templates_dir = pjoin(app_dir, 'static')

    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)
Exemplo n.º 13
0
def load_jupyter_server_extension(nbapp):
    """Load the JupyterLab server extension.
    """
    # Print messages.
    here = os.path.dirname(__file__)
    nbapp.log.info('JupyterLab alpha preview extension loaded from %s' % here)

    app_dir = get_app_dir()
    if hasattr(nbapp, 'app_dir'):
        app_dir = get_app_dir(nbapp.app_dir)

    web_app = nbapp.web_app
    config = LabConfig()

    config.assets_dir = os.path.join(app_dir, 'static')
    config.settings_dir = os.path.join(app_dir, 'settings')
    config.page_title = 'JupyterLab Alpha Preview'
    config.page_url = '/lab'
    config.dev_mode = False

    # Check for core mode.
    core_mode = ''
    if hasattr(nbapp, 'core_mode'):
        core_mode = nbapp.core_mode

    # Check for an app dir that is local.
    if app_dir == here or app_dir == os.path.join(here, 'build'):
        core_mode = True
        config.settings_dir = ''

    # Run core mode if explicit or there is no static dir and no
    # installed extensions.
    installed = list_extensions(app_dir)
    fallback = not installed and not os.path.exists(config.assets_dir)

    base_url = web_app.settings['base_url']

    theme_url = ujoin(base_url, 'lab/api/themes')
    web_app.settings.setdefault('page_config_data', dict())
    web_app.settings['page_config_data']['token'] = nbapp.token
    web_app.settings['page_config_data']['themePath'] = theme_url

    if core_mode or fallback:
        config.assets_dir = os.path.join(here, 'build')
        config.version = __version__
        if not os.path.exists(config.assets_dir):
            msg = 'Static assets not built, please see CONTRIBUTING.md'
            nbapp.log.error(msg)
        else:
            sentinel = os.path.join(here, 'build', 'release_data.json')
            config.dev_mode = not os.path.exists(sentinel)

    if config.dev_mode:
        nbapp.log.info(DEV_NOTE_NPM)
    elif core_mode or fallback:
        nbapp.log.info(CORE_NOTE.strip())

    add_handlers(web_app, config)

    user_settings_dir = get_user_settings_dir()

    if core_mode or fallback:
        schemas_dir = os.path.join(here, 'schemas')
    else:
        schemas_dir = os.path.join(app_dir, 'schemas')

    settings_url = ujoin(base_url, settings_path)
    settings_handler = (settings_url, SettingsHandler, {
        'schemas_dir': schemas_dir,
        'settings_dir': user_settings_dir
    })

    theme_handler = (ujoin(theme_url, "(.*)"), FileFindHandler, {
        'path': os.path.join(here, 'themes')
    })

    build_url = ujoin(base_url, build_path)
    builder = Builder(nbapp.log, core_mode, app_dir)
    build_handler = (build_url, BuildHandler, {'builder': builder})

    web_app.add_handlers(".*$",
                         [settings_handler, build_handler, theme_handler])
Exemplo n.º 14
0
def load_jupyter_server_extension(nbapp):
    """Load the JupyterLab server extension.
    """
    # Delay imports to speed up jlpmapp
    from json import dumps
    from jupyterlab_launcher import add_handlers, LabConfig
    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 .demo_handler import demo_build_path, DemoBuilder, DemoHandler
    from .tianchi_game_handler import tianchi_game_path, TianchiGameBuilder, TianchiGameHandler
    from .tianchi_upload_handler import tianchi_upload_path, TianchiUploadBuilder, TianchiUploadHandler
    from .tianchi_user_handler import tianchi_user_path, TianchiUserBuilder, TianchiUserHandler
    from .tianchi_game_list_handler import tianchi_game_list_path, TianchiGameListBuilder, TianchiGameListHandler
    from .progress_handler import progress_build_path, ProgressHandler, ProgressBuilder
    from .notebook_proxy import notebook_proxy_path, NotebookProxyBuilder, NotebookProxyHandler
    from .community_download_dataset_handler import community_dataset_download_path, CommunityDatasetDownloadBuilder, \
        CommunityDatasetDownloadHandler
    from .community_download_notebook_handler import community_notebook_download_path, CommunityNotebookDownloadBuilder, \
        CommunityNotebookDownloadHandler
    from .file_exists_handler import file_exists_path, FileExistBuilder, FileExistHandler
    from .community_upload_notebook_handler import community_upload_notebook_path, CommunityNotebookUploadBuilder, CommunityNotebookUploadHandler
    from .dsw.handlers import file_upload_path, FileUploadHanler, FileUploadBuilder
    from jupyterlab.extension_manager_handler import (extensions_handler_path,
                                                      ExtensionManager,
                                                      ExtensionHandler)
    from .commands import (get_app_dir, get_user_settings_dir, watch,
                           ensure_dev, watch_dev, pjoin, DEV_DIR, HERE,
                           get_app_info, ensure_core, get_workspaces_dir)
    from .git_oper_handler import git_oper_path, GitOperBuilder, GitOperHandler

    web_app = nbapp.web_app
    logger = nbapp.log
    config = LabConfig()
    app_dir = getattr(nbapp, 'app_dir', get_app_dir())
    user_settings_dir = getattr(nbapp, 'user_settings_dir',
                                get_user_settings_dir())
    workspaces_dir = getattr(nbapp, 'workspaces_dir', get_workspaces_dir())

    # Print messages.
    logger.info('JupyterLab extension loaded from %s' % HERE)
    logger.info('JupyterLab application directory is %s' % app_dir)

    config.app_name = 'JupyterLab'
    config.app_namespace = 'jupyterlab'
    config.page_url = '/lab'
    config.cache_files = True

    # Check for core mode.
    core_mode = False
    if getattr(nbapp, 'core_mode', False) or app_dir.startswith(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):
        dev_mode = True
        logger.info('Running JupyterLab in dev mode')

    # 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:
        app_dir = HERE
        logger.info(CORE_NOTE.strip())
        ensure_core(logger)

    elif dev_mode:
        app_dir = DEV_DIR
        ensure_dev(logger)
        if not watch_mode:
            logger.info(DEV_NOTE)

    config.app_settings_dir = pjoin(app_dir, 'settings')
    config.schemas_dir = pjoin(app_dir, 'schemas')
    config.themes_dir = pjoin(app_dir, 'themes')
    config.workspaces_dir = workspaces_dir
    info = get_app_info(app_dir)
    config.app_version = info['version']
    public_url = info['publicUrl']
    if public_url:
        config.public_url = public_url
    else:
        config.static_dir = app_dir
    # logger.info('public_url is %s', public_url)
    # config.static_dir = pjoin(app_dir, 'static')
    # logger.info('config.static_dir is %s', config.static_dir)

    # if dev_mode:
    config.static_dir = pjoin(app_dir, 'static')

    config.user_settings_dir = user_settings_dir

    # The templates end up in the built static directory.
    config.templates_dir = pjoin(app_dir, 'static')

    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]

    demo_url = ujoin(base_url, demo_build_path)
    demo_builder = DemoBuilder(logger, core_mode, app_dir)
    demo_handler = (demo_url, DemoHandler, {'builder': demo_builder})
    handlers.append(demo_handler)

    progress_url = ujoin(base_url, progress_build_path)
    progress_builder = ProgressBuilder(logger, core_mode, app_dir)
    progress_handler = (progress_url, ProgressHandler, {
        'builder': progress_builder
    })
    handlers.append(progress_handler)

    # tianchi_download_url = ujoin(base_url, tianchi_download_path)
    # tianchi_download_builder = TianchiDownloadBuilder(logger, core_mode, app_dir)
    # tianchi_download_handler = (tianchi_download_url, TianchiDownloadHandler, {'builder': tianchi_download_builder})
    # handlers.append(tianchi_download_handler)

    tianchi_game_url = ujoin(base_url, tianchi_game_path)
    tianchi_game_builder = TianchiGameBuilder(logger, core_mode, app_dir)
    tianchi_game_handler = (tianchi_game_url, TianchiGameHandler, {
        'builder': tianchi_game_builder
    })
    handlers.append(tianchi_game_handler)

    tianchi_upload_url = ujoin(base_url, tianchi_upload_path)
    tianchi_upload_builder = TianchiUploadBuilder(logger, core_mode, app_dir)
    tianchi_upload_handler = (tianchi_upload_url, TianchiUploadHandler, {
        'builder': tianchi_upload_builder
    })
    handlers.append(tianchi_upload_handler)

    tianchi_user_url = ujoin(base_url, tianchi_user_path)
    tianchi_user_builder = TianchiUserBuilder(logger, core_mode, app_dir)
    tianchi_user_handler = (tianchi_user_url, TianchiUserHandler, {
        'builder': tianchi_user_builder
    })
    handlers.append(tianchi_user_handler)

    tianchi_game_list_url = ujoin(base_url, tianchi_game_list_path)
    tianchi_game_list_builder = TianchiGameListBuilder(logger, core_mode,
                                                       app_dir)
    tianchi_game_list_handler = (tianchi_game_list_url, TianchiGameListHandler,
                                 {
                                     'builder': tianchi_game_list_builder
                                 })
    handlers.append(tianchi_game_list_handler)

    notebook_proxy_url = ujoin(base_url, notebook_proxy_path)
    notebook_proxy_builder = NotebookProxyBuilder(logger, core_mode, app_dir)
    notebook_proxy_handler = (notebook_proxy_url, NotebookProxyHandler, {
        'builder': notebook_proxy_builder
    })
    handlers.append(notebook_proxy_handler)

    community_dataset_download_url = ujoin(base_url,
                                           community_dataset_download_path)
    community_dataset_download_builder = CommunityDatasetDownloadBuilder(
        logger, core_mode, app_dir)
    community_dataset_download_handler = (
        community_dataset_download_url, CommunityDatasetDownloadHandler, {
            'builder': community_dataset_download_builder
        })
    handlers.append(community_dataset_download_handler)

    community_notebook_download_url = ujoin(base_url,
                                            community_notebook_download_path)
    community_notebook_download_builder = CommunityNotebookDownloadBuilder(
        logger, core_mode, app_dir)
    community_notebook_download_handler = (
        community_notebook_download_url, CommunityNotebookDownloadHandler, {
            'builder': community_notebook_download_builder
        })
    handlers.append(community_notebook_download_handler)

    file_exists_url = ujoin(base_url, file_exists_path)
    file_exists_builder = FileExistBuilder(logger, core_mode, app_dir)
    file_exists_handler = (file_exists_url, FileExistHandler, {
        'builder': file_exists_builder
    })
    handlers.append(file_exists_handler)

    community_upload_notebook_url = ujoin(base_url,
                                          community_upload_notebook_path)
    community_upload_notebook_builder = CommunityNotebookUploadBuilder(
        logger, core_mode, app_dir)
    community_upload_notebook_handler = (community_upload_notebook_url,
                                         CommunityNotebookUploadHandler, {
                                             'builder':
                                             community_upload_notebook_builder
                                         })
    handlers.append(community_upload_notebook_handler)

    # large file upload
    file_upload = ujoin(base_url, file_upload_path)
    file_upload_builder = FileUploadBuilder(logger, core_mode, app_dir)
    file_upload_handler = (file_upload, FileUploadHanler, {
        'builder': file_upload_builder
    })
    handlers.append(file_upload_handler)

    git_oper_url = ujoin(base_url, git_oper_path)
    git_oper_builder = GitOperBuilder(logger, core_mode, app_dir)
    git_oper_handler = (git_oper_url, GitOperHandler, {
        'builder': git_oper_builder
    })
    handlers.append(git_oper_handler)

    from jupyterlab.custom_file_handler import CustomFileHandler
    handlers.append((r"/files/(.*)", CustomFileHandler, {}))

    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.
    logger.info('now handlers is')
    logger.info(handlers)
    web_app.add_handlers('.*$', handlers)

    add_handlers(web_app, config)