Show More
@@ -1,92 +1,100 b'' | |||||
1 | .. _extensions_overview: |
|
1 | .. _extensions_overview: | |
2 |
|
2 | |||
3 | ================== |
|
3 | ================== | |
4 | IPython extensions |
|
4 | IPython extensions | |
5 | ================== |
|
5 | ================== | |
6 |
|
6 | |||
7 | A level above configuration are IPython extensions, Python modules which modify |
|
7 | A level above configuration are IPython extensions, Python modules which modify | |
8 | the behaviour of the shell. They are referred to by an importable module name, |
|
8 | the behaviour of the shell. They are referred to by an importable module name, | |
9 | and can be placed anywhere you'd normally import from, or in |
|
9 | and can be placed anywhere you'd normally import from, or in | |
10 | ``.ipython/extensions/``. |
|
10 | ``.ipython/extensions/``. | |
11 |
|
11 | |||
12 | Getting extensions |
|
12 | Getting extensions | |
13 | ================== |
|
13 | ================== | |
14 |
|
14 | |||
15 | A few important extensions are :ref:`bundled with IPython <bundled_extensions>`. |
|
15 | A few important extensions are :ref:`bundled with IPython <bundled_extensions>`. | |
16 | Others can be found on the `extensions index |
|
16 | Others can be found on the `extensions index | |
17 |
<https://github.com/ipython/ipython/wiki/Extensions-Index>`_ on the wiki, and |
|
17 | <https://github.com/ipython/ipython/wiki/Extensions-Index>`_ on the wiki, and | |
18 | the ``%install_ext`` magic function. |
|
18 | the `Framework :: IPython tag <https://pypi.python.org/pypi?:action=browse&c=586>`_ | |
|
19 | on PyPI. | |||
|
20 | ||||
|
21 | Extensions on PyPI can be installed using ``pip``, like any other Python package. | |||
|
22 | Other simple extensions can be installed with the ``%install_ext`` magic. The | |||
|
23 | latter does no validation, so be careful using it on untrusted networks like | |||
|
24 | public wifi. | |||
19 |
|
25 | |||
20 | Using extensions |
|
26 | Using extensions | |
21 | ================ |
|
27 | ================ | |
22 |
|
28 | |||
23 | To load an extension while IPython is running, use the ``%load_ext`` magic: |
|
29 | To load an extension while IPython is running, use the ``%load_ext`` magic: | |
24 |
|
30 | |||
25 | .. sourcecode:: ipython |
|
31 | .. sourcecode:: ipython | |
26 |
|
32 | |||
27 | In [1]: %load_ext myextension |
|
33 | In [1]: %load_ext myextension | |
28 |
|
34 | |||
29 | To load it each time IPython starts, list it in your configuration file:: |
|
35 | To load it each time IPython starts, list it in your configuration file:: | |
30 |
|
36 | |||
31 | c.InteractiveShellApp.extensions = [ |
|
37 | c.InteractiveShellApp.extensions = [ | |
32 | 'myextension' |
|
38 | 'myextension' | |
33 | ] |
|
39 | ] | |
34 |
|
40 | |||
35 | Writing extensions |
|
41 | Writing extensions | |
36 | ================== |
|
42 | ================== | |
37 |
|
43 | |||
38 | An IPython extension is an importable Python module that has a couple of special |
|
44 | An IPython extension is an importable Python module that has a couple of special | |
39 | functions to load and unload it. Here is a template:: |
|
45 | functions to load and unload it. Here is a template:: | |
40 |
|
46 | |||
41 | # myextension.py |
|
47 | # myextension.py | |
42 |
|
48 | |||
43 | def load_ipython_extension(ipython): |
|
49 | def load_ipython_extension(ipython): | |
44 | # The `ipython` argument is the currently active `InteractiveShell` |
|
50 | # The `ipython` argument is the currently active `InteractiveShell` | |
45 | # instance, which can be used in any way. This allows you to register |
|
51 | # instance, which can be used in any way. This allows you to register | |
46 | # new magics or aliases, for example. |
|
52 | # new magics or aliases, for example. | |
47 |
|
53 | |||
48 | def unload_ipython_extension(ipython): |
|
54 | def unload_ipython_extension(ipython): | |
49 | # If you want your extension to be unloadable, put that logic here. |
|
55 | # If you want your extension to be unloadable, put that logic here. | |
50 |
|
56 | |||
51 | This :func:`load_ipython_extension` function is called after your extension is |
|
57 | This :func:`load_ipython_extension` function is called after your extension is | |
52 | imported, and the currently active :class:`~IPython.core.interactiveshell.InteractiveShell` |
|
58 | imported, and the currently active :class:`~IPython.core.interactiveshell.InteractiveShell` | |
53 | instance is passed as the only argument. You can do anything you want with |
|
59 | instance is passed as the only argument. You can do anything you want with | |
54 | IPython at that point. |
|
60 | IPython at that point. | |
55 |
|
61 | |||
56 | :func:`load_ipython_extension` will be called again if you load or reload |
|
62 | :func:`load_ipython_extension` will be called again if you load or reload | |
57 | the extension again. It is up to the extension author to add code to manage |
|
63 | the extension again. It is up to the extension author to add code to manage | |
58 | that. |
|
64 | that. | |
59 |
|
65 | |||
60 | Useful :class:`InteractiveShell` methods include :meth:`~IPython.core.interactiveshell.InteractiveShell.register_magic_function`, |
|
66 | Useful :class:`InteractiveShell` methods include :meth:`~IPython.core.interactiveshell.InteractiveShell.register_magic_function`, | |
61 | :meth:`~IPython.core.interactiveshell.InteractiveShell.push` (to add variables to the user namespace) and |
|
67 | :meth:`~IPython.core.interactiveshell.InteractiveShell.push` (to add variables to the user namespace) and | |
62 | :meth:`~IPython.core.interactiveshell.InteractiveShell.drop_by_id` (to remove variables on unloading). |
|
68 | :meth:`~IPython.core.interactiveshell.InteractiveShell.drop_by_id` (to remove variables on unloading). | |
63 |
|
69 | |||
64 | .. seealso:: |
|
70 | .. seealso:: | |
65 |
|
71 | |||
66 | :ref:`defining_magics` |
|
72 | :ref:`defining_magics` | |
67 |
|
73 | |||
68 | You can put your extension modules anywhere you want, as long as they can be |
|
74 | You can put your extension modules anywhere you want, as long as they can be | |
69 | imported by Python's standard import mechanism. However, to make it easy to |
|
75 | imported by Python's standard import mechanism. However, to make it easy to | |
70 | write extensions, you can also put your extensions in :file:`extensions/` |
|
76 | write extensions, you can also put your extensions in :file:`extensions/` | |
71 | within the :ref:`IPython directory <ipythondir>`. This directory is |
|
77 | within the :ref:`IPython directory <ipythondir>`. This directory is | |
72 | added to :data:`sys.path` automatically. |
|
78 | added to :data:`sys.path` automatically. | |
73 |
|
79 | |||
74 | When your extension is ready for general use, please add it to the `extensions |
|
80 | When your extension is ready for general use, please add it to the `extensions | |
75 | index <https://github.com/ipython/ipython/wiki/Extensions-Index>`_. |
|
81 | index <https://github.com/ipython/ipython/wiki/Extensions-Index>`_. We also | |
|
82 | encourage you to upload it to PyPI and use the ``Framework :: IPython`` | |||
|
83 | classifier, so that users can install it with standard packaging tools. | |||
76 |
|
84 | |||
77 | .. _bundled_extensions: |
|
85 | .. _bundled_extensions: | |
78 |
|
86 | |||
79 | Extensions bundled with IPython |
|
87 | Extensions bundled with IPython | |
80 | =============================== |
|
88 | =============================== | |
81 |
|
89 | |||
82 | .. toctree:: |
|
90 | .. toctree:: | |
83 | :maxdepth: 1 |
|
91 | :maxdepth: 1 | |
84 |
|
92 | |||
85 | autoreload |
|
93 | autoreload | |
86 | cythonmagic |
|
94 | cythonmagic | |
87 | rmagic |
|
95 | rmagic | |
88 | storemagic |
|
96 | storemagic | |
89 | sympyprinting |
|
97 | sympyprinting | |
90 |
|
98 | |||
91 | * ``octavemagic`` used to be bundled, but is now part of `oct2py <http://blink1073.github.io/oct2py/docs/>`_. |
|
99 | * ``octavemagic`` used to be bundled, but is now part of `oct2py <http://blink1073.github.io/oct2py/docs/>`_. | |
92 | Use ``%load_ext oct2py.ipython`` to load it. |
|
100 | Use ``%load_ext oct2py.ipython`` to load it. |
General Comments 0
You need to be logged in to leave comments.
Login now