Show More
@@ -1,78 +1,77 | |||
|
1 | 1 | .. _extensions_overview: |
|
2 | 2 | |
|
3 | 3 | ================== |
|
4 | 4 | IPython extensions |
|
5 | 5 | ================== |
|
6 | 6 | |
|
7 | 7 | Configuration files are just the first level of customization that IPython |
|
8 | 8 | supports. The next level is that of extensions. An IPython extension is an |
|
9 | 9 | importable Python module that has a a few special function. By defining these |
|
10 | 10 | functions, users can customize IPython by accessing the actual runtime objects |
|
11 | 11 | of IPython. Here is a sample extension:: |
|
12 | 12 | |
|
13 | 13 | # myextension.py |
|
14 | 14 | |
|
15 | 15 | def load_ipython_extension(ipython): |
|
16 | 16 | # The ``ipython`` argument is the currently active |
|
17 | 17 | # :class:`InteractiveShell` instance that can be used in any way. |
|
18 | 18 | # This allows you do to things like register new magics, plugins or |
|
19 | 19 | # aliases. |
|
20 | 20 | |
|
21 | 21 | def unload_ipython_extension(ipython): |
|
22 | 22 | # If you want your extension to be unloadable, put that logic here. |
|
23 | 23 | |
|
24 | 24 | This :func:`load_ipython_extension` function is called after your extension is |
|
25 | 25 | imported and the currently active :class:`InteractiveShell` instance is passed |
|
26 | 26 | as the only argument. You can do anything you want with IPython at that point. |
|
27 | 27 | |
|
28 | 28 | The :func:`load_ipython_extension` will be called again is you load or reload |
|
29 | 29 | the extension again. It is up to the extension author to add code to manage |
|
30 | 30 | that. |
|
31 | 31 | |
|
32 | 32 | Useful :class:`InteractiveShell` methods include :meth:`~IPython.core.interactiveshell.InteractiveShell.define_magic`, |
|
33 | 33 | :meth:`~IPython.core.interactiveshell.InteractiveShell.push` (to add variables to the user namespace) and |
|
34 | 34 | :meth:`~IPython.core.interactiveshell.InteractiveShell.drop_by_id` (to remove variables on unloading). |
|
35 | 35 | |
|
36 | 36 | You can put your extension modules anywhere you want, as long as they can be |
|
37 | 37 | imported by Python's standard import mechanism. However, to make it easy to |
|
38 | 38 | write extensions, you can also put your extensions in |
|
39 | 39 | ``os.path.join(self.ipython_dir, 'extensions')``. This directory is added to |
|
40 | 40 | ``sys.path`` automatically. |
|
41 | 41 | |
|
42 | 42 | Using extensions |
|
43 | 43 | ================ |
|
44 | 44 | |
|
45 | 45 | There are two ways you can tell IPython to use your extension: |
|
46 | 46 | |
|
47 | 47 | 1. Listing it in a configuration file. |
|
48 | 48 | 2. Using the ``%load_ext`` magic function. |
|
49 | 49 | |
|
50 | 50 | To load an extension called :file:`myextension.py` add the following logic |
|
51 | 51 | to your configuration file:: |
|
52 | 52 | |
|
53 | 53 | c.InteractiveShellApp.extensions = [ |
|
54 | 54 | 'myextension' |
|
55 | 55 | ] |
|
56 | 56 | |
|
57 | 57 | To load that same extension at runtime, use the ``%load_ext`` magic: |
|
58 | 58 | |
|
59 | 59 | .. sourcecode:: ipython |
|
60 | 60 | |
|
61 | 61 | In [1]: %load_ext myextension |
|
62 | 62 | |
|
63 | 63 | To summarize, in conjunction with configuration files and profiles, IPython |
|
64 | 64 | extensions give you complete and flexible control over your IPython |
|
65 | 65 | setup. |
|
66 | 66 | |
|
67 | 67 | Extensions bundled with IPython |
|
68 | 68 | =============================== |
|
69 | 69 | |
|
70 | 70 | .. toctree:: |
|
71 | 71 | :maxdepth: 1 |
|
72 | 72 | |
|
73 | 73 | autoreload |
|
74 | 74 | cythonmagic |
|
75 | parallelmagic | |
|
76 | 75 | rmagic |
|
77 | 76 | storemagic |
|
78 | 77 | sympyprinting |
|
1 | NO CONTENT: file was removed |
General Comments 0
You need to be logged in to leave comments.
Login now