40

In the first cell of every iPython (Jupyter) notebook, I almost always type:

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

Is there a way to make it so that this cell appears at the top of each new notebook I create by default?

For example, could I save a template .ipynb file somewhere, which is copied by iPython when creating a new notebook?

(I found this question, but it seems to be more about css than default content in cells.)

Community
  • 1
  • 1
Bill Cheatham
  • 11,396
  • 17
  • 69
  • 104

3 Answers3

11

I know it may not be what you're looking for (this example is not good for working on notebooks that need to be run in multiple environments, e.g. shared), but I put the following in a file called ipython_config.py in my .ipython folder.

c.InteractiveShellApp.exec_lines = ['%matplotlib inline',
    'import numpy as np',
    'import scipy.constants as scc',
    'import scipy.integrate as sci',
    'from mpl_toolkits.mplot3d import Axes3D',
    'import scipy.optimize as sco'
]

This runs before anything runs in any interactive console, including the jupyter notebook. If you want explicit boilerplating, I think that you will be disappointed (unless you want to build in the functionality for us ☺)

Riet
  • 1,240
  • 1
  • 15
  • 28
  • 8
    Setting up the implicit imports in the config spoils the portability of the notebooks. It would be much better to have a template with explicit imports for clarity because in the Zen of Python: "Explicit is better than implicit". – Mike Aug 16 '16 at 21:15
  • 2
    I keep revisiting this question myself to see if someone has figured it out. I think that it could be done with a customization of the pre_save_hook that jupyter uses when it saves a notebook, but I still haven't figured it out. – Riet Aug 16 '16 at 21:25
  • 3
    This is a hack but works. I keep a startup.py (several) file which is simply a case in Jupyter of calling %load startup.py. Nothing more than a quick-type but it works... – Time Lord Dec 03 '17 at 03:41
  • 1
    It would be so cool if we could install templates, which would end up under the "New" dropdown menu from the Jupyter file browser.. – ajwood Aug 10 '18 at 17:48
4

There is a pretty cool solution using jupyterlab: jupyterlab_templates

Installation took about 5 minutes and you can have as many

templates as you'd like.

Mac Users

Assuming you have conda installed than during the installation, you'll need to install node js engine:

  • conda install -c conda-forge nodejs

And create a jupter_noteook_config.py if you don't have one :

  • jupyter notebook --generate-config
oopsi
  • 1,919
  • 3
  • 21
  • 28
3

You can do this with ipython startup files. Create a new startup file in the default profile and it'll be pulled in by jupyter automatically when it starts ipython.

 ~/.ipython/profile_default/startup/00-startup.py
Matthew Fioravante
  • 1,478
  • 15
  • 19
  • 2
    this is a nice solution but it does not textually adds the header to the notebook. This is problem when sharing nbs with other people since the import are not shared. – elbOlita Feb 26 '19 at 10:22