Configure a first cell by default in Jupyter notebooks

后端 未结 6 662
再見小時候
再見小時候 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:53

    Another half-solution: keep the default code in a file, and manually type and execute a %load command in your first cell.

    I keep my standard imports in firstcell.py:

    %reload_ext autoreload
    %autoreload 2
    
    import numpy as np
    import pandas as pd
    ... 
    

    Then in each new notebook, I type and run %load firstcell.py in the first cell, and jupyter changes the first cell contents to

    # %load firstcell.py
    %reload_ext autoreload
    %autoreload 2
    
    import numpy as np
    import pandas as pd
    
    import matplotlib.pyplot as plt
    import seaborn as sns
    %matplotlib inline
    

    If you really just want a single import statement, this doesn't get you anything, but if you have several you always want to use, this might help.

提交回复
热议问题