##// END OF EJS Templates
Update config docs so they don't refer to Str traitlet type.
Thomas Kluyver -
Show More
@@ -1,479 +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 21 The discussion that follows is focused on teaching users how to configure
22 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 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 43 application reads *one or more* configuration files and a single set of
44 44 command line options
45 45 and then produces a master configuration object for the application. This
46 46 configuration object is then passed to the configurable objects that the
47 47 application creates. These configurable objects implement the actual logic
48 48 of the application and know how to configure themselves given the
49 49 configuration object.
50 50
51 51 Applications always have a `log` attribute that is a configured Logger.
52 52 This allows centralized logging configuration per-application.
53 53
54 54 Component: :class:`~IPython.config.configurable.Configurable`
55 55 A configurable is a regular Python class that serves as a base class for
56 56 all main classes in an application. The
57 57 :class:`~IPython.config.configurable.Configurable` base class is
58 58 lightweight and only does one things.
59 59
60 60 This :class:`~IPython.config.configurable.Configurable` is a subclass
61 61 of :class:`~IPython.utils.traitlets.HasTraits` that knows how to configure
62 62 itself. Class level traits with the metadata ``config=True`` become
63 63 values that can be configured from the command line and configuration
64 64 files.
65 65
66 66 Developers create :class:`~IPython.config.configurable.Configurable`
67 67 subclasses that implement all of the logic in the application. Each of
68 68 these subclasses has its own configuration information that controls how
69 69 instances are created.
70 70
71 71 Singletons: :class:`~IPython.config.configurable.SingletonConfigurable`
72 72 Any object for which there is a single canonical instance. These are
73 73 just like Configurables, except they have a class method
74 74 :meth:`~IPython.config.configurable.SingletonConfigurable.instance`,
75 75 that returns the current active instance (or creates one if it
76 76 does not exist). Examples of singletons include
77 77 :class:`~IPython.config.application.Application`s and
78 78 :class:`~IPython.core.interactiveshell.InteractiveShell`. This lets
79 79 objects easily connect to the current running Application without passing
80 80 objects around everywhere. For instance, to get the current running
81 81 Application instance, simply do: ``app = Application.instance()``.
82 82
83 83
84 84 .. note::
85 85
86 86 Singletons are not strictly enforced - you can have many instances
87 87 of a given singleton class, but the :meth:`instance` method will always
88 88 return the same one.
89 89
90 90 Having described these main concepts, we can now state the main idea in our
91 91 configuration system: *"configuration" allows the default values of class
92 92 attributes to be controlled on a class by class basis*. Thus all instances of
93 93 a given class are configured in the same way. Furthermore, if two instances
94 94 need to be configured differently, they need to be instances of two different
95 95 classes. While this model may seem a bit restrictive, we have found that it
96 96 expresses most things that need to be configured extremely well. However, it
97 97 is possible to create two instances of the same class that have different
98 98 trait values. This is done by overriding the configuration.
99 99
100 100 Now, we show what our configuration objects and files look like.
101 101
102 102 Configuration objects and files
103 103 ===============================
104 104
105 105 A configuration file is simply a pure Python file that sets the attributes
106 106 of a global, pre-created configuration object. This configuration object is a
107 107 :class:`~IPython.config.loader.Config` instance. While in a configuration
108 108 file, to get a reference to this object, simply call the :func:`get_config`
109 109 function. We inject this function into the global namespace that the
110 110 configuration file is executed in.
111 111
112 112 Here is an example of a super simple configuration file that does nothing::
113 113
114 114 c = get_config()
115 115
116 116 Once you get a reference to the configuration object, you simply set
117 117 attributes on it. All you have to know is:
118 118
119 119 * The name of each attribute.
120 120 * The type of each attribute.
121 121
122 122 The answers to these two questions are provided by the various
123 123 :class:`~IPython.config.configurable.Configurable` subclasses that an
124 124 application uses. Let's look at how this would work for a simple component
125 125 subclass::
126 126
127 127 # Sample component that can be configured.
128 128 from IPython.config.configurable import Configurable
129 from IPython.utils.traitlets import Int, Float, Str, Bool
129 from IPython.utils.traitlets import Int, Float, Unicode, Bool
130 130
131 131 class MyClass(Configurable):
132 132 name = Unicode(u'defaultname', config=True)
133 133 ranking = Int(0, config=True)
134 134 value = Float(99.0)
135 135 # The rest of the class implementation would go here..
136 136
137 137 In this example, we see that :class:`MyClass` has three attributes, two
138 138 of whom (``name``, ``ranking``) can be configured. All of the attributes
139 139 are given types and default values. If a :class:`MyClass` is instantiated,
140 140 but not configured, these default values will be used. But let's see how
141 141 to configure this class in a configuration file::
142 142
143 143 # Sample config file
144 144 c = get_config()
145 145
146 146 c.MyClass.name = 'coolname'
147 147 c.MyClass.ranking = 10
148 148
149 149 After this configuration file is loaded, the values set in it will override
150 150 the class defaults anytime a :class:`MyClass` is created. Furthermore,
151 151 these attributes will be type checked and validated anytime they are set.
152 152 This type checking is handled by the :mod:`IPython.utils.traitlets` module,
153 which provides the :class:`Str`, :class:`Int` and :class:`Float` types. In
154 addition to these traitlets, the :mod:`IPython.utils.traitlets` provides
153 which provides the :class:`Unicode`, :class:`Int` and :class:`Float` types.
154 In addition to these traitlets, the :mod:`IPython.utils.traitlets` provides
155 155 traitlets for a number of other types.
156 156
157 157 .. note::
158 158
159 159 Underneath the hood, the :class:`Configurable` base class is a subclass of
160 160 :class:`IPython.utils.traitlets.HasTraits`. The
161 161 :mod:`IPython.utils.traitlets` module is a lightweight version of
162 162 :mod:`enthought.traits`. Our implementation is a pure Python subset
163 163 (mostly API compatible) of :mod:`enthought.traits` that does not have any
164 164 of the automatic GUI generation capabilities. Our plan is to achieve 100%
165 165 API compatibility to enable the actual :mod:`enthought.traits` to
166 166 eventually be used instead. Currently, we cannot use
167 167 :mod:`enthought.traits` as we are committed to the core of IPython being
168 168 pure Python.
169 169
170 170 It should be very clear at this point what the naming convention is for
171 171 configuration attributes::
172 172
173 173 c.ClassName.attribute_name = attribute_value
174 174
175 175 Here, ``ClassName`` is the name of the class whose configuration attribute you
176 176 want to set, ``attribute_name`` is the name of the attribute you want to set
177 177 and ``attribute_value`` the the value you want it to have. The ``ClassName``
178 178 attribute of ``c`` is not the actual class, but instead is another
179 179 :class:`~IPython.config.loader.Config` instance.
180 180
181 181 .. note::
182 182
183 183 The careful reader may wonder how the ``ClassName`` (``MyClass`` in
184 184 the above example) attribute of the configuration object ``c`` gets
185 185 created. These attributes are created on the fly by the
186 186 :class:`~IPython.config.loader.Config` instance, using a simple naming
187 187 convention. Any attribute of a :class:`~IPython.config.loader.Config`
188 188 instance whose name begins with an uppercase character is assumed to be a
189 189 sub-configuration and a new empty :class:`~IPython.config.loader.Config`
190 190 instance is dynamically created for that attribute. This allows deeply
191 191 hierarchical information created easily (``c.Foo.Bar.value``) on the fly.
192 192
193 193 Configuration files inheritance
194 194 ===============================
195 195
196 196 Let's say you want to have different configuration files for various purposes.
197 197 Our configuration system makes it easy for one configuration file to inherit
198 198 the information in another configuration file. The :func:`load_subconfig`
199 199 command can be used in a configuration file for this purpose. Here is a simple
200 200 example that loads all of the values from the file :file:`base_config.py`::
201 201
202 202 # base_config.py
203 203 c = get_config()
204 204 c.MyClass.name = 'coolname'
205 205 c.MyClass.ranking = 100
206 206
207 207 into the configuration file :file:`main_config.py`::
208 208
209 209 # main_config.py
210 210 c = get_config()
211 211
212 212 # Load everything from base_config.py
213 213 load_subconfig('base_config.py')
214 214
215 215 # Now override one of the values
216 216 c.MyClass.name = 'bettername'
217 217
218 218 In a situation like this the :func:`load_subconfig` makes sure that the
219 219 search path for sub-configuration files is inherited from that of the parent.
220 220 Thus, you can typically put the two in the same directory and everything will
221 221 just work.
222 222
223 223 You can also load configuration files by profile, for instance:
224 224
225 225 .. sourcecode:: python
226 226
227 227 load_subconfig('ipython_config.py', profile='default')
228 228
229 229 to inherit your default configuration as a starting point.
230 230
231 231
232 232 Class based configuration inheritance
233 233 =====================================
234 234
235 235 There is another aspect of configuration where inheritance comes into play.
236 236 Sometimes, your classes will have an inheritance hierarchy that you want
237 237 to be reflected in the configuration system. Here is a simple example::
238 238
239 239 from IPython.config.configurable import Configurable
240 from IPython.utils.traitlets import Int, Float, Str, Bool
240 from IPython.utils.traitlets import Int, Float, Unicode, Bool
241 241
242 242 class Foo(Configurable):
243 name = Str('fooname', config=True)
243 name = Unicode(u'fooname', config=True)
244 244 value = Float(100.0, config=True)
245 245
246 246 class Bar(Foo):
247 name = Str('barname', config=True)
247 name = Unicode(u'barname', config=True)
248 248 othervalue = Int(0, config=True)
249 249
250 250 Now, we can create a configuration file to configure instances of :class:`Foo`
251 251 and :class:`Bar`::
252 252
253 253 # config file
254 254 c = get_config()
255 255
256 c.Foo.name = 'bestname'
256 c.Foo.name = u'bestname'
257 257 c.Bar.othervalue = 10
258 258
259 259 This class hierarchy and configuration file accomplishes the following:
260 260
261 261 * The default value for :attr:`Foo.name` and :attr:`Bar.name` will be
262 262 'bestname'. Because :class:`Bar` is a :class:`Foo` subclass it also
263 263 picks up the configuration information for :class:`Foo`.
264 264 * The default value for :attr:`Foo.value` and :attr:`Bar.value` will be
265 265 ``100.0``, which is the value specified as the class default.
266 266 * The default value for :attr:`Bar.othervalue` will be 10 as set in the
267 267 configuration file. Because :class:`Foo` is the parent of :class:`Bar`
268 268 it doesn't know anything about the :attr:`othervalue` attribute.
269 269
270 270
271 271 .. _ipython_dir:
272 272
273 273 Configuration file location
274 274 ===========================
275 275
276 276 So where should you put your configuration files? IPython uses "profiles" for
277 277 configuration, and by default, all profiles will be stored in the so called
278 278 "IPython directory". The location of this directory is determined by the
279 279 following algorithm:
280 280
281 281 * If the ``ipython_dir`` command line flag is given, its value is used.
282 282
283 283 * If not, the value returned by :func:`IPython.utils.path.get_ipython_dir`
284 284 is used. This function will first look at the :envvar:`IPYTHON_DIR`
285 285 environment variable and then default to a platform-specific default.
286 286
287 287 On posix systems (Linux, Unix, etc.), IPython respects the ``$XDG_CONFIG_HOME``
288 288 part of the `XDG Base Directory`_ specification. If ``$XDG_CONFIG_HOME`` is
289 289 defined and exists ( ``XDG_CONFIG_HOME`` has a default interpretation of
290 290 :file:`$HOME/.config`), then IPython's config directory will be located in
291 291 :file:`$XDG_CONFIG_HOME/ipython`. If users still have an IPython directory
292 292 in :file:`$HOME/.ipython`, then that will be used. in preference to the
293 293 system default.
294 294
295 295 For most users, the default value will simply be something like
296 296 :file:`$HOME/.config/ipython` on Linux, or :file:`$HOME/.ipython`
297 297 elsewhere.
298 298
299 299 Once the location of the IPython directory has been determined, you need to know
300 300 which profile you are using. For users with a single configuration, this will
301 301 simply be 'default', and will be located in
302 302 :file:`<IPYTHON_DIR>/profile_default`.
303 303
304 304 The next thing you need to know is what to call your configuration file. The
305 305 basic idea is that each application has its own default configuration filename.
306 306 The default named used by the :command:`ipython` command line program is
307 307 :file:`ipython_config.py`, and *all* IPython applications will use this file.
308 308 Other applications, such as the parallel :command:`ipcluster` scripts or the
309 309 QtConsole will load their own config files *after* :file:`ipython_config.py`. To
310 310 load a particular configuration file instead of the default, the name can be
311 311 overridden by the ``config_file`` command line flag.
312 312
313 313 To generate the default configuration files, do::
314 314
315 315 $> ipython profile create
316 316
317 317 and you will have a default :file:`ipython_config.py` in your IPython directory
318 318 under :file:`profile_default`. If you want the default config files for the
319 319 :mod:`IPython.parallel` applications, add ``--parallel`` to the end of the
320 320 command-line args.
321 321
322 322 .. _Profiles:
323 323
324 324 Profiles
325 325 ========
326 326
327 327 A profile is a directory containing configuration and runtime files, such as
328 328 logs, connection info for the parallel apps, and your IPython command history.
329 329
330 330 The idea is that users often want to maintain a set of configuration files for
331 331 different purposes: one for doing numerical computing with NumPy and SciPy and
332 332 another for doing symbolic computing with SymPy. Profiles make it easy to keep a
333 333 separate configuration files, logs, and histories for each of these purposes.
334 334
335 335 Let's start by showing how a profile is used:
336 336
337 337 .. code-block:: bash
338 338
339 339 $ ipython profile=sympy
340 340
341 341 This tells the :command:`ipython` command line program to get its configuration
342 342 from the "sympy" profile. The file names for various profiles do not change. The
343 343 only difference is that profiles are named in a special way. In the case above,
344 344 the "sympy" profile means looking for :file:`ipython_config.py` in :file:`<IPYTHON_DIR>/profile_sympy`.
345 345
346 346 The general pattern is this: simply create a new profile with:
347 347
348 348 .. code-block:: bash
349 349
350 350 ipython profile create <name>
351 351
352 352 which adds a directory called ``profile_<name>`` to your IPython directory. Then
353 353 you can load this profile by adding ``profile=<name>`` to your command line
354 354 options. Profiles are supported by all IPython applications.
355 355
356 356 IPython ships with some sample profiles in :file:`IPython/config/profile`. If
357 357 you create profiles with the name of one of our shipped profiles, these config
358 358 files will be copied over instead of starting with the automatically generated
359 359 config files.
360 360
361 361 .. _commandline:
362 362
363 363 Command-line arguments
364 364 ======================
365 365
366 366 IPython exposes *all* configurable options on the command-line. The command-line
367 367 arguments are generated from the Configurable traits of the classes associated
368 368 with a given Application. Configuring IPython from the command-line may look
369 369 very similar to an IPython config file
370 370
371 371 IPython applications use a parser called
372 372 :class:`~IPython.config.loader.KeyValueLoader` to load values into a Config
373 373 object. Values are assigned in much the same way as in a config file:
374 374
375 375 .. code-block:: bash
376 376
377 377 $> ipython InteractiveShell.use_readline=False BaseIPythonApplication.profile='myprofile'
378 378
379 379 Is the same as adding:
380 380
381 381 .. sourcecode:: python
382 382
383 383 c.InteractiveShell.use_readline=False
384 384 c.BaseIPythonApplication.profile='myprofile'
385 385
386 386 to your config file. Key/Value arguments *always* take a value, separated by '='
387 387 and no spaces.
388 388
389 389 Aliases
390 390 -------
391 391
392 392 For convenience, applications have a mapping of commonly
393 393 used traits, so you don't have to specify the whole class name. For these **aliases**, the class need not be specified:
394 394
395 395 .. code-block:: bash
396 396
397 397 $> ipython profile='myprofile'
398 398 # is equivalent to
399 399 $> ipython BaseIPythonApplication.profile='myprofile'
400 400
401 401 Flags
402 402 -----
403 403
404 404 Applications can also be passed **flags**. Flags are options that take no
405 405 arguments, and are always prefixed with ``--``. They are simply wrappers for
406 406 setting one or more configurables with predefined values, often True/False.
407 407
408 408 For instance:
409 409
410 410 .. code-block:: bash
411 411
412 412 $> ipcontroller --debug
413 413 # is equivalent to
414 414 $> ipcontroller Application.log_level=DEBUG
415 415 # and
416 416 $> ipython --pylab
417 417 # is equivalent to
418 418 $> ipython pylab=auto
419 419
420 420 Subcommands
421 421 -----------
422 422
423 423
424 424 Some IPython applications have **subcommands**. Subcommands are modeled after
425 425 :command:`git`, and are called with the form :command:`command subcommand
426 426 [...args]`. Currently, the QtConsole is a subcommand of terminal IPython:
427 427
428 428 .. code-block:: bash
429 429
430 430 $> ipython qtconsole profile=myprofile
431 431
432 432 and :command:`ipcluster` is simply a wrapper for its various subcommands (start,
433 433 stop, engines).
434 434
435 435 .. code-block:: bash
436 436
437 437 $> ipcluster start profile=myprofile n=4
438 438
439 439
440 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``.
441 441
442 442
443 443 Design requirements
444 444 ===================
445 445
446 446 Here are the main requirements we wanted our configuration system to have:
447 447
448 448 * Support for hierarchical configuration information.
449 449
450 450 * Full integration with command line option parsers. Often, you want to read
451 451 a configuration file, but then override some of the values with command line
452 452 options. Our configuration system automates this process and allows each
453 453 command line option to be linked to a particular attribute in the
454 454 configuration hierarchy that it will override.
455 455
456 456 * Configuration files that are themselves valid Python code. This accomplishes
457 457 many things. First, it becomes possible to put logic in your configuration
458 458 files that sets attributes based on your operating system, network setup,
459 459 Python version, etc. Second, Python has a super simple syntax for accessing
460 460 hierarchical data structures, namely regular attribute access
461 461 (``Foo.Bar.Bam.name``). Third, using Python makes it easy for users to
462 462 import configuration attributes from one configuration file to another.
463 Forth, even though Python is dynamically typed, it does have types that can
463 Fourth, even though Python is dynamically typed, it does have types that can
464 464 be checked at runtime. Thus, a ``1`` in a config file is the integer '1',
465 465 while a ``'1'`` is a string.
466 466
467 467 * A fully automated method for getting the configuration information to the
468 468 classes that need it at runtime. Writing code that walks a configuration
469 469 hierarchy to extract a particular attribute is painful. When you have
470 470 complex configuration information with hundreds of attributes, this makes
471 471 you want to cry.
472 472
473 473 * Type checking and validation that doesn't require the entire configuration
474 474 hierarchy to be specified statically before runtime. Python is a very
475 475 dynamic language and you don't always know everything that needs to be
476 476 configured when a program starts.
477 477
478 478
479 479 .. _`XDG Base Directory`: http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
General Comments 0
You need to be logged in to leave comments. Login now