Configure a first cell by default in Jupyter notebooks

后端 未结 6 567
再見小時候
再見小時候 2021-02-02 01:03

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.

6条回答
  •  说谎
    说谎 (楼主)
    2021-02-02 01:46

    Go there:

    ~/.ipython/profile_default/startup/
    

    You can read the README:

    This is the IPython startup directory

    .py and .ipy files in this directory will be run prior to any code or files specified via the exec_lines or exec_files configurables whenever you load this profile.

    Files will be run in lexicographical order, so you can control the execution order of files with a prefix, e.g.::

    00-first.py
    50-middle.py
    99-last.ipy
    

    So you just need to create a file there like 00_imports.py which contains:

    import numpy as np
    

    if you want to add stuff like %matplotlib inline use .ipy, which you can use directly as well.

    Alternatively, there seems to exist another solution with notebook extension, but I don't know how it works, see here for the github issue of the topic: https://github.com/jupyter/notebook/issues/640

    HTH

提交回复
热议问题