forked from jupyterlab/jupyterlab_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.py
321 lines (253 loc) · 11.2 KB
/
handlers.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# coding: utf-8
"""JupyterLab Server handlers"""
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import json
import os
from urllib.parse import urlparse
from tornado import web, template
from jinja2 import FileSystemLoader, TemplateError
from traitlets import HasTraits, Bool, Unicode, default
from jupyter_server.extension.handler import ExtensionHandlerMixin, ExtensionHandlerJinjaMixin
from .server import JupyterHandler, FileFindHandler, url_path_join as ujoin
from .workspaces_handler import WorkspacesHandler
from .settings_handler import SettingsHandler
from .themes_handler import ThemesHandler
# -----------------------------------------------------------------------------
# Module globals
# -----------------------------------------------------------------------------
DEFAULT_TEMPLATE = template.Template("""
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<h2>Cannot find template: "{{name}}"</h2>
<p>In "{{path}}"</p>
</body>
</html>
""")
def is_url(url):
"""Test whether a string is a full url (e.g. https://nasa.gov)
https://stackoverflow.com/a/52455972
"""
try:
result = urlparse(url)
return all([result.scheme, result.netloc])
except ValueError:
return False
class LabHandler(ExtensionHandlerMixin, ExtensionHandlerJinjaMixin, JupyterHandler):
"""Render the JupyterLab View."""
def initialize(self, lab_config, **kwargs):
super().initialize(kwargs['extension_name'])
self.lab_config = lab_config
self.file_loader = FileSystemLoader(lab_config.templates_dir)
@web.authenticated
@web.removeslash
def get(self):
self.application.store_id = getattr(self.application, 'store_id', 0)
config = self.lab_config
settings_dir = config.app_settings_dir
# Handle page config data.
page_config = self.settings.setdefault('page_config_data', {})
terminals = self.settings.get('terminals_available', False)
server_root = self.settings.get('server_root_dir', '')
server_root = server_root.replace(os.sep, '/')
base_url = self.settings.get('base_url')
page_config.setdefault('terminalsAvailable', terminals)
page_config.setdefault('ignorePlugins', [])
page_config.setdefault('serverRoot', server_root)
page_config['store_id'] = self.application.store_id
self.application.store_id += 1
mathjax_config = self.settings.get('mathjax_config',
'TeX-AMS_HTML-full,Safe')
page_config.setdefault('mathjaxConfig', mathjax_config)
page_config.setdefault('fullMathjaxUrl', self.mathjax_url)
# Put all our config in page_config
for name in config.trait_names():
page_config[_camelCase(name)] = getattr(config, name)
# Add full versions of all the urls
for name in config.trait_names():
if not name.endswith('_url'):
continue
full_name = _camelCase('full_' + name)
full_url = ujoin(base_url, getattr(config, name))
page_config[full_name] = full_url
# Load the current page config file if available.
page_config_file = os.path.join(settings_dir, 'page_config.json')
if os.path.exists(page_config_file):
with open(page_config_file) as fid:
try:
page_config.update(json.load(fid))
except Exception as e:
print(e)
# Write the template with the config.
self.write(self.render_lab_template('index.html', page_config=page_config))
# def get_template(self, name):
# return self.file_loader.load(self.settings['jinja2_env'], name)
def render_lab_template(self, name, **ns):
try:
return JupyterHandler.render_template(self, name, **ns)
except TemplateError:
return DEFAULT_TEMPLATE.generate(
name=name, path=self.lab_config.templates_dir
)
class LabConfig(HasTraits):
"""The lab application configuration object.
"""
app_name = Unicode('', help='The name of the application.')
app_version = Unicode('', help='The version of the application.')
app_namespace = Unicode('', help='The namespace of the application.')
app_url = Unicode('/lab', help='The url path for the application.')
app_settings_dir = Unicode('', help='The application settings directory.')
templates_dir = Unicode('', help='The application templates directory.')
static_dir = Unicode('',
help=('The optional location of local static files. '
'If given, a static file handler will be '
'added.'))
static_url = Unicode(help=('The url path for static application '
'assets. This can be a CDN if desired.'))
settings_url = Unicode(help='The url path of the settings handler.')
user_settings_dir = Unicode('',
help=('The optional location of the user '
'settings directory.'))
schemas_dir = Unicode('',
help=('The optional location of the settings '
'schemas directory. If given, a handler will '
'be added for settings.'))
workspaces_api_url = Unicode(help='The url path of the workspaces API.')
workspaces_dir = Unicode('',
help=('The optional location of the saved '
'workspaces directory. If given, a handler '
'will be added for workspaces.'))
workspaces_url = Unicode(help='The url path of the workspaces handler.')
themes_url = Unicode(help='The theme url.')
themes_dir = Unicode('',
help=('The optional location of the themes '
'directory. If given, a handler will be added '
'for themes.'))
tree_url = Unicode(help='The url path of the tree handler.')
cache_files = Bool(True,
help=('Whether to cache files on the server. '
'This should be `True` except in dev mode.'))
@default('static_url')
def _default_static_url(self):
return ujoin('static/', self.app_url)
@default('workspaces_url')
def _default_workspaces_url(self):
return ujoin(self.app_url, 'workspaces/')
@default('workspaces_api_url')
def _default_workspaces_api_url(self):
return ujoin(self.app_url, 'api', 'workspaces/')
@default('settings_url')
def _default_settings_url(self):
return ujoin(self.app_url, 'api', 'settings/')
@default('themes_url')
def _default_themes_url(self):
return ujoin(self.app_url, 'api', 'themes/')
@default('tree_url')
def _default_tree_url(self):
return ujoin(self.app_url, 'tree/')
class NotFoundHandler(LabHandler):
def render_template(self, name, **ns):
if 'page_config' in ns:
ns['page_config'] = ns['page_config'].copy()
ns['page_config']['notFoundUrl'] = self.request.path
return self.render_lab_template(name, **ns)
def add_handlers(labserverapp, web_app, config):
"""Add the appropriate handlers to the web app.
"""
# Normalize directories.
for name in config.trait_names():
if not name.endswith('_dir'):
continue
value = getattr(config, name)
setattr(config, name, value.replace(os.sep, '/'))
# Normalize urls
# Local urls should have a leading slash but no trailing slash
for name in config.trait_names():
if not name.endswith('_url'):
continue
value = getattr(config, name)
if is_url(value):
continue
if not value.startswith('/'):
value = '/' + value
if value.endswith('/'):
value = value[:-1]
setattr(config, name, value)
# Set up the main page handler and tree handler.
base_url = web_app.settings['base_url']
lab_path = ujoin(base_url, config.app_url)
tree_path = ujoin(base_url, config.tree_url, r'.+')
handlers = [
(lab_path, LabHandler, {'lab_config': config}),
(tree_path, LabHandler, {'lab_config': config})
]
# Cache all or none of the files depending on the `cache_files` setting.
no_cache_paths = [] if config.cache_files else ['/']
# Handle local static assets.
if config.static_dir:
static_path = ujoin(base_url, config.static_url, '(.*)')
handlers.append((static_path, FileFindHandler, {
'path': config.static_dir,
'no_cache_paths': no_cache_paths
}))
# Handle local settings.
if config.schemas_dir:
settings_config = {
'app_settings_dir': config.app_settings_dir,
'schemas_dir': config.schemas_dir,
'settings_dir': config.user_settings_dir
}
# Handle requests for the list of settings. Make slash optional.
settings_path = ujoin(base_url, config.settings_url, '?')
handlers.append((settings_path, SettingsHandler, settings_config))
# Handle requests for an individual set of settings.
setting_path = ujoin(
base_url, config.settings_url, '(?P<schema_name>.+)')
handlers.append((setting_path, SettingsHandler, settings_config))
# Handle saved workspaces.
if config.workspaces_dir:
# Handle JupyterLab client URLs that include workspaces.
workspaces_path = ujoin(base_url, config.workspaces_url, r'.+')
handlers.append((workspaces_path, LabHandler, {'lab_config': config}))
workspaces_config = {
'workspaces_url': config.workspaces_url,
'path': config.workspaces_dir
}
# Handle requests for the list of workspaces. Make slash optional.
workspaces_api_path = ujoin(base_url, config.workspaces_api_url, '?')
handlers.append((
workspaces_api_path, WorkspacesHandler, workspaces_config))
# Handle requests for an individually named workspace.
workspace_api_path = ujoin(
base_url, config.workspaces_api_url, '(?P<space_name>.+)')
handlers.append((
workspace_api_path, WorkspacesHandler, workspaces_config))
# Handle local themes.
if config.themes_dir:
themes_url = ujoin(base_url, config.themes_url)
themes_path = ujoin(themes_url, '(.*)')
handlers.append((
themes_path,
ThemesHandler,
{
'themes_url': themes_url,
'path': config.themes_dir,
'no_cache_paths': no_cache_paths
}
))
# Let the lab handler act as the fallthrough option instead of a 404.
fallthrough_url = ujoin(base_url, config.app_url, r'.*')
handlers.append((fallthrough_url, NotFoundHandler, {'lab_config': config}))
# web_app.add_handlers('.*$', handlers)
labserverapp.handlers.extend(handlers)
def _camelCase(base):
"""Convert a string to camelCase.
https://stackoverflow.com/a/20744956
"""
output = ''.join(x for x in base.title() if x.isalpha())
return output[0].lower() + output[1:]