Show More
@@ -1,167 +1,151 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """A class for managing IPython extensions.""" |
|
2 | """A class for managing IPython extensions.""" | |
3 |
|
3 | |||
4 | # Copyright (c) IPython Development Team. |
|
4 | # Copyright (c) IPython Development Team. | |
5 | # Distributed under the terms of the Modified BSD License. |
|
5 | # Distributed under the terms of the Modified BSD License. | |
6 |
|
6 | |||
7 | import os |
|
7 | import os | |
8 | import os.path |
|
8 | import os.path | |
9 | import sys |
|
9 | import sys | |
10 | from importlib import import_module, reload |
|
10 | from importlib import import_module, reload | |
11 |
|
11 | |||
12 | from traitlets.config.configurable import Configurable |
|
12 | from traitlets.config.configurable import Configurable | |
13 | from IPython.utils.path import ensure_dir_exists, compress_user |
|
13 | from IPython.utils.path import ensure_dir_exists, compress_user | |
14 | from IPython.utils.decorators import undoc |
|
14 | from IPython.utils.decorators import undoc | |
15 | from traitlets import Instance |
|
15 | from traitlets import Instance | |
16 |
|
16 | |||
17 |
|
17 | |||
18 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
19 | # Main class |
|
19 | # Main class | |
20 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
21 |
|
21 | |||
22 | BUILTINS_EXTS = {"storemagic": False, "autoreload": False} |
|
22 | BUILTINS_EXTS = {"storemagic": False, "autoreload": False} | |
23 |
|
23 | |||
24 |
|
24 | |||
25 | class ExtensionManager(Configurable): |
|
25 | class ExtensionManager(Configurable): | |
26 | """A class to manage IPython extensions. |
|
26 | """A class to manage IPython extensions. | |
27 |
|
27 | |||
28 | An IPython extension is an importable Python module that has |
|
28 | An IPython extension is an importable Python module that has | |
29 | a function with the signature:: |
|
29 | a function with the signature:: | |
30 |
|
30 | |||
31 | def load_ipython_extension(ipython): |
|
31 | def load_ipython_extension(ipython): | |
32 | # Do things with ipython |
|
32 | # Do things with ipython | |
33 |
|
33 | |||
34 | This function is called after your extension is imported and the |
|
34 | This function is called after your extension is imported and the | |
35 | currently active :class:`InteractiveShell` instance is passed as |
|
35 | currently active :class:`InteractiveShell` instance is passed as | |
36 | the only argument. You can do anything you want with IPython at |
|
36 | the only argument. You can do anything you want with IPython at | |
37 | that point, including defining new magic and aliases, adding new |
|
37 | that point, including defining new magic and aliases, adding new | |
38 | components, etc. |
|
38 | components, etc. | |
39 |
|
39 | |||
40 | You can also optionally define an :func:`unload_ipython_extension(ipython)` |
|
40 | You can also optionally define an :func:`unload_ipython_extension(ipython)` | |
41 | function, which will be called if the user unloads or reloads the extension. |
|
41 | function, which will be called if the user unloads or reloads the extension. | |
42 | The extension manager will only call :func:`load_ipython_extension` again |
|
42 | The extension manager will only call :func:`load_ipython_extension` again | |
43 | if the extension is reloaded. |
|
43 | if the extension is reloaded. | |
44 |
|
44 | |||
45 | You can put your extension modules anywhere you want, as long as |
|
45 | You can put your extension modules anywhere you want, as long as | |
46 | they can be imported by Python's standard import mechanism. However, |
|
46 | they can be imported by Python's standard import mechanism. However, | |
47 | to make it easy to write extensions, you can also put your extensions |
|
47 | to make it easy to write extensions, you can also put your extensions | |
48 | in ``os.path.join(self.ipython_dir, 'extensions')``. This directory |
|
48 | in ``os.path.join(self.ipython_dir, 'extensions')``. This directory | |
49 | is added to ``sys.path`` automatically. |
|
49 | is added to ``sys.path`` automatically. | |
50 | """ |
|
50 | """ | |
51 |
|
51 | |||
52 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True) |
|
52 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True) | |
53 |
|
53 | |||
54 | def __init__(self, shell=None, **kwargs): |
|
54 | def __init__(self, shell=None, **kwargs): | |
55 | super(ExtensionManager, self).__init__(shell=shell, **kwargs) |
|
55 | super(ExtensionManager, self).__init__(shell=shell, **kwargs) | |
56 | self.shell.observe( |
|
56 | self.shell.observe( | |
57 | self._on_ipython_dir_changed, names=('ipython_dir',) |
|
57 | self._on_ipython_dir_changed, names=('ipython_dir',) | |
58 | ) |
|
58 | ) | |
59 | self.loaded = set() |
|
59 | self.loaded = set() | |
60 |
|
60 | |||
61 | @property |
|
61 | @property | |
62 | def ipython_extension_dir(self): |
|
62 | def ipython_extension_dir(self): | |
63 | return os.path.join(self.shell.ipython_dir, u'extensions') |
|
63 | return os.path.join(self.shell.ipython_dir, u'extensions') | |
64 |
|
64 | |||
65 | def _on_ipython_dir_changed(self, change): |
|
65 | def _on_ipython_dir_changed(self, change): | |
66 | ensure_dir_exists(self.ipython_extension_dir) |
|
66 | ensure_dir_exists(self.ipython_extension_dir) | |
67 |
|
67 | |||
68 | def load_extension(self, module_str: str): |
|
68 | def load_extension(self, module_str: str): | |
69 | """Load an IPython extension by its module name. |
|
69 | """Load an IPython extension by its module name. | |
70 |
|
70 | |||
71 | Returns the string "already loaded" if the extension is already loaded, |
|
71 | Returns the string "already loaded" if the extension is already loaded, | |
72 | "no load function" if the module doesn't have a load_ipython_extension |
|
72 | "no load function" if the module doesn't have a load_ipython_extension | |
73 | function, or None if it succeeded. |
|
73 | function, or None if it succeeded. | |
74 | """ |
|
74 | """ | |
75 | try: |
|
75 | try: | |
76 | return self._load_extension(module_str) |
|
76 | return self._load_extension(module_str) | |
77 | except ModuleNotFoundError: |
|
77 | except ModuleNotFoundError: | |
78 | if module_str in BUILTINS_EXTS: |
|
78 | if module_str in BUILTINS_EXTS: | |
79 | BUILTINS_EXTS[module_str] = True |
|
79 | BUILTINS_EXTS[module_str] = True | |
80 | return self._load_extension("IPython.extensions." + module_str) |
|
80 | return self._load_extension("IPython.extensions." + module_str) | |
81 | raise |
|
81 | raise | |
82 |
|
82 | |||
83 | def _load_extension(self, module_str: str): |
|
83 | def _load_extension(self, module_str: str): | |
84 | if module_str in self.loaded: |
|
84 | if module_str in self.loaded: | |
85 | return "already loaded" |
|
85 | return "already loaded" | |
86 |
|
86 | |||
87 | from IPython.utils.syspathcontext import prepended_to_syspath |
|
87 | from IPython.utils.syspathcontext import prepended_to_syspath | |
88 |
|
88 | |||
89 | with self.shell.builtin_trap: |
|
89 | with self.shell.builtin_trap: | |
90 | if module_str not in sys.modules: |
|
90 | if module_str not in sys.modules: | |
91 | with prepended_to_syspath(self.ipython_extension_dir): |
|
91 | mod = import_module(module_str) | |
92 | mod = import_module(module_str) |
|
|||
93 | if mod.__file__.startswith(self.ipython_extension_dir): |
|
|||
94 | print(("Loading extensions from {dir} is deprecated. " |
|
|||
95 | "We recommend managing extensions like any " |
|
|||
96 | "other Python packages, in site-packages.").format( |
|
|||
97 | dir=compress_user(self.ipython_extension_dir))) |
|
|||
98 | mod = sys.modules[module_str] |
|
92 | mod = sys.modules[module_str] | |
99 | if self._call_load_ipython_extension(mod): |
|
93 | if self._call_load_ipython_extension(mod): | |
100 | self.loaded.add(module_str) |
|
94 | self.loaded.add(module_str) | |
101 | else: |
|
95 | else: | |
102 | return "no load function" |
|
96 | return "no load function" | |
103 |
|
97 | |||
104 | def unload_extension(self, module_str: str): |
|
98 | def unload_extension(self, module_str: str): | |
105 | """Unload an IPython extension by its module name. |
|
99 | """Unload an IPython extension by its module name. | |
106 |
|
100 | |||
107 | This function looks up the extension's name in ``sys.modules`` and |
|
101 | This function looks up the extension's name in ``sys.modules`` and | |
108 | simply calls ``mod.unload_ipython_extension(self)``. |
|
102 | simply calls ``mod.unload_ipython_extension(self)``. | |
109 |
|
103 | |||
110 | Returns the string "no unload function" if the extension doesn't define |
|
104 | Returns the string "no unload function" if the extension doesn't define | |
111 | a function to unload itself, "not loaded" if the extension isn't loaded, |
|
105 | a function to unload itself, "not loaded" if the extension isn't loaded, | |
112 | otherwise None. |
|
106 | otherwise None. | |
113 | """ |
|
107 | """ | |
114 | if BUILTINS_EXTS.get(module_str, False) is True: |
|
108 | if BUILTINS_EXTS.get(module_str, False) is True: | |
115 | module_str = "IPython.extensions." + module_str |
|
109 | module_str = "IPython.extensions." + module_str | |
116 | if module_str not in self.loaded: |
|
110 | if module_str not in self.loaded: | |
117 | return "not loaded" |
|
111 | return "not loaded" | |
118 |
|
112 | |||
119 | if module_str in sys.modules: |
|
113 | if module_str in sys.modules: | |
120 | mod = sys.modules[module_str] |
|
114 | mod = sys.modules[module_str] | |
121 | if self._call_unload_ipython_extension(mod): |
|
115 | if self._call_unload_ipython_extension(mod): | |
122 | self.loaded.discard(module_str) |
|
116 | self.loaded.discard(module_str) | |
123 | else: |
|
117 | else: | |
124 | return "no unload function" |
|
118 | return "no unload function" | |
125 |
|
119 | |||
126 | def reload_extension(self, module_str: str): |
|
120 | def reload_extension(self, module_str: str): | |
127 | """Reload an IPython extension by calling reload. |
|
121 | """Reload an IPython extension by calling reload. | |
128 |
|
122 | |||
129 | If the module has not been loaded before, |
|
123 | If the module has not been loaded before, | |
130 | :meth:`InteractiveShell.load_extension` is called. Otherwise |
|
124 | :meth:`InteractiveShell.load_extension` is called. Otherwise | |
131 | :func:`reload` is called and then the :func:`load_ipython_extension` |
|
125 | :func:`reload` is called and then the :func:`load_ipython_extension` | |
132 | function of the module, if it exists is called. |
|
126 | function of the module, if it exists is called. | |
133 | """ |
|
127 | """ | |
134 | from IPython.utils.syspathcontext import prepended_to_syspath |
|
128 | from IPython.utils.syspathcontext import prepended_to_syspath | |
135 |
|
129 | |||
136 | if BUILTINS_EXTS.get(module_str, False) is True: |
|
130 | if BUILTINS_EXTS.get(module_str, False) is True: | |
137 | module_str = "IPython.extensions." + module_str |
|
131 | module_str = "IPython.extensions." + module_str | |
138 |
|
132 | |||
139 | if (module_str in self.loaded) and (module_str in sys.modules): |
|
133 | if (module_str in self.loaded) and (module_str in sys.modules): | |
140 | self.unload_extension(module_str) |
|
134 | self.unload_extension(module_str) | |
141 | mod = sys.modules[module_str] |
|
135 | mod = sys.modules[module_str] | |
142 | with prepended_to_syspath(self.ipython_extension_dir): |
|
136 | with prepended_to_syspath(self.ipython_extension_dir): | |
143 | reload(mod) |
|
137 | reload(mod) | |
144 | if self._call_load_ipython_extension(mod): |
|
138 | if self._call_load_ipython_extension(mod): | |
145 | self.loaded.add(module_str) |
|
139 | self.loaded.add(module_str) | |
146 | else: |
|
140 | else: | |
147 | self.load_extension(module_str) |
|
141 | self.load_extension(module_str) | |
148 |
|
142 | |||
149 | def _call_load_ipython_extension(self, mod): |
|
143 | def _call_load_ipython_extension(self, mod): | |
150 | if hasattr(mod, 'load_ipython_extension'): |
|
144 | if hasattr(mod, 'load_ipython_extension'): | |
151 | mod.load_ipython_extension(self.shell) |
|
145 | mod.load_ipython_extension(self.shell) | |
152 | return True |
|
146 | return True | |
153 |
|
147 | |||
154 | def _call_unload_ipython_extension(self, mod): |
|
148 | def _call_unload_ipython_extension(self, mod): | |
155 | if hasattr(mod, 'unload_ipython_extension'): |
|
149 | if hasattr(mod, 'unload_ipython_extension'): | |
156 | mod.unload_ipython_extension(self.shell) |
|
150 | mod.unload_ipython_extension(self.shell) | |
157 | return True |
|
151 | return True | |
158 |
|
||||
159 | @undoc |
|
|||
160 | def install_extension(self, url, filename=None): |
|
|||
161 | """ |
|
|||
162 | Deprecated. |
|
|||
163 | """ |
|
|||
164 | # Ensure the extension directory exists |
|
|||
165 | raise DeprecationWarning( |
|
|||
166 | '`install_extension` and the `install_ext` magic have been deprecated since IPython 4.0' |
|
|||
167 | 'Use pip or other package managers to manage ipython extensions.') |
|
@@ -1,103 +1,99 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 |
|
9 | and can be placed anywhere you'd normally import from. | |
10 | ``.ipython/extensions/``. |
|
|||
11 |
|
10 | |||
12 | Getting extensions |
|
11 | Getting extensions | |
13 | ================== |
|
12 | ================== | |
14 |
|
13 | |||
15 | A few important extensions are :ref:`bundled with IPython <bundled_extensions>`. |
|
14 | A few important extensions are :ref:`bundled with IPython <bundled_extensions>`. | |
16 | Others can be found on the `extensions index |
|
15 | Others can be found on the `extensions index | |
17 | <https://github.com/ipython/ipython/wiki/Extensions-Index>`_ on the wiki, and |
|
16 | <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>`_ |
|
17 | the `Framework :: IPython tag <https://pypi.python.org/pypi?:action=browse&c=586>`_ | |
19 | on PyPI. |
|
18 | on PyPI. | |
20 |
|
19 | |||
21 | Extensions on PyPI can be installed using ``pip``, like any other Python package. |
|
20 | Extensions on PyPI can be installed using ``pip``, like any other Python package. | |
22 |
|
21 | |||
23 | Using extensions |
|
22 | Using extensions | |
24 | ================ |
|
23 | ================ | |
25 |
|
24 | |||
26 | To load an extension while IPython is running, use the ``%load_ext`` magic: |
|
25 | To load an extension while IPython is running, use the ``%load_ext`` magic: | |
27 |
|
26 | |||
28 | .. sourcecode:: ipython |
|
27 | .. sourcecode:: ipython | |
29 |
|
28 | |||
30 | In [1]: %load_ext myextension |
|
29 | In [1]: %load_ext myextension | |
31 |
|
30 | |||
32 | To load it each time IPython starts, list it in your configuration file:: |
|
31 | To load it each time IPython starts, list it in your configuration file:: | |
33 |
|
32 | |||
34 | c.InteractiveShellApp.extensions = [ |
|
33 | c.InteractiveShellApp.extensions = [ | |
35 | 'myextension' |
|
34 | 'myextension' | |
36 | ] |
|
35 | ] | |
37 |
|
36 | |||
38 | Writing extensions |
|
37 | Writing extensions | |
39 | ================== |
|
38 | ================== | |
40 |
|
39 | |||
41 | An IPython extension is an importable Python module that has a couple of special |
|
40 | An IPython extension is an importable Python module that has a couple of special | |
42 | functions to load and unload it. Here is a template:: |
|
41 | functions to load and unload it. Here is a template:: | |
43 |
|
42 | |||
44 | # myextension.py |
|
43 | # myextension.py | |
45 |
|
44 | |||
46 | def load_ipython_extension(ipython): |
|
45 | def load_ipython_extension(ipython): | |
47 | # The `ipython` argument is the currently active `InteractiveShell` |
|
46 | # The `ipython` argument is the currently active `InteractiveShell` | |
48 | # instance, which can be used in any way. This allows you to register |
|
47 | # instance, which can be used in any way. This allows you to register | |
49 | # new magics or aliases, for example. |
|
48 | # new magics or aliases, for example. | |
50 |
|
49 | |||
51 | def unload_ipython_extension(ipython): |
|
50 | def unload_ipython_extension(ipython): | |
52 | # If you want your extension to be unloadable, put that logic here. |
|
51 | # If you want your extension to be unloadable, put that logic here. | |
53 |
|
52 | |||
54 | This :func:`load_ipython_extension` function is called after your extension is |
|
53 | This :func:`load_ipython_extension` function is called after your extension is | |
55 | imported, and the currently active :class:`~IPython.core.interactiveshell.InteractiveShell` |
|
54 | imported, and the currently active :class:`~IPython.core.interactiveshell.InteractiveShell` | |
56 | instance is passed as the only argument. You can do anything you want with |
|
55 | instance is passed as the only argument. You can do anything you want with | |
57 | IPython at that point. |
|
56 | IPython at that point. | |
58 |
|
57 | |||
59 | :func:`load_ipython_extension` will not be called again if the user use |
|
58 | :func:`load_ipython_extension` will not be called again if the user use | |
60 | `%load_extension`. The user have to explicitly ask the extension to be |
|
59 | `%load_extension`. The user have to explicitly ask the extension to be | |
61 | reloaded (with `%reload_extension`). In case where the use ask the extension to |
|
60 | reloaded (with `%reload_extension`). In case where the use ask the extension to | |
62 | be reloaded, , the extension will be unloaded (with |
|
61 | be reloaded, , the extension will be unloaded (with | |
63 | `unload_ipython_extension`), and loaded again. |
|
62 | `unload_ipython_extension`), and loaded again. | |
64 |
|
63 | |||
65 | Useful :class:`InteractiveShell` methods include :meth:`~IPython.core.interactiveshell.InteractiveShell.register_magic_function`, |
|
64 | Useful :class:`InteractiveShell` methods include :meth:`~IPython.core.interactiveshell.InteractiveShell.register_magic_function`, | |
66 | :meth:`~IPython.core.interactiveshell.InteractiveShell.push` (to add variables to the user namespace) and |
|
65 | :meth:`~IPython.core.interactiveshell.InteractiveShell.push` (to add variables to the user namespace) and | |
67 | :meth:`~IPython.core.interactiveshell.InteractiveShell.drop_by_id` (to remove variables on unloading). |
|
66 | :meth:`~IPython.core.interactiveshell.InteractiveShell.drop_by_id` (to remove variables on unloading). | |
68 |
|
67 | |||
69 | .. seealso:: |
|
68 | .. seealso:: | |
70 |
|
69 | |||
71 | :ref:`defining_magics` |
|
70 | :ref:`defining_magics` | |
72 |
|
71 | |||
73 | You can put your extension modules anywhere you want, as long as they can be |
|
72 | You can put your extension modules anywhere you want, as long as they can be | |
74 |
imported by Python's standard import mechanism. |
|
73 | imported by Python's standard import mechanism. | |
75 | write extensions, you can also put your extensions in :file:`extensions/` |
|
|||
76 | within the :ref:`IPython directory <ipythondir>`. This directory is |
|
|||
77 | added to :data:`sys.path` automatically. |
|
|||
78 |
|
74 | |||
79 | When your extension is ready for general use, please add it to the `extensions |
|
75 | When your extension is ready for general use, please add it to the `extensions | |
80 | index <https://github.com/ipython/ipython/wiki/Extensions-Index>`_. We also |
|
76 | index <https://github.com/ipython/ipython/wiki/Extensions-Index>`_. We also | |
81 | encourage you to upload it to PyPI and use the ``Framework :: IPython`` |
|
77 | encourage you to upload it to PyPI and use the ``Framework :: IPython`` | |
82 | classifier, so that users can install it with standard packaging tools. |
|
78 | classifier, so that users can install it with standard packaging tools. | |
83 |
|
79 | |||
84 | .. _bundled_extensions: |
|
80 | .. _bundled_extensions: | |
85 |
|
81 | |||
86 | Extensions bundled with IPython |
|
82 | Extensions bundled with IPython | |
87 | =============================== |
|
83 | =============================== | |
88 |
|
84 | |||
89 | .. toctree:: |
|
85 | .. toctree:: | |
90 | :maxdepth: 1 |
|
86 | :maxdepth: 1 | |
91 |
|
87 | |||
92 | autoreload |
|
88 | autoreload | |
93 | storemagic |
|
89 | storemagic | |
94 |
|
90 | |||
95 | * ``octavemagic`` used to be bundled, but is now part of `oct2py <https://blink1073.github.io/oct2py/>`_. |
|
91 | * ``octavemagic`` used to be bundled, but is now part of `oct2py <https://blink1073.github.io/oct2py/>`_. | |
96 | Use ``%load_ext oct2py.ipython`` to load it. |
|
92 | Use ``%load_ext oct2py.ipython`` to load it. | |
97 | * ``rmagic`` is now part of `rpy2 <http://rpy.sourceforge.net/>`_. Use |
|
93 | * ``rmagic`` is now part of `rpy2 <http://rpy.sourceforge.net/>`_. Use | |
98 | ``%load_ext rpy2.ipython`` to load it, and see :mod:`rpy2.ipython.rmagic` for |
|
94 | ``%load_ext rpy2.ipython`` to load it, and see :mod:`rpy2.ipython.rmagic` for | |
99 | details of how to use it. |
|
95 | details of how to use it. | |
100 | * ``cythonmagic`` used to be bundled, but is now part of `cython <https://github.com/cython/cython/>`_ |
|
96 | * ``cythonmagic`` used to be bundled, but is now part of `cython <https://github.com/cython/cython/>`_ | |
101 | Use ``%load_ext Cython`` to load it. |
|
97 | Use ``%load_ext Cython`` to load it. | |
102 | * ``sympyprinting`` used to be a bundled extension, but you should now use |
|
98 | * ``sympyprinting`` used to be a bundled extension, but you should now use | |
103 | :func:`sympy.init_printing` instead. |
|
99 | :func:`sympy.init_printing` instead. |
General Comments 0
You need to be logged in to leave comments.
Login now