python - 在 Jupyter 笔记本中默认配置第一个单元格

标签 python jupyter jupyter-notebook

有没有办法在 Jupyter notebook 中为特定的 python 内核配置默认的第一个单元格?我同意默认的 python 导入违背了良好的编码习惯。

那么,我可以配置笔记本,使新 python 笔记本的第一个单元格始终是

import numpy as np

例如?

最佳答案

如上所述创建 IPython 配置文件是一个很好的第一个解决方案,但 IMO 它并不完全令人满意,尤其是在代码共享方面。

通过命令 exec_lines 导入的库的名称不会出现在笔记本中,因此您很容易忘记它。在另一个配置文件/机器上运行代码会引发错误。

因此我建议使用 Jupyter notebook 扩展,因为会显示导入的库。 它避免了在笔记本的开头总是导入相同的库。 首先需要安装Jupyter的nbextension。 您可以克隆存储库:https://github.com/ipython-contrib/jupyter_contrib_nbextensions 或使用 pip:pip install jupyter_contrib_nbextensions

然后您可以通过将文件夹“default_cells”添加到安装 nb 扩展的路径来创建 nb 扩展。例如在 Ubuntu 上它是/usr/local/share/jupyter/nbextensions/.,也许在 Windows 上:C:\Users\xxx.xxx\AppData\Roaming\jupyter\nbextensions\

您必须在此文件夹中创建 3 个文件:

  • main.js 包含扩展的js代码
  • Jupyter 中 API 的 default_cells.yaml 描述
  • README.MD 出现在 API 中的读者常用描述。

我使用的代码来自:https://github.com/jupyter/notebook/issues/1451my main.js 是:

    define([
        'base/js/namespace'
    ], function(
        Jupyter
    ) {
        function load_ipython_extension() {
          if (Jupyter.notebook.get_cells().length===1){
       //do your thing
            Jupyter.notebook.insert_cell_above('code', 0).set_text("# Scientific libraries\nimport numpy as np\nimport scipy\n\n# import Pandas\n\nimport pandas as pd\n\n# Graphic libraries\n\nimport matplotlib as plt\n%matplotlib inline\nimport seaborn as sns\nfrom plotly.offline import init_notebook_mode, iplot, download_plotlyjs\ninit_notebook_mode()\nimport plotly.graph_objs as go\n\n# Extra options \n\npd.options.display.max_rows = 10\npd.set_option('max_columns', 50)\nsns.set(style='ticks', context='talk')\n\n# Creating alias for magic commands\n%alias_magic t time");
          }
        }
        return {
            load_ipython_extension: load_ipython_extension
        };
    });

.yaml 的格式必须如下所示:

    Type: IPython Notebook Extension
    Compatibility: 3.x, 4.x
    Name: Default cells
    Main: main.js
    Link: README.md
    Description: |
      Add a default cell for each new notebook. Useful when you import always the same libraries$
    
    
    Parameters:
    - none

和 README.md

    default_cells
    =========
    
    Add default cells to each new notebook. You have to modify this line in the main.js file to change your default cell. For example
    
    `Jupyter.notebook.insert_cell_above('code', 0).set_text("import numpy as np/nimportpandas as pd")`
    
    
    You can also add another default cell by creating a new line just below :
    
    `Jupyter.notebook.insert_cell_above('code', 1).set_text("from sklearn.meatrics import mean_squared_error")`
    
    **Don't forget to increment 1 if you want more than one extra cell. **

然后您只需在 Jupyter 中出现的新选项卡“nbextensions”中启用“默认单元格”扩展。

唯一的问题是它通过查看笔记本中的单元格数量来检测笔记本是否是新的。但是,如果您将所有代码都写在一个单元格中,它会将其检测为一个新笔记本并仍然添加默认单元格。

关于python - 在 Jupyter 笔记本中默认配置第一个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36194865/

相关文章:

python - 索引超出迭代范围

python - IPython笔记本: how do I change the text color in output cells?

widget - 更改 ipywidgets 上的按钮名称

python - 软件包安装在 anaconda\lib\site-packages 中并在 spy 程序中找到,但不在 jupyter 笔记本中

python - Django REST Framework 嵌套序列化程序不会更新

python - 使用替换方法替换 Python 中字符串中的值不起作用

python - subprocess.Popen 后如何清理?

apache-spark - Jupyter Notebook 仅在 Spark 本地运行

ipython - Jupyter 在哪个 conda 环境中执行?

Jupyter-notebook 不显示我的桌面文件夹