创建已填充一些单元格的IPython笔记本

当我创建一个新的I Python笔记本时,它会打开一个空白笔记本.我希望我的所有笔记本都打开了几个单元格已经填充了我一直使用的东西.例如,第一个单元格将具有一些魔术命令

%load_ext autoreload
%autoreload 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

有可能告诉IPython某处如何模板化新文件以便这样做?

最佳答案 您可以为 here所述的ipython设置配置文件.例如,运行:

ipython profile create

创建一个默认的ipython配置文件(可能名为${HOME} /.ipython / profile_default / ipython_config.py目录)(或者您可以自己创建此文件).然后你可以编辑它以包括例如:

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'
]

但是,这只是在您的笔记本ipython会话开始之前在后台运行命令,并且实际上并不填充第一个单元格.

点赞