Show More
@@ -1,93 +1,96 | |||||
1 | """Implementation of magic functions for the extension machinery. |
|
1 | """Implementation of magic functions for the extension machinery. | |
2 | """ |
|
2 | """ | |
3 | from __future__ import print_function |
|
3 | from __future__ import print_function | |
4 | #----------------------------------------------------------------------------- |
|
4 | #----------------------------------------------------------------------------- | |
5 | # Copyright (c) 2012 The IPython Development Team. |
|
5 | # Copyright (c) 2012 The IPython Development Team. | |
6 | # |
|
6 | # | |
7 | # Distributed under the terms of the Modified BSD License. |
|
7 | # Distributed under the terms of the Modified BSD License. | |
8 | # |
|
8 | # | |
9 | # The full license is in the file COPYING.txt, distributed with this software. |
|
9 | # The full license is in the file COPYING.txt, distributed with this software. | |
10 | #----------------------------------------------------------------------------- |
|
10 | #----------------------------------------------------------------------------- | |
11 |
|
11 | |||
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 | # Imports |
|
13 | # Imports | |
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 |
|
15 | |||
16 | # Stdlib |
|
16 | # Stdlib | |
17 | import os |
|
17 | import os | |
18 |
|
18 | |||
19 | # Our own packages |
|
19 | # Our own packages | |
20 | from IPython.core.error import UsageError |
|
20 | from IPython.core.error import UsageError | |
21 | from IPython.core.magic import Magics, magics_class, line_magic |
|
21 | from IPython.core.magic import Magics, magics_class, line_magic | |
|
22 | from warnings import warn | |||
22 |
|
23 | |||
23 | #----------------------------------------------------------------------------- |
|
24 | #----------------------------------------------------------------------------- | |
24 | # Magic implementation classes |
|
25 | # Magic implementation classes | |
25 | #----------------------------------------------------------------------------- |
|
26 | #----------------------------------------------------------------------------- | |
26 |
|
27 | |||
27 | @magics_class |
|
28 | @magics_class | |
28 | class ExtensionMagics(Magics): |
|
29 | class ExtensionMagics(Magics): | |
29 | """Magics to manage the IPython extensions system.""" |
|
30 | """Magics to manage the IPython extensions system.""" | |
30 |
|
31 | |||
31 | @line_magic |
|
32 | @line_magic | |
32 | def install_ext(self, parameter_s=''): |
|
33 | def install_ext(self, parameter_s=''): | |
33 | """Download and install an extension from a URL, e.g.:: |
|
34 | """Download and install an extension from a URL, e.g.:: | |
34 |
|
35 | |||
35 | %install_ext https://bitbucket.org/birkenfeld/ipython-physics/raw/d1310a2ab15d/physics.py |
|
36 | %install_ext https://bitbucket.org/birkenfeld/ipython-physics/raw/d1310a2ab15d/physics.py | |
36 |
|
37 | |||
37 | The URL should point to an importable Python module - either a .py file |
|
38 | The URL should point to an importable Python module - either a .py file | |
38 | or a .zip file. |
|
39 | or a .zip file. | |
39 |
|
40 | |||
40 | Parameters: |
|
41 | Parameters: | |
41 |
|
42 | |||
42 | -n filename : Specify a name for the file, rather than taking it from |
|
43 | -n filename : Specify a name for the file, rather than taking it from | |
43 | the URL. |
|
44 | the URL. | |
44 | """ |
|
45 | """ | |
|
46 | warn("%install_ext` is deprecated, please distribute your extension(s)" | |||
|
47 | "as a python packages.", UserWarning) | |||
45 | opts, args = self.parse_options(parameter_s, 'n:') |
|
48 | opts, args = self.parse_options(parameter_s, 'n:') | |
46 | try: |
|
49 | try: | |
47 | filename = self.shell.extension_manager.install_extension(args, |
|
50 | filename = self.shell.extension_manager.install_extension(args, | |
48 | opts.get('n')) |
|
51 | opts.get('n')) | |
49 | except ValueError as e: |
|
52 | except ValueError as e: | |
50 | print(e) |
|
53 | print(e) | |
51 | return |
|
54 | return | |
52 |
|
55 | |||
53 | filename = os.path.basename(filename) |
|
56 | filename = os.path.basename(filename) | |
54 | print("Installed %s. To use it, type:" % filename) |
|
57 | print("Installed %s. To use it, type:" % filename) | |
55 | print(" %%load_ext %s" % os.path.splitext(filename)[0]) |
|
58 | print(" %%load_ext %s" % os.path.splitext(filename)[0]) | |
56 |
|
59 | |||
57 |
|
60 | |||
58 | @line_magic |
|
61 | @line_magic | |
59 | def load_ext(self, module_str): |
|
62 | def load_ext(self, module_str): | |
60 | """Load an IPython extension by its module name.""" |
|
63 | """Load an IPython extension by its module name.""" | |
61 | if not module_str: |
|
64 | if not module_str: | |
62 | raise UsageError('Missing module name.') |
|
65 | raise UsageError('Missing module name.') | |
63 | res = self.shell.extension_manager.load_extension(module_str) |
|
66 | res = self.shell.extension_manager.load_extension(module_str) | |
64 |
|
67 | |||
65 | if res == 'already loaded': |
|
68 | if res == 'already loaded': | |
66 | print("The %s extension is already loaded. To reload it, use:" % module_str) |
|
69 | print("The %s extension is already loaded. To reload it, use:" % module_str) | |
67 | print(" %reload_ext", module_str) |
|
70 | print(" %reload_ext", module_str) | |
68 | elif res == 'no load function': |
|
71 | elif res == 'no load function': | |
69 | print("The %s module is not an IPython extension." % module_str) |
|
72 | print("The %s module is not an IPython extension." % module_str) | |
70 |
|
73 | |||
71 | @line_magic |
|
74 | @line_magic | |
72 | def unload_ext(self, module_str): |
|
75 | def unload_ext(self, module_str): | |
73 | """Unload an IPython extension by its module name. |
|
76 | """Unload an IPython extension by its module name. | |
74 |
|
77 | |||
75 | Not all extensions can be unloaded, only those which define an |
|
78 | Not all extensions can be unloaded, only those which define an | |
76 | ``unload_ipython_extension`` function. |
|
79 | ``unload_ipython_extension`` function. | |
77 | """ |
|
80 | """ | |
78 | if not module_str: |
|
81 | if not module_str: | |
79 | raise UsageError('Missing module name.') |
|
82 | raise UsageError('Missing module name.') | |
80 |
|
83 | |||
81 | res = self.shell.extension_manager.unload_extension(module_str) |
|
84 | res = self.shell.extension_manager.unload_extension(module_str) | |
82 |
|
85 | |||
83 | if res == 'no unload function': |
|
86 | if res == 'no unload function': | |
84 | print("The %s extension doesn't define how to unload it." % module_str) |
|
87 | print("The %s extension doesn't define how to unload it." % module_str) | |
85 | elif res == "not loaded": |
|
88 | elif res == "not loaded": | |
86 | print("The %s extension is not loaded." % module_str) |
|
89 | print("The %s extension is not loaded." % module_str) | |
87 |
|
90 | |||
88 | @line_magic |
|
91 | @line_magic | |
89 | def reload_ext(self, module_str): |
|
92 | def reload_ext(self, module_str): | |
90 | """Reload an IPython extension by its module name.""" |
|
93 | """Reload an IPython extension by its module name.""" | |
91 | if not module_str: |
|
94 | if not module_str: | |
92 | raise UsageError('Missing module name.') |
|
95 | raise UsageError('Missing module name.') | |
93 | self.shell.extension_manager.reload_extension(module_str) |
|
96 | self.shell.extension_manager.reload_extension(module_str) |
@@ -1,103 +1,100 | |||||
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 `Framework :: IPython tag <https://pypi.python.org/pypi?:action=browse&c=586>`_ |
|
18 | the `Framework :: IPython tag <https://pypi.python.org/pypi?:action=browse&c=586>`_ | |
19 | on PyPI. |
|
19 | on PyPI. | |
20 |
|
20 | |||
21 | Extensions on PyPI can be installed using ``pip``, like any other Python package. |
|
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. |
|
|||
25 |
|
22 | |||
26 | Using extensions |
|
23 | Using extensions | |
27 | ================ |
|
24 | ================ | |
28 |
|
25 | |||
29 | To load an extension while IPython is running, use the ``%load_ext`` magic: |
|
26 | To load an extension while IPython is running, use the ``%load_ext`` magic: | |
30 |
|
27 | |||
31 | .. sourcecode:: ipython |
|
28 | .. sourcecode:: ipython | |
32 |
|
29 | |||
33 | In [1]: %load_ext myextension |
|
30 | In [1]: %load_ext myextension | |
34 |
|
31 | |||
35 | To load it each time IPython starts, list it in your configuration file:: |
|
32 | To load it each time IPython starts, list it in your configuration file:: | |
36 |
|
33 | |||
37 | c.InteractiveShellApp.extensions = [ |
|
34 | c.InteractiveShellApp.extensions = [ | |
38 | 'myextension' |
|
35 | 'myextension' | |
39 | ] |
|
36 | ] | |
40 |
|
37 | |||
41 | Writing extensions |
|
38 | Writing extensions | |
42 | ================== |
|
39 | ================== | |
43 |
|
40 | |||
44 | An IPython extension is an importable Python module that has a couple of special |
|
41 | An IPython extension is an importable Python module that has a couple of special | |
45 | functions to load and unload it. Here is a template:: |
|
42 | functions to load and unload it. Here is a template:: | |
46 |
|
43 | |||
47 | # myextension.py |
|
44 | # myextension.py | |
48 |
|
45 | |||
49 | def load_ipython_extension(ipython): |
|
46 | def load_ipython_extension(ipython): | |
50 | # The `ipython` argument is the currently active `InteractiveShell` |
|
47 | # The `ipython` argument is the currently active `InteractiveShell` | |
51 | # instance, which can be used in any way. This allows you to register |
|
48 | # instance, which can be used in any way. This allows you to register | |
52 | # new magics or aliases, for example. |
|
49 | # new magics or aliases, for example. | |
53 |
|
50 | |||
54 | def unload_ipython_extension(ipython): |
|
51 | def unload_ipython_extension(ipython): | |
55 | # If you want your extension to be unloadable, put that logic here. |
|
52 | # If you want your extension to be unloadable, put that logic here. | |
56 |
|
53 | |||
57 | This :func:`load_ipython_extension` function is called after your extension is |
|
54 | This :func:`load_ipython_extension` function is called after your extension is | |
58 | imported, and the currently active :class:`~IPython.core.interactiveshell.InteractiveShell` |
|
55 | imported, and the currently active :class:`~IPython.core.interactiveshell.InteractiveShell` | |
59 | instance is passed as the only argument. You can do anything you want with |
|
56 | instance is passed as the only argument. You can do anything you want with | |
60 | IPython at that point. |
|
57 | IPython at that point. | |
61 |
|
58 | |||
62 | :func:`load_ipython_extension` will be called again if you load or reload |
|
59 | :func:`load_ipython_extension` will be called again if you load or reload | |
63 | the extension again. It is up to the extension author to add code to manage |
|
60 | the extension again. It is up to the extension author to add code to manage | |
64 | that. |
|
61 | that. | |
65 |
|
62 | |||
66 | Useful :class:`InteractiveShell` methods include :meth:`~IPython.core.interactiveshell.InteractiveShell.register_magic_function`, |
|
63 | Useful :class:`InteractiveShell` methods include :meth:`~IPython.core.interactiveshell.InteractiveShell.register_magic_function`, | |
67 | :meth:`~IPython.core.interactiveshell.InteractiveShell.push` (to add variables to the user namespace) and |
|
64 | :meth:`~IPython.core.interactiveshell.InteractiveShell.push` (to add variables to the user namespace) and | |
68 | :meth:`~IPython.core.interactiveshell.InteractiveShell.drop_by_id` (to remove variables on unloading). |
|
65 | :meth:`~IPython.core.interactiveshell.InteractiveShell.drop_by_id` (to remove variables on unloading). | |
69 |
|
66 | |||
70 | .. seealso:: |
|
67 | .. seealso:: | |
71 |
|
68 | |||
72 | :ref:`defining_magics` |
|
69 | :ref:`defining_magics` | |
73 |
|
70 | |||
74 | You can put your extension modules anywhere you want, as long as they can be |
|
71 | You can put your extension modules anywhere you want, as long as they can be | |
75 | imported by Python's standard import mechanism. However, to make it easy to |
|
72 | imported by Python's standard import mechanism. However, to make it easy to | |
76 | write extensions, you can also put your extensions in :file:`extensions/` |
|
73 | write extensions, you can also put your extensions in :file:`extensions/` | |
77 | within the :ref:`IPython directory <ipythondir>`. This directory is |
|
74 | within the :ref:`IPython directory <ipythondir>`. This directory is | |
78 | added to :data:`sys.path` automatically. |
|
75 | added to :data:`sys.path` automatically. | |
79 |
|
76 | |||
80 | When your extension is ready for general use, please add it to the `extensions |
|
77 | When your extension is ready for general use, please add it to the `extensions | |
81 | index <https://github.com/ipython/ipython/wiki/Extensions-Index>`_. We also |
|
78 | index <https://github.com/ipython/ipython/wiki/Extensions-Index>`_. We also | |
82 | encourage you to upload it to PyPI and use the ``Framework :: IPython`` |
|
79 | encourage you to upload it to PyPI and use the ``Framework :: IPython`` | |
83 | classifier, so that users can install it with standard packaging tools. |
|
80 | classifier, so that users can install it with standard packaging tools. | |
84 |
|
81 | |||
85 | .. _bundled_extensions: |
|
82 | .. _bundled_extensions: | |
86 |
|
83 | |||
87 | Extensions bundled with IPython |
|
84 | Extensions bundled with IPython | |
88 | =============================== |
|
85 | =============================== | |
89 |
|
86 | |||
90 | .. toctree:: |
|
87 | .. toctree:: | |
91 | :maxdepth: 1 |
|
88 | :maxdepth: 1 | |
92 |
|
89 | |||
93 | autoreload |
|
90 | autoreload | |
94 | storemagic |
|
91 | storemagic | |
95 | sympyprinting |
|
92 | sympyprinting | |
96 |
|
93 | |||
97 | * ``octavemagic`` used to be bundled, but is now part of `oct2py <http://blink1073.github.io/oct2py/docs/>`_. |
|
94 | * ``octavemagic`` used to be bundled, but is now part of `oct2py <http://blink1073.github.io/oct2py/docs/>`_. | |
98 | Use ``%load_ext oct2py.ipython`` to load it. |
|
95 | Use ``%load_ext oct2py.ipython`` to load it. | |
99 | * ``rmagic`` is now part of `rpy2 <http://rpy.sourceforge.net/>`_. Use |
|
96 | * ``rmagic`` is now part of `rpy2 <http://rpy.sourceforge.net/>`_. Use | |
100 | ``%load_ext rpy2.ipython`` to load it, and see :mod:`rpy2.ipython.rmagic` for |
|
97 | ``%load_ext rpy2.ipython`` to load it, and see :mod:`rpy2.ipython.rmagic` for | |
101 | details of how to use it. |
|
98 | details of how to use it. | |
102 | * ``cythonmagic`` used to be bundled, but is now part of `cython <https://github.com/cython/cython/>`_ |
|
99 | * ``cythonmagic`` used to be bundled, but is now part of `cython <https://github.com/cython/cython/>`_ | |
103 | Use ``%load_ext Cython`` to load it. |
|
100 | Use ``%load_ext Cython`` to load it. |
General Comments 0
You need to be logged in to leave comments.
Login now