【问题标题】:How to add/edit code snippets in jupyer notebook?如何在 jupyter notebook 中添加/编辑代码片段?
【发布时间】:2019-08-11 04:06:15
【问题描述】:

我尝试按照扩展的自述文件中给出的说明进行操作。 我使用 Windows 并打开我的笔记本,我使用存储在目录中的 jupyter-notebook.exe

..\Anaconda3\脚本

在 Anaconda3 目录中,我转到子目录

Anaconda3\Lib\site-packages\jupyter_contrib_nbextensions\nbextensions\sn-ps

然后更改文件“sn-ps.json”的代码

{
"snippets" : [
    {
        "name" : "example",
        "code" : [
            "# This is an example snippet!",
            "# To create your own, add a new snippet block to the",
            "# snippets.json file in your jupyter data directory under nbextensions:",
            "# $(jupyter --data-dir)/nbextensions/snippets/snippets.json",
            "import this"
        ]
    }
]
}

到

{
    "snippets" : [
        {
            "name" : "example",
            "code" : [
                "# This is a test if something changed",
            ]
    ]
}

然后我重新启动我的笔记本并插入示例 sn-p。但是我的更改没有被采用,我仍然把原来的例子剪掉了。

我做错了什么?

【问题讨论】:

标签: jupyter-notebook code-snippets jupyter-contrib-nbextensions


【解决方案1】:

如果您使用 Anaconda,则不一定需要搜索目录。 “Nbextensions”选项卡中嵌入了一个模板。

  • 选中“片段菜单”框
  • 向下滚动到“参数”并选中“在下方包含自定义菜单...JSON 字符串”框
  • 插入任何你想要的样本 sn-p
  • 刷新你的笔记本

查看我的一个 sn-ps:

{
    "name" : "My favorites",
    "sub-menu" : [
        {
            "name" : "import packages",
            "snippet" : ["# import various packages"
                   "import os"
                   "import scipy"
                   "import pandas as pd"
                   "import numpy as np"
                   "import seaborn as sns"
                   "import matplotlib.pyplot as plt"

                   "%matplotlib inline"

                   "# plot settings"
                   "from pandas.plotting import register_matplotlib_converters"
                   "register_matplotlib_converters()"
                   "plt.rcParams['agg.path.chunksize'] = 10000"]
        },
        {
            "name" : "TeX can be written in menu labels $\\alpha_W e\\int_0 \\mu \\epsilon$",
            "snippet" : ["another_new_command(2.78)"]
        }
    ]
}

另外,请注意引号和逗号。可以在 here.

找到更多帮助

【讨论】:

    【解决方案2】:

    在 windows(os) 中,jupyter notebook 扩展隐藏在 programData 文件夹中 (C:\ProgramData\jupyter\nbextensions\sn-ps)

    要在 sn-p 中进行更改,只需相应地编辑 sn-ps.JSON 文件:

    【讨论】:

      【解决方案3】:

      我认为您在错误的目录中搜索。

      • 在 windows 中,在 anaconda 提示符下运行命令jupyter --paths 这将返回config: data: & runtime: 的位置
      • 在data: 位置搜索文件snippets.json
      • 在我的例子中是C:\ProgramData\jupyter\nbextensions\snippets

      更改并保存snippets.json 中的内容,然后重启你的jupyter notebook 它会工作的!

      【讨论】:

      • 我想指出 ProgramData 是一个隐藏的系统文件夹,因此它不会显示在文件资源管理器中。
      猜你喜欢
      • 2022-11-02
      • 2020-03-22
      • 1970-01-01
      • 2017-11-16
      • 1970-01-01
      • 2020-12-31
      • 1970-01-01
      • 2018-12-16
      • 2020-04-03
      相关资源
      最近更新 更多