默认情况下在Jupyter笔记本电脑中配置第一个单元格 [英] Configure a first cell by default in Jupyter notebooks

查看:82
本文介绍了默认情况下在Jupyter笔记本电脑中配置第一个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TI是否可以为Jupyter笔记本中的特定python内核配置默认的第一个单元格?我同意默认的python导入违反良好的编码习惯.

TIs there a way to configure a default first cell for a specific python kernel in the Jupyter notebook? I agree that default python imports go against good coding practices.

因此,我可以配置笔记本以使新python笔记本的第一个单元始终为

So, can I configure the notebook such that the first cell of a new python notebook is always

import numpy as np

例如?

推荐答案

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

Creating an IPython profile as mentioned above is a good first solution, but IMO it isn't totally satisfying, especially when it comes to code sharing.

通过命令exec_lines导入的库的名称未出现在笔记本中,因此您可以轻松地将其忘记.在其他配置文件/机器上运行代码将引发错误.

The names of your libraries imported through the command exec_lines do not appear in the notebook, so you can easily forget it. And running the code on another profile / machine would raise an error.

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

Therefore I would recommend to use a Jupyter notebook extension, because the imported libraries are displayed. It avoids importing always the same libraries at the beginning of a notebook. First you need to install the nbextension of Jupyter. You can either clone the repo : https://github.com/ipython-contrib/jupyter_contrib_nbextensions or use the 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 \

Then you can create a nb extension by adding a folder 'default_cells' to the path where the nb extensions are installed. For example on Ubuntu it's /usr/local/share/jupyter/nbextensions/., maybe on windows : C:\Users\xxx.xxx\AppData\Roaming\jupyter\nbextensions\

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

You have to create 3 files in this folder:

  • main.js,其中包含扩展名的js代码
  • Jupyter中API的default_cells.yaml描述
  • README.MD是出现在API中的阅读器的常用描述.

我使用了以下代码: https://github.com/jupyter/notebook/issues/1451my main.js是:

I used the code from : https://github.com/jupyter/notebook/issues/1451my main.js is :

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必须采用以下格式:

the .yaml has to be formatted like this :

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

and the 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"中启用默认单元格"扩展名即可.

Then you just have to enable the 'Default cells' extension in the new tab 'nbextensions' which appeared in Jupyter.

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

The only issue is that it detects if the notebook is new, by looking at the number of cells in the notebook. But if you wrote all your code in one cell, it will detect it as a new notebook and still add the default cells.

这篇关于默认情况下在Jupyter笔记本电脑中配置第一个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆