Skip to content

Commit 883ae21

Browse files
committed
Add documentation
1 parent 440c6b8 commit 883ae21

File tree

1 file changed

+35
-16
lines changed

1 file changed

+35
-16
lines changed

jupyter_server_nbmodel/handlers.py

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
class ExecutionStack:
3434
"""Execution request stack.
3535
36+
It is keeping track of the execution requests.
37+
3638
The request result can only be queried once.
3739
"""
3840

@@ -44,11 +46,12 @@ def __del__(self):
4446
for task in filter(lambda t: not t.cancelled(), self.__tasks.values()):
4547
task.cancel()
4648

47-
def cancel(self, uid: str) -> None:
49+
def cancel(self, kernel_id: str, uid: str) -> None:
4850
"""Cancel the request ``uid``.
4951
5052
Args:
51-
uid: Task identifier
53+
kernel_id : Kernel identifier
54+
uid : Request identifier
5255
"""
5356
get_logger().debug(f"Cancel request {uid}.")
5457
if uid not in self.__tasks:
@@ -57,18 +60,18 @@ def cancel(self, uid: str) -> None:
5760
self.__tasks[uid].cancel()
5861

5962
def get(self, kernel_id: str, uid: str) -> t.Any:
60-
"""Get the request ``uid`` results or None.
63+
"""Get the request ``uid`` results, its pending input or None.
6164
6265
Args:
6366
kernel_id : Kernel identifier
64-
uid : Request index
67+
uid : Request identifier
6568
6669
Returns:
67-
Any: None if the request is pending else its result
70+
Any: None if the request is pending else its result or the pending input.
6871
6972
Raises:
70-
ValueError: If the request `uid` does not exists.
71-
asyncio.CancelledError: If the request `uid` was cancelled.
73+
ValueError: If the request ``uid`` does not exists.
74+
asyncio.CancelledError: If the request ``uid`` was cancelled.
7275
"""
7376
if uid not in self.__tasks:
7477
raise ValueError(f"Request {uid} does not exists.")
@@ -88,20 +91,25 @@ def put(
8891
"""Add a asynchronous execution request.
8992
9093
Args:
91-
task: Asynchronous task
92-
*args : arguments of the task
94+
km: Kernel manager
95+
snippet: Snippet to be executed
96+
ycell: [optional] The cell to update with the execution results
9397
9498
Returns:
9599
Request identifier
96100
"""
97101
uid = str(uuid.uuid4())
98102

99103
self.__tasks[uid] = asyncio.create_task(
100-
execute_task(uid, km, snippet, ycell, partial(self._stdin_hook, km.kernel_id))
104+
_execute_task(uid, km, snippet, ycell, partial(self._stdin_hook, km.kernel_id))
101105
)
102106
return uid
103107

104108
def _stdin_hook(self, kernel_id: str, msg: dict) -> None:
109+
"""Callback on stdin message.
110+
111+
It will register the pending input as temporary answer to the execution request.
112+
"""
105113
get_logger().info(f"Execution request {kernel_id} received a input request {msg!s}")
106114
if kernel_id in self.__pending_inputs:
107115
get_logger().error(
@@ -116,7 +124,7 @@ def _stdin_hook(self, kernel_id: str, msg: dict) -> None:
116124
}
117125

118126

119-
async def execute_task(
127+
async def _execute_task(
120128
uid,
121129
km: jupyter_client.manager.KernelManager,
122130
snippet: str,
@@ -335,7 +343,15 @@ class InputHandler(ExtensionHandlerMixin, APIHandler):
335343

336344
@tornado.web.authenticated
337345
async def post(self, kernel_id: str) -> None:
338-
body = self.get_json_body()
346+
"""
347+
Send an input value to kernel ``kernel_id``.
348+
349+
Args:
350+
kernel_id: Kernel identifier
351+
352+
Json Body Required:
353+
input (str): Input value
354+
"""
339355

340356
try:
341357
km = self.kernel_manager.get_kernel(kernel_id)
@@ -344,6 +360,7 @@ async def post(self, kernel_id: str) -> None:
344360
get_logger().error(msg, exc_info=e)
345361
raise tornado.web.HTTPError(status_code=HTTPStatus.NOT_FOUND, reason=msg) from e
346362

363+
body = self.get_json_body()
347364
client = km.client()
348365

349366
try:
@@ -369,7 +386,7 @@ def initialize(
369386

370387
@tornado.web.authenticated
371388
def get(self, kernel_id: str, request_id: str) -> None:
372-
"""`GET /api/kernels/<kernel_id>/requests/<id>` Returns the request ``uid`` status.
389+
"""`GET /api/kernels/<kernel_id>/requests/<request_id>` Returns the request ``uid`` status.
373390
374391
Status are:
375392
@@ -379,7 +396,8 @@ def get(self, kernel_id: str, request_id: str) -> None:
379396
* 500: Request ends with errors
380397
381398
Args:
382-
index: Request identifier
399+
kernel_id: Kernel identifier
400+
request_id: Request identifier
383401
384402
Raises:
385403
404 if request ``uid`` does not exist
@@ -405,13 +423,14 @@ def get(self, kernel_id: str, request_id: str) -> None:
405423

406424
@tornado.web.authenticated
407425
def delete(self, kernel_id: str, request_id: str) -> None:
408-
"""`DELETE /api/kernels/<kernel_id>/requests/<id>` cancels the request ``uid``.
426+
"""`DELETE /api/kernels/<kernel_id>/requests/<request_id>` cancels the request ``uid``.
409427
410428
Status are:
411429
* 204: Request cancelled
412430
413431
Args:
414-
uid: Request uid
432+
kernel_id: Kernel identifier
433+
request_id: Request identifier
415434
416435
Raises:
417436
404 if request ``uid`` does not exist

0 commit comments

Comments
 (0)