栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 npnimport 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

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

 效果:

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/656903.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号