Show More
@@ -1,132 +1,133 b'' | |||
|
1 | 1 | .. _defining_magics: |
|
2 | 2 | |
|
3 | 3 | Defining custom magics |
|
4 | 4 | ====================== |
|
5 | 5 | |
|
6 | 6 | There are two main ways to define your own magic functions: from standalone |
|
7 | 7 | functions and by inheriting from a base class provided by IPython: |
|
8 | 8 | :class:`IPython.core.magic.Magics`. Below we show code you can place in a file |
|
9 | 9 | that you load from your configuration, such as any file in the ``startup`` |
|
10 | 10 | subdirectory of your default IPython profile. |
|
11 | 11 | |
|
12 | 12 | First, let us see the simplest case. The following shows how to create a line |
|
13 | 13 | magic, a cell one and one that works in both modes, using just plain functions: |
|
14 | 14 | |
|
15 | 15 | .. sourcecode:: python |
|
16 | 16 | |
|
17 | 17 | from IPython.core.magic import (register_line_magic, register_cell_magic, |
|
18 | 18 | register_line_cell_magic) |
|
19 | 19 | |
|
20 | 20 | @register_line_magic |
|
21 | 21 | def lmagic(line): |
|
22 | 22 | "my line magic" |
|
23 | 23 | return line |
|
24 | 24 | |
|
25 | 25 | @register_cell_magic |
|
26 | 26 | def cmagic(line, cell): |
|
27 | 27 | "my cell magic" |
|
28 | 28 | return line, cell |
|
29 | 29 | |
|
30 | 30 | @register_line_cell_magic |
|
31 | 31 | def lcmagic(line, cell=None): |
|
32 | 32 | "Magic that works both as %lcmagic and as %%lcmagic" |
|
33 | 33 | if cell is None: |
|
34 | 34 | print("Called as line magic") |
|
35 | 35 | return line |
|
36 | 36 | else: |
|
37 | 37 | print("Called as cell magic") |
|
38 | 38 | return line, cell |
|
39 | 39 | |
|
40 | # We delete these to avoid name conflicts for automagic to work | |
|
40 | # In an interactive session, we need to delete these to avoid | |
|
41 | # name conflicts for automagic to work on line magics. | |
|
41 | 42 | del lmagic, lcmagic |
|
42 | 43 | |
|
43 | 44 | |
|
44 | 45 | You can also create magics of all three kinds by inheriting from the |
|
45 | 46 | :class:`IPython.core.magic.Magics` class. This lets you create magics that can |
|
46 | 47 | potentially hold state in between calls, and that have full access to the main |
|
47 | 48 | IPython object: |
|
48 | 49 | |
|
49 | 50 | .. sourcecode:: python |
|
50 | 51 | |
|
51 | 52 | # This code can be put in any Python module, it does not require IPython |
|
52 | 53 | # itself to be running already. It only creates the magics subclass but |
|
53 | 54 | # doesn't instantiate it yet. |
|
54 | 55 | from __future__ import print_function |
|
55 | 56 | from IPython.core.magic import (Magics, magics_class, line_magic, |
|
56 | 57 | cell_magic, line_cell_magic) |
|
57 | 58 | |
|
58 | 59 | # The class MUST call this class decorator at creation time |
|
59 | 60 | @magics_class |
|
60 | 61 | class MyMagics(Magics): |
|
61 | 62 | |
|
62 | 63 | @line_magic |
|
63 | 64 | def lmagic(self, line): |
|
64 | 65 | "my line magic" |
|
65 | 66 | print("Full access to the main IPython object:", self.shell) |
|
66 | 67 | print("Variables in the user namespace:", list(self.shell.user_ns.keys())) |
|
67 | 68 | return line |
|
68 | 69 | |
|
69 | 70 | @cell_magic |
|
70 | 71 | def cmagic(self, line, cell): |
|
71 | 72 | "my cell magic" |
|
72 | 73 | return line, cell |
|
73 | 74 | |
|
74 | 75 | @line_cell_magic |
|
75 | 76 | def lcmagic(self, line, cell=None): |
|
76 | 77 | "Magic that works both as %lcmagic and as %%lcmagic" |
|
77 | 78 | if cell is None: |
|
78 | 79 | print("Called as line magic") |
|
79 | 80 | return line |
|
80 | 81 | else: |
|
81 | 82 | print("Called as cell magic") |
|
82 | 83 | return line, cell |
|
83 | 84 | |
|
84 | 85 | |
|
85 | 86 | # In order to actually use these magics, you must register them with a |
|
86 | 87 | # running IPython. This code must be placed in a file that is loaded once |
|
87 | 88 | # IPython is up and running: |
|
88 | 89 | ip = get_ipython() |
|
89 | 90 | # You can register the class itself without instantiating it. IPython will |
|
90 | 91 | # call the default constructor on it. |
|
91 | 92 | ip.register_magics(MyMagics) |
|
92 | 93 | |
|
93 | 94 | If you want to create a class with a different constructor that holds |
|
94 | 95 | additional state, then you should always call the parent constructor and |
|
95 | 96 | instantiate the class yourself before registration: |
|
96 | 97 | |
|
97 | 98 | .. sourcecode:: python |
|
98 | 99 | |
|
99 | 100 | @magics_class |
|
100 | 101 | class StatefulMagics(Magics): |
|
101 | 102 | "Magics that hold additional state" |
|
102 | 103 | |
|
103 | 104 | def __init__(self, shell, data): |
|
104 | 105 | # You must call the parent constructor |
|
105 | 106 | super(StatefulMagics, self).__init__(shell) |
|
106 | 107 | self.data = data |
|
107 | 108 | |
|
108 | 109 | # etc... |
|
109 | 110 | |
|
110 | 111 | # This class must then be registered with a manually created instance, |
|
111 | 112 | # since its constructor has different arguments from the default: |
|
112 | 113 | ip = get_ipython() |
|
113 | 114 | magics = StatefulMagics(ip, some_data) |
|
114 | 115 | ip.register_magics(magics) |
|
115 | 116 | |
|
116 | 117 | |
|
117 | 118 | In earlier versions, IPython had an API for the creation of line magics (cell |
|
118 | 119 | magics did not exist at the time) that required you to create functions with a |
|
119 | 120 | method-looking signature and to manually pass both the function and the name. |
|
120 | 121 | While this API is no longer recommended, it remains indefinitely supported for |
|
121 | 122 | backwards compatibility purposes. With the old API, you'd create a magic as |
|
122 | 123 | follows: |
|
123 | 124 | |
|
124 | 125 | .. sourcecode:: python |
|
125 | 126 | |
|
126 | 127 | def func(self, line): |
|
127 | 128 | print("Line magic called with line:", line) |
|
128 | 129 | print("IPython object:", self.shell) |
|
129 | 130 | |
|
130 | 131 | ip = get_ipython() |
|
131 | 132 | # Declare this function as the magic %mycommand |
|
132 | 133 | ip.define_magic('mycommand', func) |
General Comments 0
You need to be logged in to leave comments.
Login now