Show More
@@ -1,61 +1,61 b'' | |||
|
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 | You can put your extension modules anywhere you want, as long as they can be |
|
33 | 33 | imported by Python's standard import mechanism. However, to make it easy to |
|
34 | 34 | write extensions, you can also put your extensions in |
|
35 | 35 | ``os.path.join(self.ipython_dir, 'extensions')``. This directory is added to |
|
36 | 36 | ``sys.path`` automatically. |
|
37 | 37 | |
|
38 | 38 | Using extensions |
|
39 | 39 | ================ |
|
40 | 40 | |
|
41 | 41 | There are two ways you can tell IPython to use your extension: |
|
42 | 42 | |
|
43 | 43 | 1. Listing it in a configuration file. |
|
44 | 44 | 2. Using the ``%load_ext`` magic function. |
|
45 | 45 | |
|
46 | 46 | To load an extension called :file:`myextension.py` add the following logic |
|
47 | 47 | to your configuration file:: |
|
48 | 48 | |
|
49 |
c. |
|
|
49 | c.InteractiveShellApp.extensions = [ | |
|
50 | 50 | 'myextension' |
|
51 | 51 | ] |
|
52 | 52 | |
|
53 | 53 | To load that same extension at runtime, use the ``%load_ext`` magic:: |
|
54 | 54 | |
|
55 | 55 | .. sourcecode:: ipython |
|
56 | 56 | |
|
57 | 57 | In [1]: %load_ext myextension |
|
58 | 58 | |
|
59 | 59 | To summarize, in conjunction with configuration files and profiles, IPython |
|
60 | 60 | extensions give you complete and flexible control over your IPython |
|
61 | 61 | setup. |
@@ -1,136 +1,147 b'' | |||
|
1 | 1 | .. _configuring_ipython: |
|
2 | 2 | |
|
3 | 3 | =========================================================== |
|
4 | 4 | Configuring the :command:`ipython` command line application |
|
5 | 5 | =========================================================== |
|
6 | 6 | |
|
7 | 7 | This section contains information about how to configure the |
|
8 | 8 | :command:`ipython` command line application. See the :ref:`configuration |
|
9 | 9 | overview <config_overview>` for a more general description of the |
|
10 | 10 | configuration system and configuration file format. |
|
11 | 11 | |
|
12 | 12 | The default configuration file for the :command:`ipython` command line application |
|
13 | 13 | is :file:`ipython_config.py`. By setting the attributes in this file, you |
|
14 | 14 | can configure the application. A sample is provided in |
|
15 | 15 | :mod:`IPython.config.default.ipython_config`. Simply copy this file to your |
|
16 | 16 | :ref:`IPython directory <ipython_dir>` to start using it. |
|
17 | 17 | |
|
18 | Most configuration attributes that this file accepts are associated with | |
|
19 |
|
|
|
18 | Most configuration attributes that this file accepts are associated with classes | |
|
19 | that are subclasses of :class:`~IPython.config.configurable.Configurable`. | |
|
20 | 20 | |
|
21 | A few configuration attributes are not associated with a particular | |
|
22 | :class:`~IPython.core.component.Component` subclass. These are application | |
|
23 | wide configuration attributes and are stored in the ``Global`` | |
|
24 | sub-configuration section. We begin with a description of these | |
|
25 | attributes. | |
|
21 | Applications themselves are Configurable as well, so we will start with some | |
|
22 | application-level config. | |
|
26 | 23 | |
|
27 |
|
|
|
28 | ==================== | |
|
24 | Application-level configuration | |
|
25 | =============================== | |
|
29 | 26 | |
|
30 | 27 | Assuming that your configuration file has the following at the top:: |
|
31 | 28 | |
|
32 | 29 | c = get_config() |
|
33 | 30 | |
|
34 |
the following attributes |
|
|
31 | the following attributes are set application-wide: | |
|
35 | 32 | |
|
36 | :attr:`c.IPythonApp.display_banner` | |
|
33 | terminal IPython-only flags: | |
|
34 | ||
|
35 | :attr:`c.TerminalIPythonApp.display_banner` | |
|
37 | 36 | A boolean that determined if the banner is printer when :command:`ipython` |
|
38 | 37 | is started. |
|
39 | 38 | |
|
40 | :attr:`c.IPythonApp.classic` | |
|
39 | :attr:`c.TerminalIPythonApp.classic` | |
|
41 | 40 | A boolean that determines if IPython starts in "classic" mode. In this |
|
42 | 41 | mode, the prompts and everything mimic that of the normal :command:`python` |
|
43 | 42 | shell |
|
44 | 43 | |
|
45 | :attr:`c.IPythonApp.nosep` | |
|
44 | :attr:`c.TerminalIPythonApp.nosep` | |
|
46 | 45 | A boolean that determines if there should be no blank lines between |
|
47 | 46 | prompts. |
|
48 | 47 | |
|
49 |
:attr:`c. |
|
|
48 | :attr:`c.Application.log_level` | |
|
50 | 49 | An integer that sets the detail of the logging level during the startup |
|
51 | 50 | of :command:`ipython`. The default is 30 and the possible values are |
|
52 | 51 | (0, 10, 20, 30, 40, 50). Higher is quieter and lower is more verbose. |
|
52 | This can also be set by the name of the logging level, e.g. INFO=20, | |
|
53 | WARN=30. | |
|
54 | ||
|
55 | Some options, such as extensions and startup code, can be set for any | |
|
56 | application that starts an | |
|
57 | :class:`~IPython.core.interactiveshell.InteractiveShell`. These apps are | |
|
58 | subclasses of :class:`~IPython.core.shellapp.InteractiveShellApp`. Since | |
|
59 | subclasses inherit configuration, setting a trait of | |
|
60 | :attr:`c.InteractiveShellApp` will affect all IPython applications, but if you | |
|
61 | want terminal IPython and the QtConsole to have different values, you can set | |
|
62 | them via :attr:`c.TerminalIPythonApp` and :attr:`c.IPKernelApp` respectively. | |
|
63 | ||
|
53 | 64 | |
|
54 |
:attr:`c.I |
|
|
65 | :attr:`c.InteractiveShellApp.extensions` | |
|
55 | 66 | A list of strings, each of which is an importable IPython extension. An |
|
56 | 67 | IPython extension is a regular Python module or package that has a |
|
57 | 68 | :func:`load_ipython_extension(ip)` method. This method gets called when |
|
58 | 69 | the extension is loaded with the currently running |
|
59 |
:class:`~IPython.core.i |
|
|
60 |
can put your extensions anywhere they can be imported but we |
|
|
61 |
:file:`extensions` subdirectory of the ipython directory to |
|
|
62 |
during extension loading, so you can put them there as well. |
|
|
63 |
are not executed in the user's interactive namespace and they |
|
|
64 | Python code. Extensions are the recommended way of customizing | |
|
70 | :class:`~IPython.core.interactiveshell.InteractiveShell` as its only | |
|
71 | argument. You can put your extensions anywhere they can be imported but we | |
|
72 | add the :file:`extensions` subdirectory of the ipython directory to | |
|
73 | ``sys.path`` during extension loading, so you can put them there as well. | |
|
74 | Extensions are not executed in the user's interactive namespace and they | |
|
75 | must be pure Python code. Extensions are the recommended way of customizing | |
|
65 | 76 | :command:`ipython`. Extensions can provide an |
|
66 | 77 | :func:`unload_ipython_extension` that will be called when the extension is |
|
67 | 78 | unloaded. |
|
68 | 79 | |
|
69 |
:attr:`c.I |
|
|
80 | :attr:`c.InteractiveShellApp.exec_lines` | |
|
70 | 81 | A list of strings, each of which is Python code that is run in the user's |
|
71 | 82 | namespace after IPython start. These lines can contain full IPython syntax |
|
72 | 83 | with magics, etc. |
|
73 | 84 | |
|
74 |
:attr:`c.I |
|
|
85 | :attr:`c.InteractiveShellApp.exec_files` | |
|
75 | 86 | A list of strings, each of which is the full pathname of a ``.py`` or |
|
76 | 87 | ``.ipy`` file that will be executed as IPython starts. These files are run |
|
77 | 88 | in IPython in the user's namespace. Files with a ``.py`` extension need to |
|
78 | 89 | be pure Python. Files with a ``.ipy`` extension can have custom IPython |
|
79 | 90 | syntax (magics, etc.). These files need to be in the cwd, the ipythondir |
|
80 | 91 | or be absolute paths. |
|
81 | 92 | |
|
82 | 93 | Classes that can be configured |
|
83 | 94 | ============================== |
|
84 | 95 | |
|
85 | 96 | The following classes can also be configured in the configuration file for |
|
86 | 97 | :command:`ipython`: |
|
87 | 98 | |
|
88 |
* :class:`~IPython.core.i |
|
|
99 | * :class:`~IPython.core.interactiveshell.InteractiveShell` | |
|
89 | 100 | |
|
90 | 101 | * :class:`~IPython.core.prefilter.PrefilterManager` |
|
91 | 102 | |
|
92 | 103 | * :class:`~IPython.core.alias.AliasManager` |
|
93 | 104 | |
|
94 | 105 | To see which attributes of these classes are configurable, please see the |
|
95 | 106 | source code for these classes, the class docstrings or the sample |
|
96 | 107 | configuration file :mod:`IPython.config.default.ipython_config`. |
|
97 | 108 | |
|
98 | 109 | Example |
|
99 | 110 | ======= |
|
100 | 111 | |
|
101 | 112 | For those who want to get a quick start, here is a sample |
|
102 | 113 | :file:`ipython_config.py` that sets some of the common configuration |
|
103 | 114 | attributes:: |
|
104 | 115 | |
|
105 | 116 | # sample ipython_config.py |
|
106 | 117 | c = get_config() |
|
107 | 118 | |
|
108 | c.IPythonApp.display_banner = True | |
|
109 |
c.I |
|
|
110 |
c.I |
|
|
119 | c.IPythonTerminalApp.display_banner = True | |
|
120 | c.InteractiveShellApp.log_level = 20 | |
|
121 | c.InteractiveShellApp.extensions = [ | |
|
111 | 122 | 'myextension' |
|
112 | 123 | ] |
|
113 |
c.I |
|
|
124 | c.InteractiveShellApp.exec_lines = [ | |
|
114 | 125 | 'import numpy', |
|
115 | 126 | 'import scipy' |
|
116 | 127 | ] |
|
117 |
c.I |
|
|
128 | c.InteractiveShellApp.exec_files = [ | |
|
118 | 129 | 'mycode.py', |
|
119 | 130 | 'fancy.ipy' |
|
120 | 131 | ] |
|
121 | 132 | c.InteractiveShell.autoindent = True |
|
122 | 133 | c.InteractiveShell.colors = 'LightBG' |
|
123 | 134 | c.InteractiveShell.confirm_exit = False |
|
124 | 135 | c.InteractiveShell.deep_reload = True |
|
125 | 136 | c.InteractiveShell.editor = 'nano' |
|
126 | 137 | c.InteractiveShell.prompt_in1 = 'In [\#]: ' |
|
127 | 138 | c.InteractiveShell.prompt_in2 = ' .\D.: ' |
|
128 | 139 | c.InteractiveShell.prompt_out = 'Out[\#]: ' |
|
129 | 140 | c.InteractiveShell.prompts_pad_left = True |
|
130 | 141 | c.InteractiveShell.xmode = 'Context' |
|
131 | 142 | |
|
132 | 143 | c.PrefilterManager.multi_line_specials = True |
|
133 | 144 | |
|
134 | 145 | c.AliasManager.user_aliases = [ |
|
135 | 146 | ('la', 'ls -al') |
|
136 | 147 | ] |
@@ -1,342 +1,479 b'' | |||
|
1 | 1 | .. _config_overview: |
|
2 | 2 | |
|
3 | 3 | ============================================ |
|
4 | 4 | Overview of the IPython configuration system |
|
5 | 5 | ============================================ |
|
6 | 6 | |
|
7 | 7 | This section describes the IPython configuration system. Starting with version |
|
8 | 8 | 0.11, IPython has a completely new configuration system that is quite |
|
9 | 9 | different from the older :file:`ipythonrc` or :file:`ipy_user_conf.py` |
|
10 | 10 | approaches. The new configuration system was designed from scratch to address |
|
11 | 11 | the particular configuration needs of IPython. While there are many |
|
12 | 12 | other excellent configuration systems out there, we found that none of them |
|
13 | 13 | met our requirements. |
|
14 | 14 | |
|
15 | 15 | .. warning:: |
|
16 | 16 | |
|
17 | 17 | If you are upgrading to version 0.11 of IPython, you will need to migrate |
|
18 | 18 | your old :file:`ipythonrc` or :file:`ipy_user_conf.py` configuration files |
|
19 | 19 | to the new system. Read on for information on how to do this. |
|
20 | 20 | |
|
21 |
The discussion that follows is focused on teaching user |
|
|
22 |
IPython to their liking. Developer |
|
|
21 | The discussion that follows is focused on teaching users how to configure | |
|
22 | IPython to their liking. Developers who want to know more about how they | |
|
23 | 23 | can enable their objects to take advantage of the configuration system |
|
24 | 24 | should consult our :ref:`developer guide <developer_guide>` |
|
25 | 25 | |
|
26 | 26 | The main concepts |
|
27 | 27 | ================= |
|
28 | 28 | |
|
29 | 29 | There are a number of abstractions that the IPython configuration system uses. |
|
30 | 30 | Each of these abstractions is represented by a Python class. |
|
31 | 31 | |
|
32 | 32 | Configuration object: :class:`~IPython.config.loader.Config` |
|
33 | 33 | A configuration object is a simple dictionary-like class that holds |
|
34 | 34 | configuration attributes and sub-configuration objects. These classes |
|
35 | 35 | support dotted attribute style access (``Foo.bar``) in addition to the |
|
36 | 36 | regular dictionary style access (``Foo['bar']``). Configuration objects |
|
37 | 37 | are smart. They know how to merge themselves with other configuration |
|
38 | 38 | objects and they automatically create sub-configuration objects. |
|
39 | 39 | |
|
40 |
Application: :class:`~IPython.co |
|
|
40 | Application: :class:`~IPython.config.application.Application` | |
|
41 | 41 | An application is a process that does a specific job. The most obvious |
|
42 | 42 | application is the :command:`ipython` command line program. Each |
|
43 |
application reads |
|
|
43 | application reads *one or more* configuration files and a single set of | |
|
44 | command line options | |
|
44 | 45 | and then produces a master configuration object for the application. This |
|
45 | 46 | configuration object is then passed to the configurable objects that the |
|
46 | 47 | application creates. These configurable objects implement the actual logic |
|
47 | 48 | of the application and know how to configure themselves given the |
|
48 | 49 | configuration object. |
|
49 | 50 | |
|
51 | Applications always have a `log` attribute that is a configured Logger. | |
|
52 | This allows centralized logging configuration per-application. | |
|
53 | ||
|
50 | 54 | Component: :class:`~IPython.config.configurable.Configurable` |
|
51 | 55 | A configurable is a regular Python class that serves as a base class for |
|
52 | 56 | all main classes in an application. The |
|
53 | 57 | :class:`~IPython.config.configurable.Configurable` base class is |
|
54 | 58 | lightweight and only does one things. |
|
55 | 59 | |
|
56 | 60 | This :class:`~IPython.config.configurable.Configurable` is a subclass |
|
57 | 61 | of :class:`~IPython.utils.traitlets.HasTraits` that knows how to configure |
|
58 | 62 | itself. Class level traits with the metadata ``config=True`` become |
|
59 | 63 | values that can be configured from the command line and configuration |
|
60 | 64 | files. |
|
61 | 65 | |
|
62 | 66 | Developers create :class:`~IPython.config.configurable.Configurable` |
|
63 | 67 | subclasses that implement all of the logic in the application. Each of |
|
64 | 68 | these subclasses has its own configuration information that controls how |
|
65 | 69 | instances are created. |
|
66 | 70 | |
|
71 | Singletons: :class:`~IPython.config.configurable.SingletonConfigurable` | |
|
72 | Any object for which there is a single canonical instance. These are | |
|
73 | just like Configurables, except they have a class method | |
|
74 | :meth:`~IPython.config.configurable.SingletonConfigurable.instance`, | |
|
75 | that returns the current active instance (or creates one if it | |
|
76 | does not exist). Examples of singletons include | |
|
77 | :class:`~IPython.config.application.Application`s and | |
|
78 | :class:`~IPython.core.interactiveshell.InteractiveShell`. This lets | |
|
79 | objects easily connect to the current running Application without passing | |
|
80 | objects around everywhere. For instance, to get the current running | |
|
81 | Application instance, simply do: ``app = Application.instance()``. | |
|
82 | ||
|
83 | ||
|
84 | .. note:: | |
|
85 | ||
|
86 | Singletons are not strictly enforced - you can have many instances | |
|
87 | of a given singleton class, but the :meth:`instance` method will always | |
|
88 | return the same one. | |
|
89 | ||
|
67 | 90 | Having described these main concepts, we can now state the main idea in our |
|
68 | 91 | configuration system: *"configuration" allows the default values of class |
|
69 | 92 | attributes to be controlled on a class by class basis*. Thus all instances of |
|
70 | 93 | a given class are configured in the same way. Furthermore, if two instances |
|
71 | 94 | need to be configured differently, they need to be instances of two different |
|
72 | 95 | classes. While this model may seem a bit restrictive, we have found that it |
|
73 | 96 | expresses most things that need to be configured extremely well. However, it |
|
74 | 97 | is possible to create two instances of the same class that have different |
|
75 | 98 | trait values. This is done by overriding the configuration. |
|
76 | 99 | |
|
77 | 100 | Now, we show what our configuration objects and files look like. |
|
78 | 101 | |
|
79 | 102 | Configuration objects and files |
|
80 | 103 | =============================== |
|
81 | 104 | |
|
82 | 105 | A configuration file is simply a pure Python file that sets the attributes |
|
83 | 106 | of a global, pre-created configuration object. This configuration object is a |
|
84 | 107 | :class:`~IPython.config.loader.Config` instance. While in a configuration |
|
85 | 108 | file, to get a reference to this object, simply call the :func:`get_config` |
|
86 | 109 | function. We inject this function into the global namespace that the |
|
87 | 110 | configuration file is executed in. |
|
88 | 111 | |
|
89 | 112 | Here is an example of a super simple configuration file that does nothing:: |
|
90 | 113 | |
|
91 | 114 | c = get_config() |
|
92 | 115 | |
|
93 | 116 | Once you get a reference to the configuration object, you simply set |
|
94 | 117 | attributes on it. All you have to know is: |
|
95 | 118 | |
|
96 | 119 | * The name of each attribute. |
|
97 | 120 | * The type of each attribute. |
|
98 | 121 | |
|
99 | 122 | The answers to these two questions are provided by the various |
|
100 | 123 | :class:`~IPython.config.configurable.Configurable` subclasses that an |
|
101 | 124 | application uses. Let's look at how this would work for a simple component |
|
102 | 125 | subclass:: |
|
103 | 126 | |
|
104 | 127 | # Sample component that can be configured. |
|
105 | 128 | from IPython.config.configurable import Configurable |
|
106 | 129 | from IPython.utils.traitlets import Int, Float, Str, Bool |
|
107 | 130 | |
|
108 | 131 | class MyClass(Configurable): |
|
109 |
name = |
|
|
132 | name = Unicode(u'defaultname', config=True) | |
|
110 | 133 | ranking = Int(0, config=True) |
|
111 | 134 | value = Float(99.0) |
|
112 | 135 | # The rest of the class implementation would go here.. |
|
113 | 136 | |
|
114 | 137 | In this example, we see that :class:`MyClass` has three attributes, two |
|
115 | 138 | of whom (``name``, ``ranking``) can be configured. All of the attributes |
|
116 | 139 | are given types and default values. If a :class:`MyClass` is instantiated, |
|
117 | 140 | but not configured, these default values will be used. But let's see how |
|
118 | 141 | to configure this class in a configuration file:: |
|
119 | 142 | |
|
120 | 143 | # Sample config file |
|
121 | 144 | c = get_config() |
|
122 | 145 | |
|
123 | 146 | c.MyClass.name = 'coolname' |
|
124 | 147 | c.MyClass.ranking = 10 |
|
125 | 148 | |
|
126 | 149 | After this configuration file is loaded, the values set in it will override |
|
127 | 150 | the class defaults anytime a :class:`MyClass` is created. Furthermore, |
|
128 | 151 | these attributes will be type checked and validated anytime they are set. |
|
129 | 152 | This type checking is handled by the :mod:`IPython.utils.traitlets` module, |
|
130 | 153 | which provides the :class:`Str`, :class:`Int` and :class:`Float` types. In |
|
131 | 154 | addition to these traitlets, the :mod:`IPython.utils.traitlets` provides |
|
132 | 155 | traitlets for a number of other types. |
|
133 | 156 | |
|
134 | 157 | .. note:: |
|
135 | 158 | |
|
136 | 159 | Underneath the hood, the :class:`Configurable` base class is a subclass of |
|
137 | 160 | :class:`IPython.utils.traitlets.HasTraits`. The |
|
138 | 161 | :mod:`IPython.utils.traitlets` module is a lightweight version of |
|
139 | 162 | :mod:`enthought.traits`. Our implementation is a pure Python subset |
|
140 | 163 | (mostly API compatible) of :mod:`enthought.traits` that does not have any |
|
141 | 164 | of the automatic GUI generation capabilities. Our plan is to achieve 100% |
|
142 | 165 | API compatibility to enable the actual :mod:`enthought.traits` to |
|
143 | 166 | eventually be used instead. Currently, we cannot use |
|
144 | 167 | :mod:`enthought.traits` as we are committed to the core of IPython being |
|
145 | 168 | pure Python. |
|
146 | 169 | |
|
147 | 170 | It should be very clear at this point what the naming convention is for |
|
148 | 171 | configuration attributes:: |
|
149 | 172 | |
|
150 | 173 | c.ClassName.attribute_name = attribute_value |
|
151 | 174 | |
|
152 | 175 | Here, ``ClassName`` is the name of the class whose configuration attribute you |
|
153 | 176 | want to set, ``attribute_name`` is the name of the attribute you want to set |
|
154 | 177 | and ``attribute_value`` the the value you want it to have. The ``ClassName`` |
|
155 | 178 | attribute of ``c`` is not the actual class, but instead is another |
|
156 | 179 | :class:`~IPython.config.loader.Config` instance. |
|
157 | 180 | |
|
158 | 181 | .. note:: |
|
159 | 182 | |
|
160 | 183 | The careful reader may wonder how the ``ClassName`` (``MyClass`` in |
|
161 | 184 | the above example) attribute of the configuration object ``c`` gets |
|
162 | 185 | created. These attributes are created on the fly by the |
|
163 | 186 | :class:`~IPython.config.loader.Config` instance, using a simple naming |
|
164 | 187 | convention. Any attribute of a :class:`~IPython.config.loader.Config` |
|
165 | 188 | instance whose name begins with an uppercase character is assumed to be a |
|
166 | 189 | sub-configuration and a new empty :class:`~IPython.config.loader.Config` |
|
167 | 190 | instance is dynamically created for that attribute. This allows deeply |
|
168 | 191 | hierarchical information created easily (``c.Foo.Bar.value``) on the fly. |
|
169 | 192 | |
|
170 | 193 | Configuration files inheritance |
|
171 | 194 | =============================== |
|
172 | 195 | |
|
173 | 196 | Let's say you want to have different configuration files for various purposes. |
|
174 | 197 | Our configuration system makes it easy for one configuration file to inherit |
|
175 | 198 | the information in another configuration file. The :func:`load_subconfig` |
|
176 | 199 | command can be used in a configuration file for this purpose. Here is a simple |
|
177 | 200 | example that loads all of the values from the file :file:`base_config.py`:: |
|
178 | 201 | |
|
179 | 202 | # base_config.py |
|
180 | 203 | c = get_config() |
|
181 | 204 | c.MyClass.name = 'coolname' |
|
182 | 205 | c.MyClass.ranking = 100 |
|
183 | 206 | |
|
184 | 207 | into the configuration file :file:`main_config.py`:: |
|
185 | 208 | |
|
186 | 209 | # main_config.py |
|
187 | 210 | c = get_config() |
|
188 | 211 | |
|
189 | 212 | # Load everything from base_config.py |
|
190 | 213 | load_subconfig('base_config.py') |
|
191 | 214 | |
|
192 | 215 | # Now override one of the values |
|
193 | 216 | c.MyClass.name = 'bettername' |
|
194 | 217 | |
|
195 | 218 | In a situation like this the :func:`load_subconfig` makes sure that the |
|
196 | 219 | search path for sub-configuration files is inherited from that of the parent. |
|
197 | 220 | Thus, you can typically put the two in the same directory and everything will |
|
198 | 221 | just work. |
|
199 | 222 | |
|
223 | You can also load configuration files by profile, for instance: | |
|
224 | ||
|
225 | .. sourcecode:: python | |
|
226 | ||
|
227 | load_subconfig('ipython_config.py', profile='default') | |
|
228 | ||
|
229 | to inherit your default configuration as a starting point. | |
|
230 | ||
|
231 | ||
|
200 | 232 | Class based configuration inheritance |
|
201 | 233 | ===================================== |
|
202 | 234 | |
|
203 | 235 | There is another aspect of configuration where inheritance comes into play. |
|
204 | 236 | Sometimes, your classes will have an inheritance hierarchy that you want |
|
205 | 237 | to be reflected in the configuration system. Here is a simple example:: |
|
206 | 238 | |
|
207 | 239 | from IPython.config.configurable import Configurable |
|
208 | 240 | from IPython.utils.traitlets import Int, Float, Str, Bool |
|
209 | 241 | |
|
210 | 242 | class Foo(Configurable): |
|
211 | 243 | name = Str('fooname', config=True) |
|
212 | 244 | value = Float(100.0, config=True) |
|
213 | 245 | |
|
214 | 246 | class Bar(Foo): |
|
215 | 247 | name = Str('barname', config=True) |
|
216 | 248 | othervalue = Int(0, config=True) |
|
217 | 249 | |
|
218 | 250 | Now, we can create a configuration file to configure instances of :class:`Foo` |
|
219 | 251 | and :class:`Bar`:: |
|
220 | 252 | |
|
221 | 253 | # config file |
|
222 | 254 | c = get_config() |
|
223 | 255 | |
|
224 | 256 | c.Foo.name = 'bestname' |
|
225 | 257 | c.Bar.othervalue = 10 |
|
226 | 258 | |
|
227 | 259 | This class hierarchy and configuration file accomplishes the following: |
|
228 | 260 | |
|
229 | 261 | * The default value for :attr:`Foo.name` and :attr:`Bar.name` will be |
|
230 | 262 | 'bestname'. Because :class:`Bar` is a :class:`Foo` subclass it also |
|
231 | 263 | picks up the configuration information for :class:`Foo`. |
|
232 | 264 | * The default value for :attr:`Foo.value` and :attr:`Bar.value` will be |
|
233 | 265 | ``100.0``, which is the value specified as the class default. |
|
234 | 266 | * The default value for :attr:`Bar.othervalue` will be 10 as set in the |
|
235 | 267 | configuration file. Because :class:`Foo` is the parent of :class:`Bar` |
|
236 | 268 | it doesn't know anything about the :attr:`othervalue` attribute. |
|
237 | 269 | |
|
238 | 270 | |
|
239 | 271 | .. _ipython_dir: |
|
240 | 272 | |
|
241 | 273 | Configuration file location |
|
242 | 274 | =========================== |
|
243 | 275 | |
|
244 |
So where should you put your configuration files? |
|
|
245 | applications look in the so called "IPython directory". The location of | |
|
246 | this directory is determined by the following algorithm: | |
|
276 | So where should you put your configuration files? IPython uses "profiles" for | |
|
277 | configuration, and by default, all profiles will be stored in the so called | |
|
278 | "IPython directory". The location of this directory is determined by the | |
|
279 | following algorithm: | |
|
247 | 280 | |
|
248 | 281 | * If the ``ipython_dir`` command line flag is given, its value is used. |
|
249 | 282 | |
|
250 | 283 | * If not, the value returned by :func:`IPython.utils.path.get_ipython_dir` |
|
251 | 284 | is used. This function will first look at the :envvar:`IPYTHON_DIR` |
|
252 | 285 | environment variable and then default to a platform-specific default. |
|
253 | 286 | |
|
254 | 287 | On posix systems (Linux, Unix, etc.), IPython respects the ``$XDG_CONFIG_HOME`` |
|
255 | 288 | part of the `XDG Base Directory`_ specification. If ``$XDG_CONFIG_HOME`` is |
|
256 | 289 | defined and exists ( ``XDG_CONFIG_HOME`` has a default interpretation of |
|
257 | 290 | :file:`$HOME/.config`), then IPython's config directory will be located in |
|
258 | 291 | :file:`$XDG_CONFIG_HOME/ipython`. If users still have an IPython directory |
|
259 | 292 | in :file:`$HOME/.ipython`, then that will be used. in preference to the |
|
260 | 293 | system default. |
|
261 | 294 | |
|
262 | 295 | For most users, the default value will simply be something like |
|
263 | 296 | :file:`$HOME/.config/ipython` on Linux, or :file:`$HOME/.ipython` |
|
264 | 297 | elsewhere. |
|
265 | 298 | |
|
266 | Once the location of the IPython directory has been determined, you need to | |
|
267 | know what filename to use for the configuration file. The basic idea is that | |
|
268 | each application has its own default configuration filename. The default named | |
|
269 | used by the :command:`ipython` command line program is | |
|
270 | :file:`ipython_config.py`. This value can be overriden by the ``config_file`` | |
|
271 | command line flag. A sample :file:`ipython_config.py` file can be found | |
|
272 | in :mod:`IPython.config.default.ipython_config.py`. Simple copy it to your | |
|
273 | IPython directory to begin using it. | |
|
299 | Once the location of the IPython directory has been determined, you need to know | |
|
300 | which profile you are using. For users with a single configuration, this will | |
|
301 | simply be 'default', and will be located in | |
|
302 | :file:`<IPYTHON_DIR>/profile_default`. | |
|
303 | ||
|
304 | The next thing you need to know is what to call your configuration file. The | |
|
305 | basic idea is that each application has its own default configuration filename. | |
|
306 | The default named used by the :command:`ipython` command line program is | |
|
307 | :file:`ipython_config.py`, and *all* IPython applications will use this file. | |
|
308 | Other applications, such as the parallel :command:`ipcluster` scripts or the | |
|
309 | QtConsole will load their own config files *after* :file:`ipython_config.py`. To | |
|
310 | load a particular configuration file instead of the default, the name can be | |
|
311 | overridden by the ``config_file`` command line flag. | |
|
312 | ||
|
313 | To generate the default configuration files, do:: | |
|
314 | ||
|
315 | $> ipython profile create | |
|
316 | ||
|
317 | and you will have a default :file:`ipython_config.py` in your IPython directory | |
|
318 | under :file:`profile_default`. If you want the default config files for the | |
|
319 | :mod:`IPython.parallel` applications, add ``--parallel`` to the end of the | |
|
320 | command-line args. | |
|
274 | 321 | |
|
275 | 322 | .. _Profiles: |
|
276 | 323 | |
|
277 | 324 | Profiles |
|
278 | 325 | ======== |
|
279 | 326 | |
|
280 | A profile is simply a configuration file that follows a simple naming | |
|
281 | convention and can be loaded using a simplified syntax. The idea is | |
|
282 | that users often want to maintain a set of configuration files for different | |
|
283 | purposes: one for doing numerical computing with NumPy and SciPy and | |
|
284 | another for doing symbolic computing with SymPy. Profiles make it easy | |
|
285 | to keep a separate configuration file for each of these purposes. | |
|
327 | A profile is a directory containing configuration and runtime files, such as | |
|
328 | logs, connection info for the parallel apps, and your IPython command history. | |
|
329 | ||
|
330 | The idea is that users often want to maintain a set of configuration files for | |
|
331 | different purposes: one for doing numerical computing with NumPy and SciPy and | |
|
332 | another for doing symbolic computing with SymPy. Profiles make it easy to keep a | |
|
333 | separate configuration files, logs, and histories for each of these purposes. | |
|
286 | 334 | |
|
287 | 335 | Let's start by showing how a profile is used: |
|
288 | 336 | |
|
289 | 337 | .. code-block:: bash |
|
290 | 338 | |
|
291 | 339 | $ ipython profile=sympy |
|
292 | 340 | |
|
293 | This tells the :command:`ipython` command line program to get its | |
|
294 |
|
|
|
295 | same as that of regular configuration files. The only difference is that | |
|
296 | profiles are named in a special way. In the case above, the "sympy" profile | |
|
297 | would need to have the name :file:`ipython_config_sympy.py`. | |
|
341 | This tells the :command:`ipython` command line program to get its configuration | |
|
342 | from the "sympy" profile. The file names for various profiles do not change. The | |
|
343 | only difference is that profiles are named in a special way. In the case above, | |
|
344 | the "sympy" profile means looking for :file:`ipython_config.py` in :file:`<IPYTHON_DIR>/profile_sympy`. | |
|
345 | ||
|
346 | The general pattern is this: simply create a new profile with: | |
|
347 | ||
|
348 | .. code-block:: bash | |
|
349 | ||
|
350 | ipython profile create <name> | |
|
351 | ||
|
352 | which adds a directory called ``profile_<name>`` to your IPython directory. Then | |
|
353 | you can load this profile by adding ``profile=<name>`` to your command line | |
|
354 | options. Profiles are supported by all IPython applications. | |
|
355 | ||
|
356 | IPython ships with some sample profiles in :file:`IPython/config/profile`. If | |
|
357 | you create profiles with the name of one of our shipped profiles, these config | |
|
358 | files will be copied over instead of starting with the automatically generated | |
|
359 | config files. | |
|
360 | ||
|
361 | .. _commandline: | |
|
362 | ||
|
363 | Command-line arguments | |
|
364 | ====================== | |
|
365 | ||
|
366 | IPython exposes *all* configurable options on the command-line. The command-line | |
|
367 | arguments are generated from the Configurable traits of the classes associated | |
|
368 | with a given Application. Configuring IPython from the command-line may look | |
|
369 | very similar to an IPython config file | |
|
370 | ||
|
371 | IPython applications use a parser called | |
|
372 | :class:`~IPython.config.loader.KeyValueLoader` to load values into a Config | |
|
373 | object. Values are assigned in much the same way as in a config file: | |
|
374 | ||
|
375 | .. code-block:: bash | |
|
376 | ||
|
377 | $> ipython InteractiveShell.use_readline=False BaseIPythonApplication.profile='myprofile' | |
|
378 | ||
|
379 | Is the same as adding: | |
|
380 | ||
|
381 | .. sourcecode:: python | |
|
382 | ||
|
383 | c.InteractiveShell.use_readline=False | |
|
384 | c.BaseIPythonApplication.profile='myprofile' | |
|
385 | ||
|
386 | to your config file. Key/Value arguments *always* take a value, separated by '=' | |
|
387 | and no spaces. | |
|
388 | ||
|
389 | Aliases | |
|
390 | ------- | |
|
391 | ||
|
392 | For convenience, applications have a mapping of commonly | |
|
393 | used traits, so you don't have to specify the whole class name. For these **aliases**, the class need not be specified: | |
|
394 | ||
|
395 | .. code-block:: bash | |
|
396 | ||
|
397 | $> ipython profile='myprofile' | |
|
398 | # is equivalent to | |
|
399 | $> ipython BaseIPythonApplication.profile='myprofile' | |
|
400 | ||
|
401 | Flags | |
|
402 | ----- | |
|
403 | ||
|
404 | Applications can also be passed **flags**. Flags are options that take no | |
|
405 | arguments, and are always prefixed with ``--``. They are simply wrappers for | |
|
406 | setting one or more configurables with predefined values, often True/False. | |
|
407 | ||
|
408 | For instance: | |
|
409 | ||
|
410 | .. code-block:: bash | |
|
411 | ||
|
412 | $> ipcontroller --debug | |
|
413 | # is equivalent to | |
|
414 | $> ipcontroller Application.log_level=DEBUG | |
|
415 | # and | |
|
416 | $> ipython --pylab | |
|
417 | # is equivalent to | |
|
418 | $> ipython pylab=auto | |
|
419 | ||
|
420 | Subcommands | |
|
421 | ----------- | |
|
422 | ||
|
423 | ||
|
424 | Some IPython applications have **subcommands**. Subcommands are modeled after | |
|
425 | :command:`git`, and are called with the form :command:`command subcommand | |
|
426 | [...args]`. Currently, the QtConsole is a subcommand of terminal IPython: | |
|
427 | ||
|
428 | .. code-block:: bash | |
|
429 | ||
|
430 | $> ipython qtconsole profile=myprofile | |
|
431 | ||
|
432 | and :command:`ipcluster` is simply a wrapper for its various subcommands (start, | |
|
433 | stop, engines). | |
|
434 | ||
|
435 | .. code-block:: bash | |
|
436 | ||
|
437 | $> ipcluster start profile=myprofile n=4 | |
|
438 | ||
|
298 | 439 | |
|
299 | The general pattern is this: simply add ``<profilename>`` to the end of the | |
|
300 | normal configuration file name. Then load the profile by adding | |
|
301 | ``profile=<profilename>`` to your command line options. | |
|
440 | To see a list of the available aliases, flags, and subcommands for an IPython application, simply pass ``-h`` or ``--help``. And to see the full list of configurable options (*very* long), pass ``--help-all``. | |
|
302 | 441 | |
|
303 | IPython ships with some sample profiles in :mod:`IPython.config.profile`. | |
|
304 | Simply copy these to your IPython directory to begin using them. | |
|
305 | 442 | |
|
306 | 443 | Design requirements |
|
307 | 444 | =================== |
|
308 | 445 | |
|
309 | 446 | Here are the main requirements we wanted our configuration system to have: |
|
310 | 447 | |
|
311 | 448 | * Support for hierarchical configuration information. |
|
312 | 449 | |
|
313 | 450 | * Full integration with command line option parsers. Often, you want to read |
|
314 | 451 | a configuration file, but then override some of the values with command line |
|
315 | 452 | options. Our configuration system automates this process and allows each |
|
316 | 453 | command line option to be linked to a particular attribute in the |
|
317 | 454 | configuration hierarchy that it will override. |
|
318 | 455 | |
|
319 | 456 | * Configuration files that are themselves valid Python code. This accomplishes |
|
320 | 457 | many things. First, it becomes possible to put logic in your configuration |
|
321 | 458 | files that sets attributes based on your operating system, network setup, |
|
322 | 459 | Python version, etc. Second, Python has a super simple syntax for accessing |
|
323 | 460 | hierarchical data structures, namely regular attribute access |
|
324 | 461 | (``Foo.Bar.Bam.name``). Third, using Python makes it easy for users to |
|
325 | 462 | import configuration attributes from one configuration file to another. |
|
326 | 463 | Forth, even though Python is dynamically typed, it does have types that can |
|
327 | 464 | be checked at runtime. Thus, a ``1`` in a config file is the integer '1', |
|
328 | 465 | while a ``'1'`` is a string. |
|
329 | 466 | |
|
330 | 467 | * A fully automated method for getting the configuration information to the |
|
331 | 468 | classes that need it at runtime. Writing code that walks a configuration |
|
332 | 469 | hierarchy to extract a particular attribute is painful. When you have |
|
333 | 470 | complex configuration information with hundreds of attributes, this makes |
|
334 | 471 | you want to cry. |
|
335 | 472 | |
|
336 | 473 | * Type checking and validation that doesn't require the entire configuration |
|
337 | 474 | hierarchy to be specified statically before runtime. Python is a very |
|
338 | 475 | dynamic language and you don't always know everything that needs to be |
|
339 | 476 | configured when a program starts. |
|
340 | 477 | |
|
341 | 478 | |
|
342 | 479 | .. _`XDG Base Directory`: http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html |
@@ -1,26 +1,25 b'' | |||
|
1 | 1 | .. _developer_guide: |
|
2 | 2 | |
|
3 | 3 | ========================= |
|
4 | 4 | IPython developer's guide |
|
5 | 5 | ========================= |
|
6 | 6 | |
|
7 | 7 | .. toctree:: |
|
8 | 8 | :maxdepth: 1 |
|
9 | 9 | |
|
10 | 10 | contributing.txt |
|
11 | 11 | gitwash/index.txt |
|
12 | 12 | coding_guide.txt |
|
13 | 13 | doc_guide.txt |
|
14 | 14 | testing.txt |
|
15 | 15 | release.txt |
|
16 | 16 | roadmap.txt |
|
17 | 17 | reorg.txt |
|
18 | 18 | messaging.txt |
|
19 | 19 | parallel_messages.txt |
|
20 | 20 | parallel_connections.txt |
|
21 | 21 | magic_blueprint.txt |
|
22 | 22 | notification_blueprint.txt |
|
23 | 23 | ipgraph.txt |
|
24 | 24 | ipython_qt.txt |
|
25 | 25 | ipythonzmq.txt |
|
26 | parallelzmq.txt |
General Comments 0
You need to be logged in to leave comments.
Login now