API Reference

Metakernel

class metakernel.MetaKernel(**kwargs: Any)[source]

The base MetaKernel class.

Initialize the kernel.

classmethod run_as_main(*args: Any, **kwargs: Any) None[source]

Launch or install a metakernel.

Modules implementing a metakernel subclass can use the following lines:

if __name__ == ‘__main__’:

MetaKernelSubclass.run_as_main()

makeSubkernel(kernel: MetaKernel) None[source]

Run this method in an IPython kernel to set this kernel’s input/output settings.

set_variable(name: str, value: Any) None[source]

Set a variable to a Python-typed value.

get_variable(name: str) Any[source]

Lookup a variable name and return a Python-typed value.

repr(item: Any) str[source]

The repr of the kernel.

get_usage() str[source]

Get the usage statement for the kernel.

get_kernel_help_on(info: dict[str, Any], level: int = 0, none_on_fail: bool = False) str | None[source]

Get help on an object. Called by the help magic.

handle_plot_settings() None[source]

Handle the current plot settings

get_local_magics_dir() str[source]

Returns the path to local magics dir (eg ~/.ipython/metakernel/magics)

get_completions(info: dict[str, Any]) list[str][source]

Get completions from kernel based on info dict.

do_execute_direct(code: str, silent: bool = False) Any[source]

Execute code in the kernel language.

do_execute_file(filename: str) Any[source]

Default code for running a file. Just opens the file, and sends the text to do_execute_direct.

do_execute_meta(code: str) Any[source]

Execute meta code in the kernel. This uses the execute infrastructure but allows JavaScript to talk directly to the kernel bypassing normal processing.

When responding to the %%debug magic, the step and reset meta commands can answer with a string in the format:

“highlight: [start_line, start_col, end_line, end_col]”

for highlighting expressions in the frontend.

initialize_debug(code: str) str[source]

This function is used with the %%debug magic for highlighting lines of code, and for initializing debug functions.

Return the empty string if highlighting is not supported.

do_function_direct(function_name: str, arg: Any) Any[source]

Call a function in the kernel language with args (as a single item).

restart_kernel() None[source]

Restart the kernel

async do_execute(code: Any, silent: Any = False, store_history: Any = True, user_expressions: Any = None, allow_stdin: Any = False, *, cell_meta: Any = None, cell_id: Any = None) Any[source]

Handle code execution.

https://jupyter-client.readthedocs.io/en/stable/messaging.html#execute

async post_execute(retval: Any, code: str, silent: bool) None[source]

Post-execution actions

Handle special kernel variables and display response if not silent.

async do_history(hist_access_type: str | None, output: str | None, raw: bool | None, session: Any = None, start: int | None = None, stop: int | None = None, n: int | None = None, pattern: Any = None, unique: bool = False) dict[str, str | list[Any]][source]

Access history at startup.

https://jupyter-client.readthedocs.io/en/stable/messaging.html#history

async do_shutdown(restart: bool) dict[str, Any][source]

Shut down the app gracefully, saving history.

https://jupyter-client.readthedocs.io/en/stable/messaging.html#kernel-shutdown

async do_is_complete(code: str) dict[str, str][source]

Given code as string, returns dictionary with ‘status’ representing whether code is ready to evaluate. Possible values for status are:

‘complete’ - ready to evaluate ‘incomplete’ - not yet ready ‘invalid’ - invalid code ‘unknown’ - unknown; the default unless overridden

Optionally, if ‘status’ is ‘incomplete’, you may indicate an indentation string.

Example:

return {‘status’‘incomplete’,

‘indent’: ‘ ‘ * 4}

https://jupyter-client.readthedocs.io/en/stable/messaging.html#code-completeness

async do_complete(code: str, cursor_pos: int) dict[str, Any][source]

Handle code completion for the kernel.

https://jupyter-client.readthedocs.io/en/stable/messaging.html#completion

async do_inspect(code: str, cursor_pos: int, detail_level: int = 0, omit_sections: Any = ()) dict[str, Any] | None[source]

Object introspection.

https://jupyter-client.readthedocs.io/en/stable/messaging.html#introspection

async clear_output(wait: bool = False) None[source]

Clear the output of the kernel.

Display(*objects: Any, **kwargs: Any) None[source]

Display one or more objects using rich display.

Supports a clear_output keyword argument that clears the output before displaying.

See https://ipython.readthedocs.io/en/stable/config/integrating.html?highlight=display#rich-display

Print(*objects: Any, **kwargs: Any) None[source]

Print objects to the iopub stream, separated by sep and followed by end.

Items can be strings or Widget instances.

Write(message: str) None[source]

Write message directly to the iopub stdout with no added end character.

Error(*objects: Any, **kwargs: Any) None[source]

Print objects to stdout, separated by sep and followed by end.

Objects are cast to strings.

Error_display(*objects: Any, **kwargs: Any) None[source]

Print objects to stdout is they area strings, separated by sep and followed by end. All other objects are rendered using the Display method Objects are cast to strings.

reload_magics() None[source]

Reload all of the line and cell magics.

register_magics(magic_klass: type[Magic]) None[source]

Register magics for a given magic_klass.

send_response(*args: Any, **kwargs: Any) None[source]

Send a response to the message we’re currently processing.

This accepts all the parameters of jupyter_client.session.Session.send() except parent.

This relies on set_parent() having been called for the current message.

call_magic(line: str) Magic[source]

Given an line, such as “%download http://example.com/”, parse and execute magic.

get_help_on(expr: str, level: int = 0, none_on_fail: bool = False, cursor_pos: int = -1) Any[source]

Get help for an expression using the help magic.

parse_code(code: str, cursor_start: int = 0, cursor_end: int = -1) dict[str, Any][source]

Parse code using our parser.

class metakernel.Magic(kernel: MetaKernel)[source]

Base class to define magics for MetaKernel based kernels.

Users can redefine the default magics provided by Metakernel by creating a module with the exact same name as the Metakernel magic.

For example, you can override %matplotlib in your kernel by writing a new magic inside magics/matplotlib_magic.py

get_completions(info: dict[str, Any]) list[str][source]

Get completions based on info dict from magic.

metakernel.option(*args: Any, **kwargs: Any) Callable[[_F], _F][source]

Return decorator that adds a magic option to a function.