Warning

This documentation is for an old version of IPython. You can find docs for newer versions here.

Module: kernel.client

Base class to manage the interaction with a running kernel

1 Class

class IPython.kernel.client.KernelClient(**kwargs)

Bases: IPython.kernel.connect.ConnectionFileMixin

Communicates with a single kernel on any host via zmq channels.

There are four channels associated with each kernel:

  • shell: for request/reply calls to the kernel.
  • iopub: for the kernel to publish results to frontends.
  • hb: for monitoring the kernel’s heartbeat.
  • stdin: for frontends to reply to raw_input calls in the kernel.

The methods of the channels are exposed as methods of the client itself (KernelClient.execute, complete, history, etc.). See the channels themselves for documentation of these methods.

channels_running

Are any of the channels created and running?

complete(code, cursor_pos=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:

The msg_id of the message sent.

execute(code, silent=False, store_history=True, user_expressions=None, allow_stdin=None, stop_on_error=True)

Execute code in the kernel.

Parameters:

code : str

A string of Python code.

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:

The msg_id of the message sent.

get_iopub_msg(*args, **kwargs)

Get a message from the iopub channel

get_shell_msg(*args, **kwargs)

Get a message from the shell channel

get_stdin_msg(*args, **kwargs)

Get a message from the stdin channel

hb_channel

Get the hb channel object for this kernel.

history(raw=True, output=False, hist_access_type='range', **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:

The msg_id of the message sent.

input(string)

Send a string of raw input to the kernel.

inspect(code, cursor_pos=None, detail_level=0)

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:

The msg_id of the message sent.

iopub_channel

Get the iopub channel object for this kernel.

is_alive()

Is the kernel process still running?

kernel_info()

Request kernel info.

shell_channel

Get the shell channel object for this kernel.

shutdown(restart=False)

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.

start_channels(shell=True, iopub=True, stdin=True, hb=True)

Starts the channels for this kernel.

This will create the channels if they do not exist and then start them (their activity runs in a thread). If port numbers of 0 are being used (random ports) then you must first call start_kernel(). If the channels have been stopped and you call this, RuntimeError will be raised.

stdin_channel

Get the stdin channel object for this kernel.

stop_channels()

Stops all the running channels for this kernel.

This stops their event loops and joins their threads.

1 Function

IPython.kernel.client.validate_string_dict(dct)

Validate that the input is a dict with string keys and values.

Raises ValueError if not.