IPython has many configuration attributes. These can be viewed using the -h
flag to the command line applications:
!ipython -h
This is an important trick for finding out configuration info:
$> ipython [subcommand] --help-all | grep [-C context] PATTERN
--help-all
exposes everything configurable in IPython, there is a good chance you will find what you are looking for.
A common configuration question is:
how do I disable the "Do you really want to exit" message when quitting with
Ctrl-d
?
Well, logically this has to do with exit
, so let's look for it:
!ipython --help-all | grep exit
Which shows me that I can disable the confirmation for a single IPython session with
$> ipython --no-confirm-exit
or I can set the TerminalInteractiveShell.confirm_exit=False
in a config file, to have it be the default behavior.
Here are the design principles of the IPython configuration system:
Configurable
Bool
, Unicode
, etc.) that have config=True
Class.attr_name=the_value
--Class.attr_name=the_value
--attr-name=value
IPython has a notion of 'profiles' - these are directories that live in your IPYTHONDIR, which contain configuration and runtime information.
Let's create the default profile
!ipython profile create
This creates a profile in your IPYTHONDIR (ipython locate
is a quick way to see where your IPYTHONDIR is), and populates it with automatically generated default config files.
!ipython locate profile default
You can skim
profile = get_ipython().profile_dir.location
profile
ls $profile
Let's peek at our config file
pycat $profile/ipython_config.py
The %config
magic lets you do some configuration at runtime, rather than in configuration files.
%pylab inline
%config
with no arguments will show you what configurable objects exist:
%config
And %config Class
will show you the config for that class:
%config InlineBackend
Most importantly, the %config
magic can be used to change the value of a configurable attribute at runtime. Here we tell the inline matplotlib backend to use SVG instead of the default PNG
x = linspace(0,10,1000)
y = sin(x)
plot(x,sin(x))
%config InlineBackend.figure_format = 'svg'
plot(x,sin(x))
As we have seen already, IPython has cell and line magics. You can define your own magics using any Python function and the register_magic_function
method:
ip = get_ipython()
def sleep_magic(line):
"""A simple function for sleeping"""
t = float(line)
import time
time.sleep(t)
ip.register_magic_function(sleep_magic, "line", "sleep")
%sleep 2
%sleep?
ip.magics_manager.define_magic?
Define %tic
and %toc
magics, which can be use for simple timings, e.g. where
for p in range(1,4):
N = 10**p
print "N=%i" % N
%tic
A = np.random.random((N,N))
np.linalg.eigvals(A)
%toc
each %toc
will print the time since the last %tic
. Create separate tic
and toc
functions that read and write a global time variable.
%load soln/tictocf.py
import time
def format_time(dt):
if dt < 1e-6:
return u"%.3g ns" % (dt * 1e9)
elif dt < 1e-3:
return u"%.3g µs" % (dt * 1e6)
elif dt < 1:
return u"%.3g ms" % (dt * 1e3)
else:
return "%.3g s" % dt
def tic(line):
global t0
t0 = time.time()
def toc(line):
global t0
print format_time(time.time() - t0)
ip = get_ipython()
ip.register_magic_function(tic)
ip.register_magic_function(toc)
import numpy as np
for p in range(1,4):
N = 10**p
print "N=%i" % N
%tic
A = np.random.random((N,N))
np.linalg.eigvals(A)
%toc
Startup files are simple Python or IPython scripts that are run whenever you start IPython. These are a useful way to do super common imports, or for building database connections to load on startup of a non-default profile.
We can use a startup file to ensure that our %tic/toc
magics are always defined, every time we start IPython.
!ls $profile/startup
Adding common imports, so we never have to forget them again
%%file simpleimports.py
import sys, os, time, re
!cp simpleimports.py $profile/startup/simpleimports.py
And the same can be done for our tic/toc magics
!cp soln/tictocf.py $profile/startup/tictocf.py
Restart the kernel and then run the following cells immediately to verify these scripts have been executed:
sys
%tic
time.sleep(0.5)
%toc
Orignal Source: IPython: Interactive Computing in Python's Git repository