Skip to content

Commit 4752ca2

Browse files
Frédéric Collonvalfcollonval
andauthored
Add KernelClient.execute_interactive (#2)
Co-authored-by: Frédéric Collonval <[email protected]>
1 parent 2871e57 commit 4752ca2

File tree

2 files changed

+74
-7
lines changed

2 files changed

+74
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
## 0.1.1
66

7+
:warning: Not published
8+
79
([Full Changelog](https://github.com/datalayer/jupyter-kernel-client/compare/66064f9f7afe59b2450fc3a15a2e78f4eb606852))
810

911
First version

jupyter_kernel_client/client.py

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def last_activity(self) -> datetime.datetime | None:
7676
else None
7777
)
7878

79-
def execute(
79+
def execute( # noqa: C901
8080
self,
8181
code: str,
8282
silent: bool = False,
@@ -89,11 +89,6 @@ def execute(
8989
) -> dict[str, t.Any]:
9090
"""Execute code in the kernel interactively
9191
92-
Output will be redisplayed, and stdin prompts will be relayed as well.
93-
94-
You can pass a custom output_hook callable that will be called
95-
with every IOPub message that is produced instead of the default redisplay.
96-
9792
Args:
9893
code: A string of code in the kernel's language.
9994
silent: optional (default False) If set, the kernel will execute the code as quietly possible, and
@@ -109,7 +104,6 @@ def execute(
109104
Flag whether to abort the execution queue, if an exception is encountered.
110105
timeout:
111106
Timeout to use when waiting for a reply
112-
113107
stdin_hook:
114108
Function to be called with stdin_request messages.
115109
If not specified, input/getpass will be called.
@@ -195,6 +189,77 @@ def output_hook(outputs: list[dict], msg: dict) -> None:
195189
"status": reply_content["status"],
196190
}
197191

192+
def execute_interactive(
193+
self,
194+
code: str,
195+
silent: bool = False,
196+
store_history: bool = True,
197+
user_expressions: dict[str, t.Any] | None = None,
198+
allow_stdin: bool | None = None,
199+
stop_on_error: bool = True,
200+
timeout: float | None = None,
201+
output_hook: t.Callable | None = None,
202+
stdin_hook: t.Callable | None = None,
203+
) -> dict[str, t.Any]:
204+
"""Execute code in the kernel with low-level API
205+
206+
Output will be redisplayed, and stdin prompts will be relayed as well.
207+
208+
You can pass a custom output_hook callable that will be called
209+
with every IOPub message that is produced instead of the default redisplay.
210+
211+
Parameters
212+
----------
213+
code : str
214+
A string of code in the kernel's language.
215+
216+
silent : bool, optional (default False)
217+
If set, the kernel will execute the code as quietly possible, and
218+
will force store_history to be False.
219+
220+
store_history : bool, optional (default True)
221+
If set, the kernel will store command history. This is forced
222+
to be False if silent is True.
223+
224+
user_expressions : dict, optional
225+
A dict mapping names to expressions to be evaluated in the user's
226+
dict. The expression values are returned as strings formatted using
227+
:func:`repr`.
228+
229+
allow_stdin : bool, optional (default self.allow_stdin)
230+
Flag for whether the kernel can send stdin requests to frontends.
231+
232+
stop_on_error: bool, optional (default True)
233+
Flag whether to abort the execution queue, if an exception is encountered.
234+
235+
timeout: float or None (default: None)
236+
Timeout to use when waiting for a reply
237+
238+
output_hook: callable(msg)
239+
Function to be called with output messages.
240+
If not specified, output will be redisplayed.
241+
242+
stdin_hook: callable(msg)
243+
Function to be called with stdin_request messages.
244+
If not specified, input/getpass will be called.
245+
246+
Returns
247+
-------
248+
reply: dict
249+
The reply message for this request
250+
"""
251+
return self._manager.client.execute_interactive(
252+
code,
253+
silent=silent,
254+
store_history=store_history,
255+
user_expressions=user_expressions,
256+
allow_stdin=allow_stdin,
257+
stop_on_error=stop_on_error,
258+
timeout=timeout,
259+
output_hook=output_hook,
260+
stdin_hook=stdin_hook,
261+
)
262+
198263
def interrupt(self, timeout: float = REQUEST_TIMEOUT) -> None:
199264
"""Interrupts the kernel."""
200265
self._manager.interrupt_kernel(timeout=timeout)

0 commit comments

Comments
 (0)