deepak
deepak

Reputation: 2075

Create IPython notebooks with some cells already populated

When I create a new IPython notebook it opens a blank notebook. I would like instead that all my notebooks open with a few cells already populated with stuff that I always use. For example, the first cell would have some magic commands

%load_ext autoreload
%autoreload 2
%matplotlib inline

The second cell might contain some standard imports

import numpy as np
import matplotlib as mpl
from matplotlib import pyplot as plt
from matplotlib.collections import PatchCollection
from netCDF4 import Dataset
from tabulate import tabulate

Is it possible to tell IPython somewhere how to template new files so that this can be done?

Upvotes: 4

Views: 165

Answers (1)

Matt Pitkin
Matt Pitkin

Reputation: 6497

You could set up a configuration file for ipython as described here. For example run:

ipython profile create

to create a default ipython profile (probably with the name ${HOME}/.ipython/profile_default/ipython_config.py directory) (or you could just create this file yourself). You could then edit this to include e.g.:

c = get_config()

c.InteractiveShellApp.exec_lines = [
    '%load_ext autoreload',
    '%autoreloud 2',
    '%matplotlib inline',
    'import numpy as np',
    'import matplotlib as mpl',
    'from matplotlib import pyplot as plt',
    'from matplotlib.collections import PatchCollection',
    'from netCDF4 import Dataset',
    'from tabulate import tabulate'
]

However, this just runs commands in the background before your notebook ipython session starts and doesn't actually populate the first cell.

Upvotes: 1

Related Questions