2015-02-06 152 views
9

我想在我創建的每個新筆記本中的幾乎每個新筆記本中都帶上幾個單元格。如何在iPython筆記本中設置默認啓動命令?

例如,當我創建一個新的筆記本,應該把

%matplotlib inline 
import matplotlib.pyplot as plt 

在默認情況下,細胞但不執行它。 我怎麼能設置這樣的東西呢?

回答

2

定義設置默認啓動的命令,你需要添加的命令在templete ipy_user_conf.py文件在您〜/ .ipython目錄。
此模塊在IPython啓動期間導入。所以,你可以很容易做到:進口模塊,配置擴展,更改選項,定義神奇的命令,把變量和函數的命名空間的IPython等
這裏是樣品ipy_user_conf.py:

# Most of your config files and extensions will probably start 
# with this import 

import IPython.ipapi 
ip = IPython.ipapi.get() 

# You probably want to uncomment this if you did %upgrade -nolegacy 
# import ipy_defaults 

import os 

def main(): 

    #ip.dbg.debugmode = True 
    ip.dbg.debug_stack() 

    # uncomment if you want to get ipython -p sh behaviour 
    # without having to use command line switches 
    import ipy_profile_sh 
    import jobctrl 

    # Configure your favourite editor? 
    # Good idea e.g. for %edit os.path.isfile 

    #import ipy_editors 

    # Choose one of these: 

    #ipy_editors.scite() 
    #ipy_editors.scite('c:/opt/scite/scite.exe') 
    #ipy_editors.komodo() 
    #ipy_editors.idle() 
    # ... or many others, try 'ipy_editors??' after import to see them 

    # Or roll your own: 
    #ipy_editors.install_editor("c:/opt/jed +$line $file") 


    o = ip.options 
    # An example on how to set options 
    #o.autocall = 1 
    o.system_verbose = 0 

    #import_all("os sys") 
    #execf('~/_ipython/ns.py') 


    # -- prompt 
    # A different, more compact set of prompts from the default ones, that 
    # always show your current location in the filesystem: 

    #o.prompt_in1 = r'\C_LightBlue[\C_LightCyan\Y2\C_LightBlue]\C_Normal\n\C_Green|\#>' 
    #o.prompt_in2 = r'.\D: ' 
    #o.prompt_out = r'[\#] ' 

    # Try one of these color settings if you can't read the text easily 
    # autoexec is a list of IPython commands to execute on startup 
    #o.autoexec.append('%colors LightBG') 
    #o.autoexec.append('%colors NoColor') 
    o.autoexec.append('%colors Linux') 


# some config helper functions you can use 
def import_all(modules): 
    """ Usage: import_all("os sys") """ 
    for m in modules.split(): 
     ip.ex("from %s import *" % m) 

def execf(fname): 
    """ Execute a file in user namespace """ 
    ip.ex('execfile("%s")' % os.path.expanduser(fname)) 

main() 

有關詳細信息,請參考鏈接:Customization of IPython

我希望這是你想知道的。

+1

謝謝。雖然你的答案似乎並不完全符合我的要求。鏈接的文檔是針對ipython的一個非常舊的版本,並解釋瞭如何設置啓動ipython時執行的命令。然而,我正在尋找的是爲每個新創建的筆記本添加自定義初始化單元的方法。這看起來可能相當,但是,例如,如果您想共享筆記本,則不是。 – user9886 2015-02-25 11:27:40

相關問題