Kernel Client¶
The Kernel Client API is how an application communicates with a previously
launched kernel using the Jupyter message protocol (FIXME jupyter_protocol).
For applications based on IO loops, Jupyter Kernel Management provides IOLoopKernelClient
.
-
class
jupyter_kernel_mgmt.client_base.
KernelClient
(connection_info, manager=None, use_heartbeat=True)¶ Communicates with a single kernel on any host via zmq channels.
The messages that can be sent are exposed as methods of the client (KernelClient.execute, complete, history, etc.). These methods only send the message, they don’t wait for a reply. To get results, use e.g.
get_shell_msg()
to fetch messages from the shell channel.-
close
()¶ Close sockets of this client.
After calling this, the client can no longer be used.
-
comm_info
(target_name=None, _header=None)¶ Request comm info
- Returns
- Return type
The msg_id of the message sent
-
complete
(code, cursor_pos=None, _header=None)¶ Tab complete text in the kernel’s namespace.
- Parameters
code (str) – The context in which completion is requested. Can be anything between a variable name and an entire cell.
cursor_pos (int, optional) – The position of the cursor in the block of code where the completion was requested. Default:
len(code)
- Returns
- Return type
The msg_id of the message sent.
-
execute
(code, silent=False, store_history=True, user_expressions=None, allow_stdin=None, stop_on_error=True, _header=None)¶ Execute code in the kernel.
- Parameters
code (str) – A string of code in the kernel’s language.
silent (bool, optional (default False)) – If set, the kernel will execute the code as quietly possible, and will force store_history to be False.
store_history (bool, optional (default True)) – If set, the kernel will store command history. This is forced to be False if silent is True.
user_expressions (dict, optional) – A dict mapping names to expressions to be evaluated in the user’s dict. The expression values are returned as strings formatted using
repr()
.allow_stdin (bool, optional (default self.allow_stdin)) –
Flag for whether the kernel can send stdin requests to frontends.
Some frontends (e.g. the Notebook) do not support stdin requests. If raw_input is called from code executed from such a frontend, a StdinNotImplementedError will be raised.
stop_on_error (bool, optional (default True)) – Flag whether to abort the execution queue, if an exception is encountered.
- Returns
- Return type
The msg_id of the message sent.
-
history
(raw=True, output=False, hist_access_type='range', _header=None, **kwargs)¶ Get entries from the kernel’s history list.
- Parameters
raw (bool) – If True, return the raw input.
output (bool) – If True, then return the output as well.
hist_access_type (str) –
- ‘range’ (fill in session, start and stop params), ‘tail’ (fill in n)
or ‘search’ (fill in pattern param).
session (int) – For a range request, the session from which to get lines. Session numbers are positive integers; negative ones count back from the current session.
start (int) – The first line number of a history range.
stop (int) – The final (excluded) line number of a history range.
n (int) – The number of lines of history to get for a tail request.
pattern (str) – The glob-syntax pattern for a search request.
- Returns
- Return type
The ID of the message sent.
-
input
(string, parent=None)¶ Send a string of raw input to the kernel.
This should only be called in response to the kernel sending an
input_request
message on the stdin channel.
-
inspect
(code, cursor_pos=None, detail_level=0, _header=None)¶ Get metadata information about an object in the kernel’s namespace.
It is up to the kernel to determine the appropriate object to inspect.
- Parameters
code (str) – The context in which info is requested. Can be anything between a variable name and an entire cell.
cursor_pos (int, optional) – The position of the cursor in the block of code where the info was requested. Default:
len(code)
detail_level (int, optional) – The level of detail for the introspection (0-2)
- Returns
- Return type
The msg_id of the message sent.
-
interrupt
(_header=None)¶ Send an interrupt message/signal to the kernel
-
is_complete
(code, _header=None)¶ Ask the kernel whether some code is complete and ready to execute.
-
kernel_info
(_header=None)¶ Request kernel info
- Returns
- Return type
The msg_id of the message sent
-
property
owned_kernel
¶ True if this client ‘owns’ the kernel, i.e. started it.
-
shutdown
(restart=False, _header=None)¶ Request an immediate kernel shutdown.
Upon receipt of the (empty) reply, client code can safely assume that the kernel has shut down and it’s safe to forcefully terminate it if it’s still alive.
The kernel will send the reply via a function registered with Python’s atexit module, ensuring it’s truly done as the kernel is done with all normal operation.
- Returns
- Return type
The msg_id of the message sent
-
-
class
jupyter_kernel_mgmt.client.
IOLoopKernelClient
(connection_info, manager=None)¶ Uses a zmq/tornado IOLoop to handle received messages and fire callbacks.
Use ClientInThread to run this in a separate thread alongside your application.
-
add_handler
(handler, channels)¶ Add a callback for received messages on one or more channels.
- Parameters
handler (function) – Will be called for each message received with the message dictionary as a single argument.
channels (set or str) – Channel names: ‘shell’, ‘iopub’, ‘stdin’ or ‘control’
-
close
()¶ Close the client’s sockets & streams.
This does not close the IOLoop.
-
comm_info
(target_name=None, _header=None)¶ Request comm info
- Returns
- Return type
The msg_id of the message sent
-
complete
(code, cursor_pos=None, _header=None)¶ Tab complete text in the kernel’s namespace.
- Parameters
code (str) – The context in which completion is requested. Can be anything between a variable name and an entire cell.
cursor_pos (int, optional) – The position of the cursor in the block of code where the completion was requested. Default:
len(code)
- Returns
- Return type
The msg_id of the message sent.
-
execute
(code, silent=False, store_history=True, user_expressions=None, allow_stdin=None, stop_on_error=True, interrupt_timeout=None, idle_timeout=None, raise_on_no_idle=False, _header=None)¶ Execute code in the kernel.
- Parameters
code (str) – A string of code in the kernel’s language.
silent (bool, optional (default False)) – If set, the kernel will execute the code as quietly possible, and will force store_history to be False.
store_history (bool, optional (default True)) – If set, the kernel will store command history. This is forced to be False if silent is True.
user_expressions (dict, optional) – A dict mapping names to expressions to be evaluated in the user’s dict. The expression values are returned as strings formatted using
repr()
.allow_stdin (bool, optional (default self.allow_stdin)) –
Flag for whether the kernel can send stdin requests to frontends.
Some frontends (e.g. the Notebook) do not support stdin requests. If raw_input is called from code executed from such a frontend, a StdinNotImplementedError will be raised.
stop_on_error (bool, optional (default True)) – Flag whether to abort the execution queue, if an exception is encountered.
- Returns
- Return type
The msg_id of the message sent.
-
history
(raw=True, output=False, hist_access_type='range', _header=None, **kwargs)¶ Get entries from the kernel’s history list.
- Parameters
raw (bool) – If True, return the raw input.
output (bool) – If True, then return the output as well.
hist_access_type (str) –
- ‘range’ (fill in session, start and stop params), ‘tail’ (fill in n)
or ‘search’ (fill in pattern param).
session (int) – For a range request, the session from which to get lines. Session numbers are positive integers; negative ones count back from the current session.
start (int) – The first line number of a history range.
stop (int) – The final (excluded) line number of a history range.
n (int) – The number of lines of history to get for a tail request.
pattern (str) – The glob-syntax pattern for a search request.
- Returns
- Return type
The ID of the message sent.
-
input
(string, parent=None)¶ Send a string of raw input to the kernel.
This should only be called in response to the kernel sending an
input_request
message on the stdin channel.
-
inspect
(code, cursor_pos=None, detail_level=0, _header=None)¶ Get metadata information about an object in the kernel’s namespace.
It is up to the kernel to determine the appropriate object to inspect.
- Parameters
code (str) – The context in which info is requested. Can be anything between a variable name and an entire cell.
cursor_pos (int, optional) – The position of the cursor in the block of code where the info was requested. Default:
len(code)
detail_level (int, optional) – The level of detail for the introspection (0-2)
- Returns
- Return type
The msg_id of the message sent.
-
async
interrupt
(_header=None)¶ Send an interrupt message/signal to the kernel
-
is_complete
(code, _header=None)¶ Ask the kernel whether some code is complete and ready to execute.
-
kernel_info
(_header=None)¶ Request kernel info
- Returns
- Return type
The msg_id of the message sent
-
property
owned_kernel
¶ True if this client ‘owns’ the kernel, i.e. started it.
-
remove_handler
(handler, channels=None)¶ Remove a previously registered callback.
-
shutdown
(restart=False, _header=None)¶ Request an immediate kernel shutdown.
Upon receipt of the (empty) reply, client code can safely assume that the kernel has shut down and it’s safe to forcefully terminate it if it’s still alive.
The kernel will send the reply via a function registered with Python’s atexit module, ensuring it’s truly done as the kernel is done with all normal operation.
- Returns
- Return type
The msg_id of the message sent
-
shutdown_or_terminate
(timeout=5.0)¶ Ask the kernel to shut down, and terminate it if it takes too long.
The kernel will be given up to timeout seconds to respond to the shutdown message, then the same timeout to terminate.
-