Creating a Custom Jupyter Kernel Configuration

When creating Jupyter notebooks we find ourselves in most occasions repeating a lot of setup code at the start: loading numpy, scipy, matplotlib, setting figures size and style, etc.

I describe here how to create a custom kernel with customizations that load the required packages at startup up and make them available by default in our Jupyter notebooks.

Create a new Jupyter Kernel

The first thing is to make sure we have the ipykernel module installed. Then we use IPython to create the profile that we call jupyter in this example.

conda install ipykernel
ipython profile create jupyter

Next we create a kernel.json file with our kernel. The display_name is the name under which it will be available in the Jupyter kernel menu.

mkdir -p $HOME/.ipython/kernels/jupyter

cat > ~/.ipython/kernels/jupyter/kernel.json <<EOF
    "display_name": "Scientific Python",
    "language": "python",
    "argv": [

Customize IPython

The kernel defined above will run IPython with the jupyter profile. The directory $HOME/.ipython/profile_jupyter/startup contains Python files that will be run at startup.

Let’s create the file inside that directory with the following contents:

import astropy
import scipy

import numpy as np
import pandas as pd
import sklearn as skl
import skimage as ski
import tensorflow as tf

import matplotlib as mpl
import matplotlib.pyplot as plt

and with:

def info_versions():
    fmt = '{:20s} : {}'
    print(fmt.format('Numpy', np.__version__))
    print(fmt.format('Scipy ', scipy.__version__))
    print(fmt.format('Matplotlib ', mpl.__version__))
    print(fmt.format('Pandas ', pd.__version__))
    print(fmt.format('Astropy ', astropy.__version__))
    print(fmt.format('Scikit-learn ', skl.__version__))
    print(fmt.format('Scikit-image ', ski.__version__))
    print(fmt.format('Tensorflow ', tf.__version__))

def load_custom():
    plt.rcParams[''] = 'Verdana'
    plt.rcParams['figure.figsize'] = [6*1.8 ,4*1.8]
    plt.rcParams['font.size'] = 14
    plt.rcParams['axes.labelsize'] = 2 + plt.rcParams['font.size']
    plt.rcParams['xtick.labelsize'] = plt.rcParams['font.size']
    plt.rcParams['ytick.labelsize'] = plt.rcParams['font.size']
    plt.rcParams['xtick.major.size'] = 6
    plt.rcParams['xtick.minor.size'] = 3
    plt.rcParams['xtick.major.width'] = 1
    plt.rcParams['xtick.minor.width'] = 1
    plt.rcParams['ytick.major.size'] = 6
    plt.rcParams['ytick.minor.size'] = 3
    plt.rcParams['ytick.major.width'] = 1
    plt.rcParams['ytick.minor.width'] = 1
    plt.rcParams['legend.frameon'] = False

Last is to add a line to

cd $HOME/.ipython/profile_jupyter
echo "c.InteractiveShellApp.exec_lines = ['%matplotlib inline','load_custom()']" >>

On the next restart of Jupyter select ‘Scientic Python’ from the kernel list to activate this configuration.