Skip to content
This repository was archived by the owner on Aug 11, 2020. It is now read-only.

Commit 9f2a9ea

Browse files
BartoszCkidandruszak
authored andcommitted
Paginate experiments list when table len exceeds height of terminal
1 parent 0d77e7f commit 9f2a9ea

File tree

5 files changed

+49
-9
lines changed

5 files changed

+49
-9
lines changed

paperspace/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ def put(self, url):
2929
logger.debug("Response content: {}".format(response.content))
3030
return response
3131

32-
def get(self, url):
32+
def get(self, url, params=None):
3333
path = self.get_path(url)
3434
logger.debug("Sending GET to {}\nwith headers: {}".format(path, self.headers))
35-
response = requests.get(path, headers=self.headers)
35+
response = requests.get(path, params=params, headers=self.headers)
3636
logger.debug("Response status code: {}".format(response.status_code))
3737
logger.debug("Response content: {}".format(response.content))
3838
return response

paperspace/commands.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import pydoc
2+
13
import terminaltables
24

35
from paperspace import version, logger, constants, client
46
from paperspace.config import config
7+
from paperspace.utils import get_terminal_lines
58

69
default_headers = {"X-API-Key": config.PAPERSPACE_API_KEY,
710
"ps_client_name": "paperspace-python",
@@ -76,7 +79,8 @@ def _make_experiments_list_table(experiments):
7679

7780

7881
def list_experiments(api=experiments_api):
79-
response = api.get("/experiments/")
82+
# TODO: change to limit: -1 when PS-9535 is deployed to production
83+
response = api.get("/experiments/", params={"limit": 1000000})
8084
details = response.content
8185
if response.ok:
8286
try:
@@ -86,7 +90,10 @@ def list_experiments(api=experiments_api):
8690
logger.log("Error parsing response data")
8791
logger.debug(e)
8892

89-
_log_response(response, details, "Unknown error while retrieving list of experiments")
93+
if response.ok and len(details.splitlines()) > get_terminal_lines():
94+
pydoc.pager(details)
95+
else:
96+
_log_response(response, details, "Unknown error while retrieving list of experiments")
9097

9198

9299
def _make_details_table(experiment):

paperspace/utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import shutil
2+
3+
import six
4+
5+
6+
def get_terminal_lines(fallback=48):
7+
if six.PY3:
8+
return shutil.get_terminal_size().lines
9+
10+
return fallback

tests/test_click_commands.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ def test_should_execute_create_experiment_command_when_cli_singlenode_command_wa
1212
"--projectHandle testHandle " \
1313
"--container testContainer " \
1414
"--machineType testType " \
15-
"--command testCommand"
15+
"--command testCommand " \
16+
"--workspaceUrl wUrl"
1617
expected_kwargs = {"name": u"exp1",
1718
"projectHandle": u"testHandle",
1819
"container": u"testContainer",
1920
"machineType": u"testType",
2021
"command": u"testCommand",
2122
"experimentTypeId": constants.ExperimentType.SINGLE_NODE,
23+
"workspaceUrl": "wUrl",
2224
}
2325

2426
result = runner.invoke(cli.cli, command.split())
@@ -41,7 +43,8 @@ def test_should_execute_create_experiment_command_when_cli_multinode_mpi_command
4143
"--parameterServerContainer testParameterServerContainer " \
4244
"--parameterServerMachineType testParameterServerMachineType " \
4345
"--parameterServerCommand testParameterServerCommand " \
44-
"--parameterServerCount 3"
46+
"--parameterServerCount 3 " \
47+
"--workspaceUrl wUrl"
4548
expected_kwargs = {"name": u"exp1",
4649
"projectHandle": u"testHandle",
4750
"experimentTypeId": constants.ExperimentType.MPI_MULTI_NODE,
@@ -53,6 +56,7 @@ def test_should_execute_create_experiment_command_when_cli_multinode_mpi_command
5356
"parameterServerMachineType": u"testParameterServerMachineType",
5457
"parameterServerCommand": u"testParameterServerCommand",
5558
"parameterServerCount": 3,
59+
"workspaceUrl": "wUrl",
5660
}
5761

5862
result = runner.invoke(cli.cli, command.split())
@@ -75,7 +79,8 @@ def test_should_execute_create_experiment_command_when_cli_multinode_grpc_comman
7579
"--parameterServerContainer testParameterServerContainer " \
7680
"--parameterServerMachineType testParameterServerMachineType " \
7781
"--parameterServerCommand testParameterServerCommand " \
78-
"--parameterServerCount 3"
82+
"--parameterServerCount 3 " \
83+
"--workspaceUrl wUrl"
7984
expected_kwargs = {"name": u"exp1",
8085
"projectHandle": u"testHandle",
8186
"experimentTypeId": constants.ExperimentType.GRPC_MULTI_NODE,
@@ -87,6 +92,7 @@ def test_should_execute_create_experiment_command_when_cli_multinode_grpc_comman
8792
"parameterServerMachineType": u"testParameterServerMachineType",
8893
"parameterServerCommand": u"testParameterServerCommand",
8994
"parameterServerCount": 3,
95+
"workspaceUrl": "wUrl",
9096
}
9197

9298
result = runner.invoke(cli.cli, command.split())
@@ -104,13 +110,15 @@ def test_should_execute_create_experiment_command_when_cli_create_and_start_sing
104110
"--projectHandle testHandle " \
105111
"--container testContainer " \
106112
"--machineType testType " \
107-
"--command testCommand"
113+
"--command testCommand " \
114+
"--workspaceUrl wUrl"
108115
expected_kwargs = {"name": u"exp1",
109116
"projectHandle": u"testHandle",
110117
"container": u"testContainer",
111118
"machineType": u"testType",
112119
"command": u"testCommand",
113120
"experimentTypeId": constants.ExperimentType.SINGLE_NODE,
121+
"workspaceUrl": "wUrl",
114122
}
115123

116124
result = runner.invoke(cli.cli, command.split())
@@ -134,7 +142,8 @@ def test_should_execute_create_experiment_command_when_cli_create_and_start_mult
134142
"--parameterServerContainer testParameterServerContainer " \
135143
"--parameterServerMachineType testParameterServerMachineType " \
136144
"--parameterServerCommand testParameterServerCommand " \
137-
"--parameterServerCount 3"
145+
"--parameterServerCount 3 " \
146+
"--workspaceUrl wUrl"
138147
expected_kwargs = {"name": u"exp1",
139148
"projectHandle": u"testHandle",
140149
"experimentTypeId": constants.ExperimentType.MPI_MULTI_NODE,
@@ -146,6 +155,7 @@ def test_should_execute_create_experiment_command_when_cli_create_and_start_mult
146155
"parameterServerMachineType": u"testParameterServerMachineType",
147156
"parameterServerCommand": u"testParameterServerCommand",
148157
"parameterServerCount": 3,
158+
"workspaceUrl": "wUrl",
149159
}
150160

151161
result = runner.invoke(cli.cli, command.split())

tests/test_experiments_functional.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,19 @@ def test_should_send_get_request_and_print_list_of_experiments(self, get_patched
718718

719719
assert result.output == self.DETAILS_STDOUT
720720

721+
@mock.patch("paperspace.cli.commands.pydoc")
722+
@mock.patch("paperspace.cli.commands.client.requests.get")
723+
def test_should_send_get_request_and_paginate_list_when_output_table_len_is_gt_lines_in_terminal(self, get_patched,
724+
pydoc_patched):
725+
list_json = {"data": self.LIST_JSON["data"] * 40}
726+
get_patched.return_value = MockResponse(list_json, 200, "")
727+
728+
runner = CliRunner()
729+
result = runner.invoke(cli.cli, self.COMMAND)
730+
731+
pydoc_patched.pager.assert_called_once()
732+
assert result.exit_code == 0
733+
721734

722735
class TestStartExperiment(object):
723736
COMMAND = ["experiments", "start", "some-handle"]

0 commit comments

Comments
 (0)