Пример #1
0
    def __init__(self, doc, nbk, logid, *args, **kwds):
        self.doc = doc
        self.notebook = nbk
        self.log = nbk.get_log(logid)
        self.logid = logid  #this is also self.log.id
        self.last = False
        self._lastcell = None  #used by self.lastcell

        #set up wrapper to use for long output
        self.wrapper = textwrap.TextWrapper()

        #Here I will sort the cells, according to their numbers
        #self.log.element[:] = sorted(self.log, key = lambda x:int(x.attrib['number']))
        #XXX modifies the xml element(tree) that nb Log wraps so it may be bad
        #then again, as log.cells is a list (array),
        #as far as i can see the elements are always sorted already
        #(so that sorting never does anything in the current impl, right?)

        #Set up plotting
        self.plot_api = backend.PlotLibraryInterface(self.filename_iter())
        #Set up the interpreter
        #For now we will keep our own excepthook
        self.stdin_orig = sys.stdin
        self.stdout_orig = sys.stdout
        self.stderr_orig = sys.stderr
        self.excepthook_orig = sys.excepthook
        import __builtin__
        __builtin__.close = __builtin__.exit = __builtin__.quit = \
                   'Click on the close button to leave the application.'

        user_ns =  {'__name__'     :'__main__',\
                    '__builtins__' : __builtin__,\
                    '__app':self.doc.app
                    }
        user_ns['grab_figure'] = self.grab_figure

        #I'll keep that for now, if we decide to go with initialization in profiles
        #        # XXX Hack to preload matptlotlib.  This will be moved out once we
        #        # have restored ipython's profile support
        #
        #        mplstart = """
        #from pylab import *
        #switch_backend('WXAgg')
        #ion()
        #"""
        #        exec mplstart in user_ns
        excepthook_orig = sys.excepthook
        self.interp = Shell.IPShellGUI(argv=['-colors', 'NoColor'],
                                       user_ns=user_ns)

        self.interp_log = self.interp.IP.log

        def donothing(*args, **kwds):
            pass

        self.interp.IP.log = donothing
        del self.interp.IP.input_hist[0]

        self.excepthook_IP = sys.excepthook
        sys.excepthook = excepthook_orig
        #Set up the number 0 cell. It is used for code which is not supposed to
        #be edited. And it has to be hidden
        #etree.dump(self.log) #dbg
        if not self.log:
            #This is a new log
            self.Append(input="""  
############DO NOT EDIT THIS CELL############  
from pylab import *  
switch_backend('WXAgg')  
ion()  
""",
                        number=0)
            self.Run()
        elif self.log[0] is not None:
            self.__run(self.Get(0))

        #Append the empty element at the end
        self.SetLastInput()

        del __builtin__