Skip to content

Commit

Permalink
write server list to stdout
Browse files Browse the repository at this point in the history
instead of logging, which goes to stderr and is often prefixed and/or filtered by log levels

already fixed upstream in notebook 6
  • Loading branch information
minrk committed May 12, 2023
1 parent 35ffe5d commit 90e2923
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
14 changes: 7 additions & 7 deletions jupyter_server/serverapp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""A tornado based Jupyter server."""
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

import datetime
import errno
import gettext
Expand Down Expand Up @@ -677,20 +678,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))
elif self.json:
if self.json:
print(json.dumps(serverinfo_list, indent=2))
elif self.jsonlist:
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"]
# Ignore print 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
50 changes: 50 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,53 @@ 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()
# cmd = [sys.executable, "-m", "jupyter_server", "list"]
# if format:
# cmd.append(format)
# p = run(cmd, capture_output=True, text=True)
# out = p.stdout.strip()
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 == "json":
servers = json.loads(out)
elif format == "jsonlist":
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

0 comments on commit 90e2923

Please sign in to comment.