Skip to content

Commit 230de3b

Browse files
committed
add no_browser_open_file
1 parent 1c9c943 commit 230de3b

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

jupyter_server/serverapp.py

+25-10
Original file line numberDiff line numberDiff line change
@@ -1573,6 +1573,10 @@ def _default_info_file(self):
15731573
info_file = "jpserver-%s.json" % os.getpid()
15741574
return os.path.join(self.runtime_dir, info_file)
15751575

1576+
no_browser_open_file = Bool(
1577+
False, help="If True, do not write redirect HTML file disk, or show in messages."
1578+
)
1579+
15761580
browser_open_file = Unicode()
15771581

15781582
@default("browser_open_file")
@@ -2815,9 +2819,11 @@ def start_app(self):
28152819

28162820
self.write_server_info_file()
28172821

2822+
if not self.no_browser_open_file:
2823+
self.write_browser_open_files()
2824+
28182825
# Handle the browser opening.
28192826
if self.open_browser and not self.sock:
2820-
self.write_browser_open_files()
28212827
self.launch_browser()
28222828

28232829
if self.identity_provider.token and self.identity_provider.token_generated:
@@ -2840,17 +2846,26 @@ def start_app(self):
28402846
)
28412847
)
28422848
else:
2843-
self.log.critical(
2844-
"\n".join(
2845-
[
2846-
"\n",
2849+
if self.no_browser_open_file:
2850+
message = [
2851+
"\n",
2852+
_i18n("To access the server, copy and paste one of these URLs:"),
2853+
" %s" % self.display_url,
2854+
]
2855+
else:
2856+
message = [
2857+
"\n",
2858+
_i18n(
28472859
"To access the server, open this file in a browser:",
2848-
" %s" % urljoin("file:", pathname2url(self.browser_open_file)),
2860+
),
2861+
" %s" % urljoin("file:", pathname2url(self.browser_open_file)),
2862+
_i18n(
28492863
"Or copy and paste one of these URLs:",
2850-
" %s" % self.display_url,
2851-
]
2852-
)
2853-
)
2864+
),
2865+
" %s" % self.display_url,
2866+
]
2867+
2868+
self.log.critical("\n".join(message))
28542869

28552870
async def _cleanup(self):
28562871
"""General cleanup of files, extensions and kernels created

tests/test_serverapp.py

+10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
list_running_servers,
1919
random_ports,
2020
)
21+
from jupyter_server.utils import pathname2url, urljoin
2122

2223

2324
def test_help_output():
@@ -484,3 +485,12 @@ async def test_shutdown_no_activity(jp_serverapp):
484485
def test_running_server_info(jp_serverapp):
485486
app: ServerApp = jp_serverapp
486487
app.running_server_info(True)
488+
489+
490+
@pytest.mark.parametrize("should_exist", [True, False])
491+
def test_browser_open_files(jp_configurable_serverapp, should_exist, caplog):
492+
app = jp_configurable_serverapp(no_browser_open_file=not should_exist)
493+
assert os.path.exists(app.browser_open_file) == should_exist
494+
url = urljoin("file:", pathname2url(app.browser_open_file))
495+
url_messages = [rec.message for rec in caplog.records if url in rec.message]
496+
assert url_messages if should_exist else not url_messages

0 commit comments

Comments
 (0)