背景
现在喜欢用jupyter,有一些代码段需要经常性的重复使用,所以找了设置snippet的方法,留个记号。
以下两个部件都可以,比较喜欢snippets
下拉框。
步骤
snippets">方法一(snippets
):
- 已经安装插件
snippets
(不是snippets menu)
jupyter notebook
中输入以下代码,【在IN[4]
中设置自定义代码段】- 其中
name
:代码段名称,下拉框中显示;code
:设置的代码
代码
# coding: utf-8
# # 导入相关模型,设置相关变量
# In[1]:
path = get_ipython().getoutput('jupyter --data-dir')
path = "".join([path[0],r"/nbextensions/snippets/snippets.json"])
import json
# # 查看已安装的snips
# In[2]:
file = open(path,"r",encoding="utf-8")
snips = json.load(file)
print("原来的snips:")
print(snips)
# # 设置自定义snips,<————**自定义snippet代码段写在这里**
# In[4]:
snips = {
"snippets" : [
{
"name" : "初始化jupyter",
"code" : [
'# Jupyter 默认设置',
'%matplotlib inline',
'%config InlineBackend.figure_format="retina"',
'%config InlineBackend.rc = {"figure.figsize": (7.5,4.5)}'
]
},
{
"name" : "导入常见模块",
"code":[
'%load "E:\桌面space\临时数据\python\ImportFile.py"'
]
},
{
"name" : "重新加载模块",
"code" : [
"# 重新加载所有模块",
"%load_ext autoreload",
"%autoreload 2"
]
},
]
}
# # 写入snips
# In[5]:
model = snips #数据
with open(path,'w',encoding='utf-8') as json_file:
json.dump(model,json_file,ensure_ascii=False)
方法二(snippet menu
):
1、已经安装插件snippet menu
2、见图示: