##// END OF EJS Templates
Fix header levels in technical description of config
Thomas Kluyver -
Show More
@@ -1,566 +1,566 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.
8 8
9 9 The main concepts
10 10 =================
11 11
12 12 There are a number of abstractions that the IPython configuration system uses.
13 13 Each of these abstractions is represented by a Python class.
14 14
15 15 Configuration object: :class:`~IPython.config.loader.Config`
16 16 A configuration object is a simple dictionary-like class that holds
17 17 configuration attributes and sub-configuration objects. These classes
18 18 support dotted attribute style access (``cfg.Foo.bar``) in addition to the
19 19 regular dictionary style access (``cfg['Foo']['bar']``).
20 20 The Config object is a wrapper around a simple dictionary with some convenience methods,
21 21 such as merging and automatic section creation.
22 22
23 23 Application: :class:`~IPython.config.application.Application`
24 24 An application is a process that does a specific job. The most obvious
25 25 application is the :command:`ipython` command line program. Each
26 26 application reads *one or more* configuration files and a single set of
27 27 command line options
28 28 and then produces a master configuration object for the application. This
29 29 configuration object is then passed to the configurable objects that the
30 30 application creates. These configurable objects implement the actual logic
31 31 of the application and know how to configure themselves given the
32 32 configuration object.
33 33
34 34 Applications always have a `log` attribute that is a configured Logger.
35 35 This allows centralized logging configuration per-application.
36 36
37 37 Configurable: :class:`~IPython.config.configurable.Configurable`
38 38 A configurable is a regular Python class that serves as a base class for
39 39 all main classes in an application. The
40 40 :class:`~IPython.config.configurable.Configurable` base class is
41 41 lightweight and only does one things.
42 42
43 43 This :class:`~IPython.config.configurable.Configurable` is a subclass
44 44 of :class:`~IPython.utils.traitlets.HasTraits` that knows how to configure
45 45 itself. Class level traits with the metadata ``config=True`` become
46 46 values that can be configured from the command line and configuration
47 47 files.
48 48
49 49 Developers create :class:`~IPython.config.configurable.Configurable`
50 50 subclasses that implement all of the logic in the application. Each of
51 51 these subclasses has its own configuration information that controls how
52 52 instances are created.
53 53
54 54 Singletons: :class:`~IPython.config.configurable.SingletonConfigurable`
55 55 Any object for which there is a single canonical instance. These are
56 56 just like Configurables, except they have a class method
57 57 :meth:`~IPython.config.configurable.SingletonConfigurable.instance`,
58 58 that returns the current active instance (or creates one if it
59 59 does not exist). Examples of singletons include
60 60 :class:`~IPython.config.application.Application`s and
61 61 :class:`~IPython.core.interactiveshell.InteractiveShell`. This lets
62 62 objects easily connect to the current running Application without passing
63 63 objects around everywhere. For instance, to get the current running
64 64 Application instance, simply do: ``app = Application.instance()``.
65 65
66 66
67 67 .. note::
68 68
69 69 Singletons are not strictly enforced - you can have many instances
70 70 of a given singleton class, but the :meth:`instance` method will always
71 71 return the same one.
72 72
73 73 Having described these main concepts, we can now state the main idea in our
74 74 configuration system: *"configuration" allows the default values of class
75 75 attributes to be controlled on a class by class basis*. Thus all instances of
76 76 a given class are configured in the same way. Furthermore, if two instances
77 77 need to be configured differently, they need to be instances of two different
78 78 classes. While this model may seem a bit restrictive, we have found that it
79 79 expresses most things that need to be configured extremely well. However, it
80 80 is possible to create two instances of the same class that have different
81 81 trait values. This is done by overriding the configuration.
82 82
83 83 Now, we show what our configuration objects and files look like.
84 84
85 85 Configuration objects and files
86 86 ===============================
87 87
88 88 A configuration object is little more than a wrapper around a dictionary.
89 89 A configuration *file* is simply a mechanism for producing that object.
90 90 The main IPython configuration file is a plain Python script,
91 91 which can perform extensive logic to populate the config object.
92 92 IPython 2.0 introduces a JSON configuration file,
93 93 which is just a direct JSON serialization of the config dictionary,
94 94 which is easily processed by external software.
95 95
96 96 When both Python and JSON configuration file are present, both will be loaded,
97 97 with JSON configuration having higher priority.
98 98
99 99 Python configuration Files
100 ~~~~~~~~~~~~~~~~~~~~~~~~~~
100 --------------------------
101 101
102 102 A Python configuration file is a pure Python file that populates a configuration object.
103 103 This configuration object is a :class:`~IPython.config.loader.Config` instance.
104 104 While in a configuration file, to get a reference to this object, simply call the :func:`get_config`
105 105 function, which is available in the global namespace of the script.
106 106
107 107 Here is an example of a super simple configuration file that does nothing::
108 108
109 109 c = get_config()
110 110
111 111 Once you get a reference to the configuration object, you simply set
112 112 attributes on it. All you have to know is:
113 113
114 114 * The name of the class to configure.
115 115 * The name of the attribute.
116 116 * The type of each attribute.
117 117
118 118 The answers to these questions are provided by the various
119 119 :class:`~IPython.config.configurable.Configurable` subclasses that an
120 120 application uses. Let's look at how this would work for a simple configurable
121 121 subclass::
122 122
123 123 # Sample configurable:
124 124 from IPython.config.configurable import Configurable
125 125 from IPython.utils.traitlets import Int, Float, Unicode, Bool
126 126
127 127 class MyClass(Configurable):
128 128 name = Unicode(u'defaultname', config=True)
129 129 ranking = Int(0, config=True)
130 130 value = Float(99.0)
131 131 # The rest of the class implementation would go here..
132 132
133 133 In this example, we see that :class:`MyClass` has three attributes, two
134 134 of which (``name``, ``ranking``) can be configured. All of the attributes
135 135 are given types and default values. If a :class:`MyClass` is instantiated,
136 136 but not configured, these default values will be used. But let's see how
137 137 to configure this class in a configuration file::
138 138
139 139 # Sample config file
140 140 c = get_config()
141 141
142 142 c.MyClass.name = 'coolname'
143 143 c.MyClass.ranking = 10
144 144
145 145 After this configuration file is loaded, the values set in it will override
146 146 the class defaults anytime a :class:`MyClass` is created. Furthermore,
147 147 these attributes will be type checked and validated anytime they are set.
148 148 This type checking is handled by the :mod:`IPython.utils.traitlets` module,
149 149 which provides the :class:`Unicode`, :class:`Int` and :class:`Float` types.
150 150 In addition to these traitlets, the :mod:`IPython.utils.traitlets` provides
151 151 traitlets for a number of other types.
152 152
153 153 .. note::
154 154
155 155 Underneath the hood, the :class:`Configurable` base class is a subclass of
156 156 :class:`IPython.utils.traitlets.HasTraits`. The
157 157 :mod:`IPython.utils.traitlets` module is a lightweight version of
158 158 :mod:`enthought.traits`. Our implementation is a pure Python subset
159 159 (mostly API compatible) of :mod:`enthought.traits` that does not have any
160 160 of the automatic GUI generation capabilities. Our plan is to achieve 100%
161 161 API compatibility to enable the actual :mod:`enthought.traits` to
162 162 eventually be used instead. Currently, we cannot use
163 163 :mod:`enthought.traits` as we are committed to the core of IPython being
164 164 pure Python.
165 165
166 166 It should be very clear at this point what the naming convention is for
167 167 configuration attributes::
168 168
169 169 c.ClassName.attribute_name = attribute_value
170 170
171 171 Here, ``ClassName`` is the name of the class whose configuration attribute you
172 172 want to set, ``attribute_name`` is the name of the attribute you want to set
173 173 and ``attribute_value`` the the value you want it to have. The ``ClassName``
174 174 attribute of ``c`` is not the actual class, but instead is another
175 175 :class:`~IPython.config.loader.Config` instance.
176 176
177 177 .. note::
178 178
179 179 The careful reader may wonder how the ``ClassName`` (``MyClass`` in
180 180 the above example) attribute of the configuration object ``c`` gets
181 181 created. These attributes are created on the fly by the
182 182 :class:`~IPython.config.loader.Config` instance, using a simple naming
183 183 convention. Any attribute of a :class:`~IPython.config.loader.Config`
184 184 instance whose name begins with an uppercase character is assumed to be a
185 185 sub-configuration and a new empty :class:`~IPython.config.loader.Config`
186 186 instance is dynamically created for that attribute. This allows deeply
187 187 hierarchical information created easily (``c.Foo.Bar.value``) on the fly.
188 188
189 189 JSON configuration Files
190 ~~~~~~~~~~~~~~~~~~~~~~~~
190 ------------------------
191 191
192 192 A JSON configuration file is simply a file that contains a
193 193 :class:`~IPython.config.loader.Config` dictionary serialized to JSON.
194 194 A JSON configuration file has the same base name as a Python configuration file,
195 195 but with a .json extension.
196 196
197 197 Configuration described in previous section could be written as follows in a
198 198 JSON configuration file:
199 199
200 200 .. sourcecode:: json
201 201
202 202 {
203 203 "version": "1.0",
204 204 "MyClass": {
205 205 "name": "coolname",
206 206 "ranking": 10
207 207 }
208 208 }
209 209
210 210 JSON configuration files can be more easily generated or processed by programs
211 211 or other languages.
212 212
213 213
214 214 Configuration files inheritance
215 215 ===============================
216 216
217 217 .. note::
218 218
219 219 This section only apply to Python configuration files.
220 220
221 221 Let's say you want to have different configuration files for various purposes.
222 222 Our configuration system makes it easy for one configuration file to inherit
223 223 the information in another configuration file. The :func:`load_subconfig`
224 224 command can be used in a configuration file for this purpose. Here is a simple
225 225 example that loads all of the values from the file :file:`base_config.py`::
226 226
227 227 # base_config.py
228 228 c = get_config()
229 229 c.MyClass.name = 'coolname'
230 230 c.MyClass.ranking = 100
231 231
232 232 into the configuration file :file:`main_config.py`::
233 233
234 234 # main_config.py
235 235 c = get_config()
236 236
237 237 # Load everything from base_config.py
238 238 load_subconfig('base_config.py')
239 239
240 240 # Now override one of the values
241 241 c.MyClass.name = 'bettername'
242 242
243 243 In a situation like this the :func:`load_subconfig` makes sure that the
244 244 search path for sub-configuration files is inherited from that of the parent.
245 245 Thus, you can typically put the two in the same directory and everything will
246 246 just work.
247 247
248 248 You can also load configuration files by profile, for instance:
249 249
250 250 .. sourcecode:: python
251 251
252 252 load_subconfig('ipython_config.py', profile='default')
253 253
254 254 to inherit your default configuration as a starting point.
255 255
256 256
257 257 Class based configuration inheritance
258 258 =====================================
259 259
260 260 There is another aspect of configuration where inheritance comes into play.
261 261 Sometimes, your classes will have an inheritance hierarchy that you want
262 262 to be reflected in the configuration system. Here is a simple example::
263 263
264 264 from IPython.config.configurable import Configurable
265 265 from IPython.utils.traitlets import Int, Float, Unicode, Bool
266 266
267 267 class Foo(Configurable):
268 268 name = Unicode(u'fooname', config=True)
269 269 value = Float(100.0, config=True)
270 270
271 271 class Bar(Foo):
272 272 name = Unicode(u'barname', config=True)
273 273 othervalue = Int(0, config=True)
274 274
275 275 Now, we can create a configuration file to configure instances of :class:`Foo`
276 276 and :class:`Bar`::
277 277
278 278 # config file
279 279 c = get_config()
280 280
281 281 c.Foo.name = u'bestname'
282 282 c.Bar.othervalue = 10
283 283
284 284 This class hierarchy and configuration file accomplishes the following:
285 285
286 286 * The default value for :attr:`Foo.name` and :attr:`Bar.name` will be
287 287 'bestname'. Because :class:`Bar` is a :class:`Foo` subclass it also
288 288 picks up the configuration information for :class:`Foo`.
289 289 * The default value for :attr:`Foo.value` and :attr:`Bar.value` will be
290 290 ``100.0``, which is the value specified as the class default.
291 291 * The default value for :attr:`Bar.othervalue` will be 10 as set in the
292 292 configuration file. Because :class:`Foo` is the parent of :class:`Bar`
293 293 it doesn't know anything about the :attr:`othervalue` attribute.
294 294
295 295
296 296 .. _ipython_dir:
297 297
298 298 Configuration file location
299 299 ===========================
300 300
301 301 So where should you put your configuration files? IPython uses "profiles" for
302 302 configuration, and by default, all profiles will be stored in the so called
303 303 "IPython directory". The location of this directory is determined by the
304 304 following algorithm:
305 305
306 306 * If the ``ipython-dir`` command line flag is given, its value is used.
307 307
308 308 * If not, the value returned by :func:`IPython.utils.path.get_ipython_dir`
309 309 is used. This function will first look at the :envvar:`IPYTHONDIR`
310 310 environment variable and then default to :file:`~/.ipython`.
311 311 Historical support for the :envvar:`IPYTHON_DIR` environment variable will
312 312 be removed in a future release.
313 313
314 314 For most users, the configuration directory will be :file:`~/.ipython`.
315 315
316 316 Previous versions of IPython on Linux would use the XDG config directory,
317 317 creating :file:`~/.config/ipython` by default. We have decided to go
318 318 back to :file:`~/.ipython` for consistency among systems. IPython will
319 319 issue a warning if it finds the XDG location, and will move it to the new
320 320 location if there isn't already a directory there.
321 321
322 322 Once the location of the IPython directory has been determined, you need to know
323 323 which profile you are using. For users with a single configuration, this will
324 324 simply be 'default', and will be located in
325 325 :file:`<IPYTHONDIR>/profile_default`.
326 326
327 327 The next thing you need to know is what to call your configuration file. The
328 328 basic idea is that each application has its own default configuration filename.
329 329 The default named used by the :command:`ipython` command line program is
330 330 :file:`ipython_config.py`, and *all* IPython applications will use this file.
331 331 Other applications, such as the parallel :command:`ipcluster` scripts or the
332 332 QtConsole will load their own config files *after* :file:`ipython_config.py`. To
333 333 load a particular configuration file instead of the default, the name can be
334 334 overridden by the ``config_file`` command line flag.
335 335
336 336 To generate the default configuration files, do::
337 337
338 338 $ ipython profile create
339 339
340 340 and you will have a default :file:`ipython_config.py` in your IPython directory
341 341 under :file:`profile_default`. If you want the default config files for the
342 342 :mod:`IPython.parallel` applications, add ``--parallel`` to the end of the
343 343 command-line args.
344 344
345 345
346 346 Locating these files
347 347 --------------------
348 348
349 349 From the command-line, you can quickly locate the IPYTHONDIR or a specific
350 350 profile with:
351 351
352 352 .. sourcecode:: bash
353 353
354 354 $ ipython locate
355 355 /home/you/.ipython
356 356
357 357 $ ipython locate profile foo
358 358 /home/you/.ipython/profile_foo
359 359
360 360 These map to the utility functions: :func:`IPython.utils.path.get_ipython_dir`
361 361 and :func:`IPython.utils.path.locate_profile` respectively.
362 362
363 363
364 364 .. _profiles_dev:
365 365
366 366 Profiles
367 367 ========
368 368
369 369 A profile is a directory containing configuration and runtime files, such as
370 370 logs, connection info for the parallel apps, and your IPython command history.
371 371
372 372 The idea is that users often want to maintain a set of configuration files for
373 373 different purposes: one for doing numerical computing with NumPy and SciPy and
374 374 another for doing symbolic computing with SymPy. Profiles make it easy to keep a
375 375 separate configuration files, logs, and histories for each of these purposes.
376 376
377 377 Let's start by showing how a profile is used:
378 378
379 379 .. code-block:: bash
380 380
381 381 $ ipython --profile=sympy
382 382
383 383 This tells the :command:`ipython` command line program to get its configuration
384 384 from the "sympy" profile. The file names for various profiles do not change. The
385 385 only difference is that profiles are named in a special way. In the case above,
386 386 the "sympy" profile means looking for :file:`ipython_config.py` in :file:`<IPYTHONDIR>/profile_sympy`.
387 387
388 388 The general pattern is this: simply create a new profile with:
389 389
390 390 .. code-block:: bash
391 391
392 392 $ ipython profile create <name>
393 393
394 394 which adds a directory called ``profile_<name>`` to your IPython directory. Then
395 395 you can load this profile by adding ``--profile=<name>`` to your command line
396 396 options. Profiles are supported by all IPython applications.
397 397
398 398 IPython ships with some sample profiles in :file:`IPython/config/profile`. If
399 399 you create profiles with the name of one of our shipped profiles, these config
400 400 files will be copied over instead of starting with the automatically generated
401 401 config files.
402 402
403 403 Security Files
404 404 --------------
405 405
406 406 If you are using the notebook, qtconsole, or parallel code, IPython stores
407 407 connection information in small JSON files in the active profile's security
408 408 directory. This directory is made private, so only you can see the files inside. If
409 409 you need to move connection files around to other computers, this is where they will
410 410 be. If you want your code to be able to open security files by name, we have a
411 411 convenience function :func:`IPython.utils.path.get_security_file`, which will return
412 412 the absolute path to a security file from its filename and [optionally] profile
413 413 name.
414 414
415 415 .. _startup_files:
416 416
417 417 Startup Files
418 418 -------------
419 419
420 420 If you want some code to be run at the beginning of every IPython session with
421 421 a particular profile, the easiest way is to add Python (``.py``) or
422 422 IPython (``.ipy``) scripts to your :file:`<profile>/startup` directory. Files
423 423 in this directory will always be executed as soon as the IPython shell is
424 424 constructed, and before any other code or scripts you have specified. If you
425 425 have multiple files in the startup directory, they will be run in
426 426 lexicographical order, so you can control the ordering by adding a '00-'
427 427 prefix.
428 428
429 429
430 430 .. _commandline:
431 431
432 432 Command-line arguments
433 433 ======================
434 434
435 435 IPython exposes *all* configurable options on the command-line. The command-line
436 436 arguments are generated from the Configurable traits of the classes associated
437 437 with a given Application. Configuring IPython from the command-line may look
438 438 very similar to an IPython config file
439 439
440 440 IPython applications use a parser called
441 441 :class:`~IPython.config.loader.KeyValueLoader` to load values into a Config
442 442 object. Values are assigned in much the same way as in a config file:
443 443
444 444 .. code-block:: bash
445 445
446 446 $ ipython --InteractiveShell.use_readline=False --BaseIPythonApplication.profile='myprofile'
447 447
448 448 Is the same as adding:
449 449
450 450 .. sourcecode:: python
451 451
452 452 c.InteractiveShell.use_readline=False
453 453 c.BaseIPythonApplication.profile='myprofile'
454 454
455 455 to your config file. Key/Value arguments *always* take a value, separated by '='
456 456 and no spaces.
457 457
458 458 Common Arguments
459 459 ----------------
460 460
461 461 Since the strictness and verbosity of the KVLoader above are not ideal for everyday
462 462 use, common arguments can be specified as flags_ or aliases_.
463 463
464 464 Flags and Aliases are handled by :mod:`argparse` instead, allowing for more flexible
465 465 parsing. In general, flags and aliases are prefixed by ``--``, except for those
466 466 that are single characters, in which case they can be specified with a single ``-``, e.g.:
467 467
468 468 .. code-block:: bash
469 469
470 470 $ ipython -i -c "import numpy; x=numpy.linspace(0,1)" --profile testing --colors=lightbg
471 471
472 472 Aliases
473 473 *******
474 474
475 475 For convenience, applications have a mapping of commonly used traits, so you don't have
476 476 to specify the whole class name:
477 477
478 478 .. code-block:: bash
479 479
480 480 $ ipython --profile myprofile
481 481 # and
482 482 $ ipython --profile='myprofile'
483 483 # are equivalent to
484 484 $ ipython --BaseIPythonApplication.profile='myprofile'
485 485
486 486 Flags
487 487 *****
488 488
489 489 Applications can also be passed **flags**. Flags are options that take no
490 490 arguments. They are simply wrappers for
491 491 setting one or more configurables with predefined values, often True/False.
492 492
493 493 For instance:
494 494
495 495 .. code-block:: bash
496 496
497 497 $ ipcontroller --debug
498 498 # is equivalent to
499 499 $ ipcontroller --Application.log_level=DEBUG
500 500 # and
501 501 $ ipython --matplotlib
502 502 # is equivalent to
503 503 $ ipython --matplotlib auto
504 504 # or
505 505 $ ipython --no-banner
506 506 # is equivalent to
507 507 $ ipython --TerminalIPythonApp.display_banner=False
508 508
509 509 Subcommands
510 510 -----------
511 511
512 512
513 513 Some IPython applications have **subcommands**. Subcommands are modeled after
514 514 :command:`git`, and are called with the form :command:`command subcommand
515 515 [...args]`. Currently, the QtConsole is a subcommand of terminal IPython:
516 516
517 517 .. code-block:: bash
518 518
519 519 $ ipython qtconsole --profile myprofile
520 520
521 521 and :command:`ipcluster` is simply a wrapper for its various subcommands (start,
522 522 stop, engines).
523 523
524 524 .. code-block:: bash
525 525
526 526 $ ipcluster start --profile=myprofile -n 4
527 527
528 528
529 529 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``.
530 530
531 531
532 532 Design requirements
533 533 ===================
534 534
535 535 Here are the main requirements we wanted our configuration system to have:
536 536
537 537 * Support for hierarchical configuration information.
538 538
539 539 * Full integration with command line option parsers. Often, you want to read
540 540 a configuration file, but then override some of the values with command line
541 541 options. Our configuration system automates this process and allows each
542 542 command line option to be linked to a particular attribute in the
543 543 configuration hierarchy that it will override.
544 544
545 545 * Configuration files that are themselves valid Python code. This accomplishes
546 546 many things. First, it becomes possible to put logic in your configuration
547 547 files that sets attributes based on your operating system, network setup,
548 548 Python version, etc. Second, Python has a super simple syntax for accessing
549 549 hierarchical data structures, namely regular attribute access
550 550 (``Foo.Bar.Bam.name``). Third, using Python makes it easy for users to
551 551 import configuration attributes from one configuration file to another.
552 552 Fourth, even though Python is dynamically typed, it does have types that can
553 553 be checked at runtime. Thus, a ``1`` in a config file is the integer '1',
554 554 while a ``'1'`` is a string.
555 555
556 556 * A fully automated method for getting the configuration information to the
557 557 classes that need it at runtime. Writing code that walks a configuration
558 558 hierarchy to extract a particular attribute is painful. When you have
559 559 complex configuration information with hundreds of attributes, this makes
560 560 you want to cry.
561 561
562 562 * Type checking and validation that doesn't require the entire configuration
563 563 hierarchy to be specified statically before runtime. Python is a very
564 564 dynamic language and you don't always know everything that needs to be
565 565 configured when a program starts.
566 566
General Comments 0
You need to be logged in to leave comments. Login now