forked from jupyterlab/jupyterlab
/
extension.py
82 lines (62 loc) · 2.51 KB
/
extension.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# coding: utf-8
"""A tornado based Jupyter lab server."""
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import os
from jupyterlab_launcher import add_handlers, LabConfig
from .commands import get_app_dir, list_extensions
from ._version import __version__
#-----------------------------------------------------------------------------
# Module globals
#-----------------------------------------------------------------------------
DEV_NOTE_NPM = """You're running JupyterLab from source.
If you're working on the TypeScript sources of JupyterLab, try running
npm run watch
from the JupyterLab repo directory in another terminal window to have the
system incrementally watch and build JupyterLab's TypeScript for you, as you
make changes.
"""
CORE_NOTE = """
Running the core application with no additional extensions or settings
"""
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)