Skip to content

Commit 5342880

Browse files
authored
Write server list to stdout (#1275)
1 parent cca1ef1 commit 5342880

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

jupyter_server/serverapp.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -677,20 +677,19 @@ class JupyterServerListApp(JupyterApp):
677677

678678
def start(self):
679679
"""Start the server list application."""
680-
info = self.log.info
681680
serverinfo_list = list(list_running_servers(self.runtime_dir, log=self.log))
682681
if self.jsonlist:
683-
info(json.dumps(serverinfo_list, indent=2))
682+
print(json.dumps(serverinfo_list, indent=2))
684683
elif self.json:
685684
for serverinfo in serverinfo_list:
686-
info(json.dumps(serverinfo))
685+
print(json.dumps(serverinfo))
687686
else:
688-
info("Currently running servers:")
687+
print("Currently running servers:")
689688
for serverinfo in serverinfo_list:
690689
url = serverinfo["url"]
691690
if serverinfo.get("token"):
692691
url = url + "?token=%s" % serverinfo["token"]
693-
info("%s :: %s", url, serverinfo["root_dir"])
692+
print(url, "::", serverinfo["root_dir"])
694693

695694

696695
# -----------------------------------------------------------------------------

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ unfixable = [
246246
# S108 Probable insecure usage of temporary file or directory
247247
# PLC1901 `ext_pkg.version == ""` can be simplified to `not ext_pkg.version` as an empty string is falsey
248248
"tests/*" = ["B011", "F841", "C408", "E402", "T201", "EM101", "EM102", "EM103", "PLR2004", "S108", "PLC1901"]
249+
# print should be used in applications
250+
"**/*app.py" = ["T201"]
249251
# Ignore flake 8 errors from shimmed imports
250252
"jupyter_server/base/zmqhandlers.py" = ["F401"]
251253
# PLR2004 Magic value used in comparison

tests/test_serverapp.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import getpass
2+
import json
23
import logging
34
import os
45
import pathlib
6+
import sys
57
import warnings
68
from unittest.mock import patch
79

@@ -14,6 +16,7 @@
1416
from jupyter_server.auth.security import passwd_check
1517
from jupyter_server.serverapp import (
1618
JupyterPasswordApp,
19+
JupyterServerListApp,
1720
ServerApp,
1821
ServerWebApplication,
1922
list_running_servers,
@@ -36,6 +39,49 @@ def test_help_output():
3639
check_help_all_output("jupyter_server")
3740

3841

42+
@pytest.mark.parametrize(
43+
"format",
44+
[
45+
"json",
46+
"jsonlist",
47+
"",
48+
],
49+
)
50+
def test_server_list(jp_configurable_serverapp, capsys, format):
51+
app = jp_configurable_serverapp(log=logging.getLogger())
52+
53+
app.write_server_info_file()
54+
55+
capsys.readouterr()
56+
listapp = JupyterServerListApp(
57+
parent=app,
58+
)
59+
if format:
60+
setattr(listapp, format, True)
61+
listapp.start()
62+
captured = capsys.readouterr()
63+
sys.stdout.write(captured.out)
64+
sys.stderr.write(captured.err)
65+
out = captured.out.strip()
66+
67+
if not format:
68+
assert "Currently running servers:" in out
69+
assert app.connection_url in out
70+
assert len(out.splitlines()) == 2
71+
return
72+
73+
if format == "jsonlist":
74+
servers = json.loads(out)
75+
elif format == "json":
76+
servers = [json.loads(line) for line in out.splitlines()]
77+
assert len(servers) == 1
78+
sinfo = servers[0]
79+
80+
assert sinfo["port"] == app.port
81+
assert sinfo["url"] == app.connection_url
82+
assert sinfo["version"] == app.version
83+
84+
3985
def test_server_info_file(tmp_path, jp_configurable_serverapp):
4086
app = jp_configurable_serverapp(log=logging.getLogger())
4187

0 commit comments

Comments
 (0)