Skip to content

Metakernel Magic Handling Discussion

blink1073 edited this page Nov 1, 2014 · 6 revisions

Metakernel can handle three different functions:

  1. cell and line magics; starts with %% and % respectively
  2. shell commands; starts with ! (shortcut for %shell)
  3. help; ? or ?? as prefix or suffix

All of the special characters used above can be modified by the kernel to accomodate the native language

All three of these must happen in the first line(s), before any code. Magics and ! must start in the first column.

Magics can be stacked such that the output of one can be the input of the next. Magics can return any object they like (but it should be JSON-dumpable) The original code text is set to self.code. If a magic wants to change the code, it need only change it, and return it in the post_process method. If a magic "eats" the code (like %%javascript and %%html), then the magic should set self.evaluate to False.

If no valid magic is found, the line will be considered code and handled by the kernel.

Examples:

This first starts the timer, send the rest of the code to the Python system, stops the timer, prints out the time. Because it sets the special name "retval", then that value is returned as the value of the cell computation:

%%time
%%python
retval = x + 1

Here we set the variable files in the kernel using the output of a shell command:

%%set files
! ls -al

Help examples:

All of the following give help on np.ones using the %python magic:

%python ?np.ones   
%help %python np.ones 
%python np.ones?

To get help on a magic itself, we would write one of the following:

%python?
%help %python
?%python
Clone this wiki locally