Skip to content

Commit 5f763c6

Browse files
authored
add page_config_hook (#220)
1 parent d577120 commit 5f763c6

File tree

1 file changed

+36
-22
lines changed

1 file changed

+36
-22
lines changed

jupyterlab_server/handlers.py

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# Distributed under the terms of the Modified BSD License.
66
import os
77
from urllib.parse import urlparse
8+
from functools import lru_cache
89

910
from tornado import template, web
1011

@@ -57,26 +58,21 @@ def is_url(url):
5758
class LabHandler(ExtensionHandlerJinjaMixin, ExtensionHandlerMixin, JupyterHandler):
5859
"""Render the JupyterLab View."""
5960

60-
@web.authenticated
61-
@web.removeslash
62-
def get(self, mode = None, workspace = None, tree = None):
63-
"""Get the JupyterLab html page."""
64-
workspace = 'default' if workspace is None else workspace.replace('/workspaces/','')
65-
tree_path = '' if tree is None else tree.replace('/tree/','')
66-
61+
@lru_cache()
62+
def get_page_config(self):
63+
"""Construct the page config object"""
6764
self.application.store_id = getattr(self.application, 'store_id', 0)
6865
config = LabConfig()
6966
app = self.extensionapp
7067
settings_dir = app.app_settings_dir
71-
7268
# Handle page config data.
7369
page_config = self.settings.setdefault('page_config_data', {})
7470
terminals = self.settings.get('terminals_available', False)
7571
server_root = self.settings.get('server_root_dir', '')
7672
server_root = server_root.replace(os.sep, '/')
7773
base_url = self.settings.get('base_url')
7874

79-
# Remove the trailing slash for compatibiity with html-webpack-plugin.
75+
# Remove the trailing slash for compatibility with html-webpack-plugin.
8076
full_static_url = self.static_url_prefix.rstrip('/')
8177
page_config.setdefault('fullStaticUrl', full_static_url)
8278

@@ -105,14 +101,6 @@ def get(self, mode = None, workspace = None, tree = None):
105101
page_config.setdefault('mathjaxConfig', mathjax_config)
106102
page_config.setdefault('fullMathjaxUrl', mathjax_url)
107103

108-
# Add parameters parsed from the URL
109-
if mode == 'doc':
110-
page_config['mode'] = 'single-document'
111-
else:
112-
page_config['mode'] = 'multiple-document'
113-
page_config['workspace'] = workspace
114-
page_config['treePath'] = tree_path
115-
116104
# Put all our config in page_config
117105
for name in config.trait_names():
118106
page_config[_camelCase(name)] = getattr(app, name)
@@ -132,17 +120,43 @@ def get(self, mode = None, workspace = None, tree = None):
132120
labextensions_path = app.extra_labextensions_path + app.labextensions_path
133121
recursive_update(page_config, get_page_config(labextensions_path, settings_dir, logger=self.log))
134122

123+
# modify page config with custom hook
124+
page_config_hook = self.settings.get("page_config_hook", None)
125+
if page_config_hook:
126+
page_config = page_config_hook(self, page_config)
127+
128+
return page_config
129+
130+
@web.authenticated
131+
@web.removeslash
132+
def get(self, mode=None, workspace=None, tree=None):
133+
"""Get the JupyterLab html page."""
134+
workspace = (
135+
"default" if workspace is None else workspace.replace("/workspaces/", "")
136+
)
137+
tree_path = "" if tree is None else tree.replace("/tree/", "")
138+
139+
page_config = self.get_page_config()
140+
141+
# Add parameters parsed from the URL
142+
if mode == "doc":
143+
page_config["mode"] = "single-document"
144+
else:
145+
page_config["mode"] = "multiple-document"
146+
page_config["workspace"] = workspace
147+
page_config["treePath"] = tree_path
148+
135149
# Write the template with the config.
136150
tpl = self.render_template('index.html', page_config=page_config)
137151
self.write(tpl)
138152

139153

140154
class NotFoundHandler(LabHandler):
141-
def render_template(self, name, **ns):
142-
if 'page_config' in ns:
143-
ns['page_config'] = ns['page_config'].copy()
144-
ns['page_config']['notFoundUrl'] = self.request.path
145-
return super().render_template(name, **ns)
155+
@lru_cache()
156+
def get_page_config(self):
157+
page_config = super().get_page_config()
158+
page_config["notFoundUrl"] = self.request.path
159+
return page_config
146160

147161

148162
def add_handlers(handlers, extension_app):

0 commit comments

Comments
 (0)