Skip to content

Write server list to stdout #1275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions jupyter_server/serverapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,20 +677,19 @@ class JupyterServerListApp(JupyterApp):

def start(self):
"""Start the server list application."""
info = self.log.info
serverinfo_list = list(list_running_servers(self.runtime_dir, log=self.log))
if self.jsonlist:
info(json.dumps(serverinfo_list, indent=2))
print(json.dumps(serverinfo_list, indent=2))
elif self.json:
for serverinfo in serverinfo_list:
info(json.dumps(serverinfo))
print(json.dumps(serverinfo))
else:
info("Currently running servers:")
print("Currently running servers:")
for serverinfo in serverinfo_list:
url = serverinfo["url"]
if serverinfo.get("token"):
url = url + "?token=%s" % serverinfo["token"]
info("%s :: %s", url, serverinfo["root_dir"])
print(url, "::", serverinfo["root_dir"])


# -----------------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ unfixable = [
# S108 Probable insecure usage of temporary file or directory
# PLC1901 `ext_pkg.version == ""` can be simplified to `not ext_pkg.version` as an empty string is falsey
"tests/*" = ["B011", "F841", "C408", "E402", "T201", "EM101", "EM102", "EM103", "PLR2004", "S108", "PLC1901"]
# print should be used in applications
"**/*app.py" = ["T201"]
# Ignore flake 8 errors from shimmed imports
"jupyter_server/base/zmqhandlers.py" = ["F401"]
# PLR2004 Magic value used in comparison
Expand Down
46 changes: 46 additions & 0 deletions tests/test_serverapp.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import getpass
import json
import logging
import os
import pathlib
import sys
import warnings
from unittest.mock import patch

Expand All @@ -14,6 +16,7 @@
from jupyter_server.auth.security import passwd_check
from jupyter_server.serverapp import (
JupyterPasswordApp,
JupyterServerListApp,
ServerApp,
ServerWebApplication,
list_running_servers,
Expand All @@ -36,6 +39,49 @@ def test_help_output():
check_help_all_output("jupyter_server")


@pytest.mark.parametrize(
"format",
[
"json",
"jsonlist",
"",
],
)
def test_server_list(jp_configurable_serverapp, capsys, format):
app = jp_configurable_serverapp(log=logging.getLogger())

app.write_server_info_file()

capsys.readouterr()
listapp = JupyterServerListApp(
parent=app,
)
if format:
setattr(listapp, format, True)
listapp.start()
captured = capsys.readouterr()
sys.stdout.write(captured.out)
sys.stderr.write(captured.err)
out = captured.out.strip()

if not format:
assert "Currently running servers:" in out
assert app.connection_url in out
assert len(out.splitlines()) == 2
return

if format == "jsonlist":
servers = json.loads(out)
elif format == "json":
servers = [json.loads(line) for line in out.splitlines()]
assert len(servers) == 1
sinfo = servers[0]

assert sinfo["port"] == app.port
assert sinfo["url"] == app.connection_url
assert sinfo["version"] == app.version


def test_server_info_file(tmp_path, jp_configurable_serverapp):
app = jp_configurable_serverapp(log=logging.getLogger())

Expand Down