##// END OF EJS Templates
Fixes, plus complete example....
Matthias Bussonnier -
Show More
@@ -1,135 +1,179 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 40 # In an interactive session, we need to delete these to avoid
41 41 # name conflicts for automagic to work on line magics.
42 42 del lmagic, lcmagic
43 43
44 44
45 45 You can also create magics of all three kinds by inheriting from the
46 46 :class:`IPython.core.magic.Magics` class. This lets you create magics that can
47 47 potentially hold state in between calls, and that have full access to the main
48 48 IPython object:
49 49
50 50 .. sourcecode:: python
51 51
52 52 # This code can be put in any Python module, it does not require IPython
53 53 # itself to be running already. It only creates the magics subclass but
54 54 # doesn't instantiate it yet.
55 55 from __future__ import print_function
56 56 from IPython.core.magic import (Magics, magics_class, line_magic,
57 57 cell_magic, line_cell_magic)
58 58
59 59 # The class MUST call this class decorator at creation time
60 60 @magics_class
61 61 class MyMagics(Magics):
62 62
63 63 @line_magic
64 64 def lmagic(self, line):
65 65 "my line magic"
66 66 print("Full access to the main IPython object:", self.shell)
67 67 print("Variables in the user namespace:", list(self.shell.user_ns.keys()))
68 68 return line
69 69
70 70 @cell_magic
71 71 def cmagic(self, line, cell):
72 72 "my cell magic"
73 73 return line, cell
74 74
75 75 @line_cell_magic
76 76 def lcmagic(self, line, cell=None):
77 77 "Magic that works both as %lcmagic and as %%lcmagic"
78 78 if cell is None:
79 79 print("Called as line magic")
80 80 return line
81 81 else:
82 82 print("Called as cell magic")
83 83 return line, cell
84 84
85 85
86 86 # In order to actually use these magics, you must register them with a
87 87 # running IPython.
88 88
89 89 def load_ipython_extension(ipython):
90 90 """
91 Any nodule file that define a function named load_ipython_extension can
92 be loaded via `load_ext` or be configured to be autoloaded by IPython at
93 startup time.
91 Any module file that define a function named `load_ipython_extension`
92 can be loaded via `%load_ext module.path` or be configured to be
93 autoloaded by IPython at startup time.
94 94 """
95 95 # You can register the class itself without instantiating it. IPython will
96 96 # call the default constructor on it.
97 97 ipython.register_magics(MyMagics)
98 98
99 99 If you want to create a class with a different constructor that holds
100 100 additional state, then you should always call the parent constructor and
101 101 instantiate the class yourself before registration:
102 102
103 103 .. sourcecode:: python
104 104
105 105 @magics_class
106 106 class StatefulMagics(Magics):
107 107 "Magics that hold additional state"
108 108
109 109 def __init__(self, shell, data):
110 110 # You must call the parent constructor
111 111 super(StatefulMagics, self).__init__(shell)
112 112 self.data = data
113 113
114 114 # etc...
115 115
116 116 def load_ipython_extension(ipython):
117 117 """
118 Any nodule file that define a function named load_ipython_extension can
119 be loaded via `load_ext` or be configured to be autoloaded by IPython at
120 startup time.
118 Any module file that define a function named `load_ipython_extension`
119 can be loaded via `%load_ext module.path` or be configured to be
120 autoloaded by IPython at startup time.
121 121 """
122 122 # This class must then be registered with a manually created instance,
123 123 # since its constructor has different arguments from the default:
124 magics = StatefulMagics(ip, some_data)
124 magics = StatefulMagics(ipython, some_data)
125 125 ipython.register_magics(magics)
126 126
127 127
128 128 .. note::
129 129
130 130 In early IPython versions 0.12 and before the line magics were
131 131 created using a :func:`define_magic` API function. This API has been
132 132 replaced with the above in IPython 0.13 and then completely removed
133 133 in IPython 5. Maintainers of IPython extensions that still use the
134 134 :func:`define_magic` function are advised to adjust their code
135 135 for the current API.
136
137 Complete Example
138 ================
139
140 Here is a full example of a magic package. You can distribute magics using
141 setuptools, distutils, or any other distribution tools like `flit
142 <http://flit.readthedocs.io>` for pure Python packages.
143
144
145 .. sourcecode::
146
147 .
148 ├── example_magic
149 │   ├── __init__.py
150 │   └── abracadabra.py
151 └── setup.py
152
153 .. sourcecode::
154
155 $ cat example_magic/__init__.py
156 """An example magic"""
157 __version__ = '0.0.1'
158
159 from .abracadabra import Abracadabra
160
161 def load_ipython_extension(ipython):
162 ipython.register_magics(Abracadabra)
163
164 .. sourcecode::
165
166 $ cat example_magic/abracadabra.py
167 from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic)
168
169 @magics_class
170 class Abracadabra(Magics):
171
172 @line_magic
173 def abra(self, line):
174 return line
175
176 @cell_magic
177 def cadabra(self, line, cell):
178 return line, cell
179
General Comments 0
You need to be logged in to leave comments. Login now