From 230de3bfab895b6c2f64f8e414f25559c7ebfd26 Mon Sep 17 00:00:00 2001 From: Nicholas Bollweg Date: Tue, 20 Dec 2022 23:00:09 -0600 Subject: [PATCH] add no_browser_open_file --- jupyter_server/serverapp.py | 35 +++++++++++++++++++++++++---------- tests/test_serverapp.py | 10 ++++++++++ 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/jupyter_server/serverapp.py b/jupyter_server/serverapp.py index 48d7043cc6..ba64b694ec 100644 --- a/jupyter_server/serverapp.py +++ b/jupyter_server/serverapp.py @@ -1573,6 +1573,10 @@ def _default_info_file(self): info_file = "jpserver-%s.json" % os.getpid() return os.path.join(self.runtime_dir, info_file) + no_browser_open_file = Bool( + False, help="If True, do not write redirect HTML file disk, or show in messages." + ) + browser_open_file = Unicode() @default("browser_open_file") @@ -2815,9 +2819,11 @@ def start_app(self): self.write_server_info_file() + if not self.no_browser_open_file: + self.write_browser_open_files() + # Handle the browser opening. if self.open_browser and not self.sock: - self.write_browser_open_files() self.launch_browser() if self.identity_provider.token and self.identity_provider.token_generated: @@ -2840,17 +2846,26 @@ def start_app(self): ) ) else: - self.log.critical( - "\n".join( - [ - "\n", + if self.no_browser_open_file: + message = [ + "\n", + _i18n("To access the server, copy and paste one of these URLs:"), + " %s" % self.display_url, + ] + else: + message = [ + "\n", + _i18n( "To access the server, open this file in a browser:", - " %s" % urljoin("file:", pathname2url(self.browser_open_file)), + ), + " %s" % urljoin("file:", pathname2url(self.browser_open_file)), + _i18n( "Or copy and paste one of these URLs:", - " %s" % self.display_url, - ] - ) - ) + ), + " %s" % self.display_url, + ] + + self.log.critical("\n".join(message)) async def _cleanup(self): """General cleanup of files, extensions and kernels created diff --git a/tests/test_serverapp.py b/tests/test_serverapp.py index 7a2ba78784..a03d20450d 100644 --- a/tests/test_serverapp.py +++ b/tests/test_serverapp.py @@ -18,6 +18,7 @@ list_running_servers, random_ports, ) +from jupyter_server.utils import pathname2url, urljoin def test_help_output(): @@ -484,3 +485,12 @@ async def test_shutdown_no_activity(jp_serverapp): def test_running_server_info(jp_serverapp): app: ServerApp = jp_serverapp app.running_server_info(True) + + +@pytest.mark.parametrize("should_exist", [True, False]) +def test_browser_open_files(jp_configurable_serverapp, should_exist, caplog): + app = jp_configurable_serverapp(no_browser_open_file=not should_exist) + assert os.path.exists(app.browser_open_file) == should_exist + url = urljoin("file:", pathname2url(app.browser_open_file)) + url_messages = [rec.message for rec in caplog.records if url in rec.message] + assert url_messages if should_exist else not url_messages