2015-02-06 4 views
9

Ich möchte ein Paar Zellen mit Befehlen, die ich brauche, in fast jedem neuen Notizbuch in jedes neue Notizbuch einfügen, das ich erstelle.Wie kann ich Standardstartbefehle in iPython-Notizbüchern einrichten?

Zum Beispiel, wenn ich eine neue Notebook sollte es ein standardmäßig

%matplotlib inline 
import matplotlib.pyplot as plt 

in einer Zelle

setzt aber nicht ausführen. Wie könnte ich so etwas einstellen?

Antwort

2

auf Standard-Startsatz von Befehlen zu definieren, müssen Sie die Befehle in der templete in Ihrem ~/.ipython Verzeichnis ipy_user_conf.py Datei hinzuzufügen.
Dieses Modul wird während des IPython-Starts importiert. So können Sie ganz einfach tun: Import-Module konfigurieren Erweiterungen, Optionen ändern, definiert Magie Befehle, setzt Variablen und Funktionen im IPython Namespace usw.
Hier wird die Probe 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() 

Weitere Informationen Bitte beachten Sie den Link: Customization of IPython.

Ich hoffe, das ist, was Sie wissen wollten.

+1

Danke. Ihre Antwort scheint jedoch nicht ganz das zu sein, wonach ich gesucht habe. Die verlinkte Dokumentation bezieht sich auf eine sehr alte Version von ipython und erklärt, wie man Befehle einrichtet, die beim Start von ipython ausgeführt werden. Was ich jedoch suche, ist eine Möglichkeit, jedem neu erstellten Notebook eine eigene Initialisierungszelle hinzuzufügen. Dies mag Ihnen ähnlich erscheinen, aber nicht, wenn Sie beispielsweise Notizbücher teilen möchten. – user9886

Verwandte Themen