@@ -76,7 +76,7 @@ def last_activity(self) -> datetime.datetime | None:
76
76
else None
77
77
)
78
78
79
- def execute (
79
+ def execute ( # noqa: C901
80
80
self ,
81
81
code : str ,
82
82
silent : bool = False ,
@@ -89,11 +89,6 @@ def execute(
89
89
) -> dict [str , t .Any ]:
90
90
"""Execute code in the kernel interactively
91
91
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
-
97
92
Args:
98
93
code: A string of code in the kernel's language.
99
94
silent: optional (default False) If set, the kernel will execute the code as quietly possible, and
@@ -109,7 +104,6 @@ def execute(
109
104
Flag whether to abort the execution queue, if an exception is encountered.
110
105
timeout:
111
106
Timeout to use when waiting for a reply
112
-
113
107
stdin_hook:
114
108
Function to be called with stdin_request messages.
115
109
If not specified, input/getpass will be called.
@@ -195,6 +189,77 @@ def output_hook(outputs: list[dict], msg: dict) -> None:
195
189
"status" : reply_content ["status" ],
196
190
}
197
191
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
+
198
263
def interrupt (self , timeout : float = REQUEST_TIMEOUT ) -> None :
199
264
"""Interrupts the kernel."""
200
265
self ._manager .interrupt_kernel (timeout = timeout )
0 commit comments