@@ -140,6 +140,38 @@ def static_url_prefix(self):
140
140
help = _ ("The default URL to redirect to from `/`" )
141
141
)
142
142
143
+ custom_display_url = Unicode (u'' , config = True ,
144
+ help = _ ("""Override URL shown to users.
145
+
146
+ Replace actual URL, including protocol, address, port and base URL,
147
+ with the given value when displaying URL to the users. Do not change
148
+ the actual connection URL. If authentication token is enabled, the
149
+ token is added to the custom URL automatically.
150
+
151
+ This option is intended to be used when the URL to display to the user
152
+ cannot be determined reliably by the Jupyter server (proxified
153
+ or containerized setups for example).""" )
154
+ )
155
+
156
+ @default ('custom_display_url' )
157
+ def _default_custom_display_url (self ):
158
+ """URL to display to the user."""
159
+ # Get url from server.
160
+ url = url_path_join (self .serverapp .base_url , self .default_url )
161
+ return self .serverapp .get_url (self .serverapp .ip , url )
162
+
163
+ def _write_browser_open_file (self , url , fh ):
164
+ """Use to hijacks the server's browser-open file and open at
165
+ the extension's homepage.
166
+ """
167
+ # Ignore server's url
168
+ del url
169
+ path = url_path_join (self .serverapp .base_url , self .default_url )
170
+ url = self .serverapp .get_url (path = path )
171
+ jinja2_env = self .serverapp .web_app .settings ['jinja2_env' ]
172
+ template = jinja2_env .get_template ('browser-open.html' )
173
+ fh .write (template .render (open_url = url ))
174
+
143
175
def initialize_settings (self ):
144
176
"""Override this method to add handling of settings."""
145
177
pass
@@ -256,15 +288,21 @@ def initialize(self, serverapp, argv=[]):
256
288
self ._prepare_settings ()
257
289
self ._prepare_handlers ()
258
290
259
- def start (self , ** kwargs ):
291
+ def start (self ):
260
292
"""Start the underlying Jupyter server.
261
293
262
294
Server should be started after extension is initialized.
263
295
"""
264
- # Start the browser at this extensions default_url.
265
- self .serverapp .default_url = self .default_url
296
+ # Override the browser open file to
297
+ # Override the server's display url to show extension's display URL.
298
+ self .serverapp .custom_display_url = self .custom_display_url
299
+ # Override the server's default option and open a broswer window.
300
+ self .serverapp .open_browser = True
301
+ # Hijack the server's browser-open file to land on
302
+ # the extensions home page.
303
+ self .serverapp ._write_browser_open_file = self ._write_browser_open_file
266
304
# Start the server.
267
- self .serverapp .start (** kwargs )
305
+ self .serverapp .start ()
268
306
269
307
def stop (self ):
270
308
"""Stop the underlying Jupyter server.
0 commit comments