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