-
Book Overview & Buying
-
Table Of Contents
IPython Interactive Computing and Visualization Cookbook
By :
IPython implements a truly powerful configuration system. This system is used throughout the project, but it can also be used by IPython extensions. It could even be used in entirely new applications.
In this recipe, we show how to use this system to write a configurable IPython extension. We will create a simple magic command that displays random numbers. This magic command comes with configurable parameters that can be set by the user in their IPython configuration file.
random_magics.py file. Let's start by importing a few objects.Be sure to put the code in steps 1-5 in an external text file named random_magics.py, rather than in the notebook's input!
from IPython.utils.traitlets import Int, Float, Unicode, Bool from IPython.core.magic import (Magics, magics_class, line_magic) import numpy as np
RandomMagics class deriving from Magics. This class contains a few configurable parameters:@magics_class
class RandomMagics(Magics):
text = Unicode(u'{n}', config=True)
max = Int(1000, config=True)
seed = Int(0, config=True) def __init__(self, shell):
super(RandomMagics, self).__init__(shell)
self._rng = np.random.RandomState(self.seed or None)%random line magic that displays a random number: @line_magic
def random(self, line):
return self.text.format(n=self._rng.randint(self.max))def load_ipython_extension(ipython):
ipython.register_magics(RandomMagics)In [1]: %load_ext random_magics In [2]: %random Out[2]: '635' In [3]: %random Out[3]: '47'
ipython --RandomMagics.text='Your number is {n}.' --RandomMagics.max=10 --RandomMagics.seed=1
In this session, we get the following behavior:
In [1]: %load_ext random_magics In [2]: %random Out[2]: u'Your number is 5.' In [3]: %random Out[3]: u'Your number is 8.'
~/.ipython/profile_default/ipython_config.py file and add the following line:c.RandomMagics.text = 'random {n}'After launching IPython, we get the following behavior:
In [4]: %random Out[4]: 'random 652'
IPython's configuration system defines several concepts:
xxx profile is stored in ~/.ipython/profile_xxx, where ~ is the user's home directory./home/yourname/.ipython/profile_xxxC:\Users\YourName\.ipython\profile_xxxConfig, is a special Python dictionary that contains key-value pairs. The Config class derives from Python's dict.HasTraits class is a class that can have special trait attributes. Traits are sophisticated Python attributes that have a specific type and a default value. Additionally, when a trait's value changes, a callback function is automatically and transparently called. This mechanism allows a class to be notified whenever a trait attribute is changed.Configurable class is the base class of all classes that want to benefit from the configuration system. A Configurable class can have configurable attributes. These attributes have default values specified directly in the class definition. The main feature of Configurable classes is that the default values of the traits can be overridden by configuration files on a class-by-class basis. Then, instances of Configurables can change these values at leisure.Configurable classes.The Configurable classes and configuration files support an inheritance model. A Configurable class can derive from another Configurable class and override its parameters. Similarly, a configuration file can be included in another file.
Here is a simple example of a Configurable class:
from IPython.config.configurable import Configurable
from IPython.utils.traitlets import Float
class MyConfigurable(Configurable):
myvariable = Float(100.0, config=True)By default, an instance of the MyConfigurable class will have its myvariable attribute equal to 100. Now, let's assume that our IPython configuration file contains the following lines:
c = get_config() c.MyConfigurable.myvariable = 123.
Then, the myvariable attribute will default to 123. Instances are free to change this default value after they are instantiated.
The get_config() function is a special function that is available in any configuration file.
Additionally, Configurable parameters can be specified in the command-line interface, as we saw in this recipe.
This configuration system is used by all IPython applications (notably console, qtconsole, and notebook). These applications have many configurable attributes. You will find the list of these attributes in your profile's configuration files.
The Magics class derives from Configurable and can contain configurable attributes. Moreover, magic commands can be defined by methods decorated by @line_magic or @cell_magic. The advantage of defining class magics instead of function magics (as in the previous recipe) is that we can keep a state between multiple magic calls (because we are using a class instead of a function).
Here are a few references:
Change the font size
Change margin width
Change background colour