Skip to content

Commit 9885528

Browse files
committed
hijack server browser file to land on extension homepage
1 parent cada11c commit 9885528

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

jupyter_server/extension/application.py

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,38 @@ def static_url_prefix(self):
140140
help=_("The default URL to redirect to from `/`")
141141
)
142142

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+
143175
def initialize_settings(self):
144176
"""Override this method to add handling of settings."""
145177
pass
@@ -256,15 +288,21 @@ def initialize(self, serverapp, argv=[]):
256288
self._prepare_settings()
257289
self._prepare_handlers()
258290

259-
def start(self, **kwargs):
291+
def start(self):
260292
"""Start the underlying Jupyter server.
261293
262294
Server should be started after extension is initialized.
263295
"""
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
266304
# Start the server.
267-
self.serverapp.start(**kwargs)
305+
self.serverapp.start()
268306

269307
def stop(self):
270308
"""Stop the underlying Jupyter server.

0 commit comments

Comments
 (0)