Creating user profiles for custom behaviour and functionality

To create a new profile:


In [ ]:
!ipython profile create pycones14

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.


In [ ]:
profile_path = !ipython locate profile pycones14

In [ ]:
profile_path = profile_path[0]
profile_path

In [ ]:
ls -lh $profile_path

Startup files

In the startup folder of your new profile you should have a README file with the following content:


This is the IPython startup directory

*.py* and *.ipy* files in this directory will be run *prior* to any code or files specified via the exec_lines or exec_files configurables whenever you load this profile.

Files will be run in lexicographical order, so you can control the execution order of files with a prefix, e.g.:

    00-first.py
    50-middle.py
    99-last.ipy

This files are executed at the beginning when the profile is used. You can use python (*.py) or ipython (*.ipy) scripts. For example, most of the times I do the following in the first code cell of the notebooks:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

This is not valid python code but it is valid ipython code so you can do the following.

You can find the following paragraph in the official docs summarising the text above:

If you want some code to be run at the beginning of every IPython session, the easiest way is to add Python (***.py***) or IPython (***.ipy***) scripts to your ***profile_default/startup/*** directory. Files here will be executed as soon as the IPython shell is constructed, before any other code or scripts you have specified. The files will be run in order of their names, so you can control the ordering with prefixes, like ***10-myimports.py***.

In [ ]:
%%writefile $profile_path/startup/01-startimports.ipy
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

[TIP] If you want valid python scripts you can use the IPython magics as follows:

from IPython import get_ipython
ipython = get_ipython()
ipython.magic("timeit for i in range(10000):pass")

This way you don't have to use ***.ipy*** scripts and you have valid python scripts. In the previous case we could do:

%%writefile $profile_path/startup/01-startimports.py
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from IPython import get_ipython
ipython = get_ipython()
ipython.magic("matplotlib inline")
 

Now, we can start with our new profile and all the included files will be executed:

ipython notebook --profile pycones14

*_config_* files (see previous notebook)

In the root of your new profile dir you could find the following files:

  • ipython_config.py
  • ipython_kernel_config.py
  • ipython_nbconvert_config.py
  • ipython_notebook_config.py
  • ipython_qtconsole_config.py

The official docs could be found in this link.

Let's show the config file for the notebook.


In [ ]:
%load $profile_path/ipython_notebook_config.py