jupyter中使用snippets创建代码块,少敲点重复代码

# 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":[
                 'import numpy as np'
                '\n'
                'import torch'
            ]
        },
        {
            "name" : "重新加载模块",
            "code" : [
                "# 重新加载所有模块",
                "%load_ext autoreload",
                 "%autoreload 2"
            ]
        },        
    ]
}
原来的snips:
{'snippets': [{'name': '初始化jupyter', 'code': ['# Jupyter 默认设置', '%matplotlib inline', '%config InlineBackend.figure_format="retina"', '%config InlineBackend.rc = {"figure.figsize": (7.5,4.5)}']}, {'name': '导入常见模块', 'code': ['import numpy as np\nimport torch']}, {'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)
x1.00

把要保存的代码写在这里:

上面的代码是新建一个python3文件写的,运行之后就会出现工具栏里面的代码段

 效果:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值