# API Reference

## Metakernel

class metakernel.MetaKernel(**kwargs: Any)

  The base MetaKernel class.

  Initialize the kernel.

  classmethod run_as_main(*args, **kwargs)

    Launch or install a metakernel.

    Modules implementing a metakernel subclass can use the following lines:

    > if __name__ == ‘__main__’:

    >   MetaKernelSubclass.run_as_main()

  makeSubkernel(kernel)

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

  set_variable(name, value)

    Set a variable to a Python-typed value.

  get_variable(name)

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

  repr(item)

    The repr of the kernel.

  get_usage()

    Get the usage statement for the kernel.

  get_kernel_help_on(info, level=0, none_on_fail=False)

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

  handle_plot_settings()

    Handle the current plot settings

  get_local_magics_dir()

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

  get_completions(info)

    Get completions from kernel based on info dict.

  do_execute_direct(code, silent=False)

    Execute code in the kernel language.

  do_execute_file(filename)

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

  do_execute_meta(code)

    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)

    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, arg)

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

  restart_kernel()

    Restart the kernel

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

    Handle code execution.

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

  post_execute(retval, code, silent)

    Post-execution actions

    Handle special kernel variables and display response if not silent.

  do_history(hist_access_type, output, raw, session=None, start=None, stop=None, n=None, pattern=None, unique=False)

    Access history at startup.

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

  do_shutdown(restart)

    Shut down the app gracefully, saving history.

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

  do_is_complete(code)

    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

  do_complete(code, cursor_pos)

    Handle code completion for the kernel.

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

  do_inspect(code, cursor_pos, detail_level=0, omit_sections=())

    Object introspection.

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

  clear_output(wait=False)

    Clear the output of the kernel.

  Display(*objects, **kwargs)

    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, **kwargs)

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

    Items can be strings or Widget instances.

  Write(message)

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

  Error(*objects, **kwargs)

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

    Objects are cast to strings.

  Error_display(*objects, **kwargs)

    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()

    Reload all of the line and cell magics.

  register_magics(magic_klass)

    Register magics for a given magic_klass.

  send_response(*args, **kwargs)

    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)

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

  get_help_on(expr, level=0, none_on_fail=False, cursor_pos=-1)

    Get help for an expression using the help magic.

  parse_code(code, cursor_start=0, cursor_end=-1)

    Parse code using our parser.

class metakernel.Magic(kernel)

  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)

    Get completions based on info dict from magic.

metakernel.option(*args, **kwargs)

  Return decorator that adds a magic option to a function.