5
5
# Distributed under the terms of the Modified BSD License.
6
6
import os
7
7
from urllib .parse import urlparse
8
+ from functools import lru_cache
8
9
9
10
from tornado import template , web
10
11
@@ -57,26 +58,21 @@ def is_url(url):
57
58
class LabHandler (ExtensionHandlerJinjaMixin , ExtensionHandlerMixin , JupyterHandler ):
58
59
"""Render the JupyterLab View."""
59
60
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"""
67
64
self .application .store_id = getattr (self .application , 'store_id' , 0 )
68
65
config = LabConfig ()
69
66
app = self .extensionapp
70
67
settings_dir = app .app_settings_dir
71
-
72
68
# Handle page config data.
73
69
page_config = self .settings .setdefault ('page_config_data' , {})
74
70
terminals = self .settings .get ('terminals_available' , False )
75
71
server_root = self .settings .get ('server_root_dir' , '' )
76
72
server_root = server_root .replace (os .sep , '/' )
77
73
base_url = self .settings .get ('base_url' )
78
74
79
- # Remove the trailing slash for compatibiity with html-webpack-plugin.
75
+ # Remove the trailing slash for compatibility with html-webpack-plugin.
80
76
full_static_url = self .static_url_prefix .rstrip ('/' )
81
77
page_config .setdefault ('fullStaticUrl' , full_static_url )
82
78
@@ -105,14 +101,6 @@ def get(self, mode = None, workspace = None, tree = None):
105
101
page_config .setdefault ('mathjaxConfig' , mathjax_config )
106
102
page_config .setdefault ('fullMathjaxUrl' , mathjax_url )
107
103
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
-
116
104
# Put all our config in page_config
117
105
for name in config .trait_names ():
118
106
page_config [_camelCase (name )] = getattr (app , name )
@@ -132,17 +120,43 @@ def get(self, mode = None, workspace = None, tree = None):
132
120
labextensions_path = app .extra_labextensions_path + app .labextensions_path
133
121
recursive_update (page_config , get_page_config (labextensions_path , settings_dir , logger = self .log ))
134
122
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
+
135
149
# Write the template with the config.
136
150
tpl = self .render_template ('index.html' , page_config = page_config )
137
151
self .write (tpl )
138
152
139
153
140
154
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
146
160
147
161
148
162
def add_handlers (handlers , extension_app ):
0 commit comments