Browse Source

Merge pull request #2076 from blink1073/use-launcher

Use jupyterlab-launcher
Afshin Darian 8 years ago
parent
commit
989305f2aa
4 changed files with 35 additions and 90 deletions
  1. 23 90
      jupyterlab/extension.py
  2. 3 0
      jupyterlab/labextensions.py
  3. 8 0
      jupyterlab/tests/test_jupyterlab.py
  4. 1 0
      setup.py

+ 23 - 90
jupyterlab/extension.py

@@ -3,13 +3,11 @@
 
 # Copyright (c) Jupyter Development Team.
 # Distributed under the terms of the Modified BSD License.
-import json
 import os
-from tornado import web
 
-from notebook.base.handlers import IPythonHandler, FileFindHandler
 from jinja2 import FileSystemLoader
-from notebook.utils import url_path_join as ujoin
+from jupyterlab_launcher import add_handlers
+
 from .commands import _get_build_dir, _get_config, _get_config_dir
 
 
@@ -32,91 +30,6 @@ FILE_LOADER = FileSystemLoader(HERE)
 PREFIX = '/lab'
 
 
-class LabHandler(IPythonHandler):
-    """Render the JupyterLab View."""
-
-    def initialize(self, page_config_data, built_files):
-        self.page_config_data = page_config_data
-        self.built_files = built_files
-
-    @web.authenticated
-    def get(self):
-        config = self._get_lab_config()
-        self.write(self.render_template('lab.html', **config))
-
-    def _get_lab_config(self):
-        """Get the config data for the page template."""
-        static_prefix = ujoin(self.base_url, PREFIX)
-
-        bundles = [ujoin(static_prefix, name + '.bundle.js') for name in
-                   ['main']]
-
-        # Only load CSS files if they exist.
-        css_files = []
-        for css_file in ['main.css']:
-            if os.path.isfile(os.path.join(self.built_files, css_file)):
-                css_files.append(ujoin(static_prefix, css_file))
-
-        configData = dict(self.page_config_data)
-        configData.setdefault('ignorePlugins', [])
-        configData.update(dict(
-            terminalsAvailable=self.settings.get('terminals_available', False),
-        ))
-
-        mathjax_config = self.settings.get('mathjax_config',
-                                           'TeX-AMS_HTML-full,Safe')
-        config = dict(
-            static_prefix=static_prefix,
-            page_title='JupyterLab Alpha Preview',
-            mathjax_url=self.mathjax_url,
-            mathjax_config=mathjax_config,
-            jupyterlab_css=css_files,
-            jupyterlab_bundles=bundles
-        )
-        config['jupyterlab_config'] = configData
-        return config
-
-    def get_template(self, name):
-        return FILE_LOADER.load(self.settings['jinja2_env'], name)
-
-
-def add_handlers(app):
-    """Add the appropriate handlers to the web app.
-    """
-    web_app = app.web_app
-    base_url = web_app.settings['base_url']
-    prefix = ujoin(base_url, PREFIX)
-
-    # Handle page config data.
-    config = _get_config()
-    page_config_data = web_app.settings.get('page_config_data', {})
-    page_config_file = os.path.join(_get_config_dir(), 'page_config_data.json')
-    if os.path.exists(page_config_file):
-        with open(page_config_file) as fid:
-            page_config_data.update(json.load(fid))
-
-    built_files = _get_build_dir(config)
-
-    # Check for dev mode.
-    dev_mode = False
-    if hasattr(app, 'dev_mode'):
-        dev_mode = app.dev_mode
-
-    if not os.path.exists(built_files) or dev_mode:
-        built_files = os.path.join(HERE, 'build')
-
-    handlers = [
-        (prefix + r'/?', LabHandler, {
-            'page_config_data': page_config_data,
-            'built_files': built_files
-        }),
-        (prefix + r"/(.*)", FileFindHandler, {
-            'path': built_files
-        })
-    ]
-    web_app.add_handlers(".*$", handlers)
-
-
 def load_jupyter_server_extension(nbapp):
     """Load the JupyterLab server extension.
     """
@@ -127,4 +40,24 @@ def load_jupyter_server_extension(nbapp):
     if dev_mode:
         nbapp.log.info(DEV_NOTE_NPM)
 
-    add_handlers(nbapp)
+    web_app = nbapp.web_app
+
+    # Handle page config data.
+    config_dir = _get_config_dir()
+    build_config = _get_config()
+    page_config_file = os.path.join(config_dir, 'page_config_data.json')
+    build_dir = _get_build_dir(build_config)
+
+    # Check for dev mode.
+    dev_mode = False
+    if hasattr(nbapp, 'dev_mode'):
+        dev_mode = nbapp.dev_mode
+
+    if not os.path.exists(build_dir) or dev_mode:
+        print('Serving local JupyterLab files')
+        build_dir = os.path.join(HERE, 'build')
+
+    add_handlers(
+        web_app, page_config_file, build_dir, 'JupyterLab Alpha Preview',
+        PREFIX
+    )

+ 3 - 0
jupyterlab/labextensions.py

@@ -27,6 +27,9 @@ class BaseExtensionApp(JupyterApp):
     version = __version__
     flags = flags
 
+    lab_config_dir = Unicode(ENV_CONFIG_PATH[0], config=True,
+        help="The lab configuration directory")
+
     should_build = Bool(True, config=True,
         help="Whether to build the app after the action")
 

+ 8 - 0
jupyterlab/tests/test_jupyterlab.py

@@ -84,6 +84,12 @@ class TestExtension(TestCase):
             if hasattr(mod, 'ENV_CONFIG_PATH'):
                 p = patch.object(mod, 'ENV_CONFIG_PATH', [self.config_dir])
                 self.patches.append(p)
+            if hasattr(mod, 'CONFIG_PATH'):
+                p = patch.object(mod, 'CONFIG_PATH', self.config_dir)
+                self.patches.append(p)
+            if hasattr(mod, 'BUILD_PATH'):
+                p = patch.object(mod, 'BUILD_PATH', self.data_dir)
+                self.patches.append(p)
         for p in self.patches:
             p.start()
             self.addCleanup(p.stop)
@@ -93,6 +99,8 @@ class TestExtension(TestCase):
         self.assertEqual(paths.ENV_JUPYTER_PATH, [self.data_dir])
         self.assertEqual(commands.ENV_JUPYTER_PATH, [self.data_dir])
         self.assertEqual(commands.ENV_CONFIG_PATH, [self.config_dir])
+        self.assertEqual(commands.CONFIG_PATH, self.config_dir)
+        self.assertEqual(commands.BUILD_PATH, self.data_dir)
 
     def tearDown(self):
         for modulename in self._mock_extensions:

+ 1 - 0
setup.py

@@ -114,6 +114,7 @@ setup_args['cmdclass'] = cmdclass
 setuptools_args = {}
 install_requires = setuptools_args['install_requires'] = [
     'notebook>=4.2.0',
+    'jupyterlab_launcher>=0.1.1'
 ]
 
 extras_require = setuptools_args['extras_require'] = {