def start(self): print("[4] inside IPythonKernel start") if self._cutter_ipython_qtimer is not None: raise Exception("IPython kernel is already running.") # The IPKernelApp initialization is based on the IPython source for # IPython.embed_kernel available here: # https://github.com/ipython/ipython/blob/rel-3.2.1/IPython/kernel/zmq/embed.py if IPKernelApp.initialized(): print("[5] inside IPythonKernel check IPKernelApp.initialized") app = IPKernelApp.instance() else: print("[6] inside IPythonKernel IPKernelApp not initialized") app = IPKernelApp.instance( outstream_class='ipycutter.kernel.CutterTeeOutStream') app.initialize() print("[7] inside IPythonKernel IPKernelApp after initialize") main = app.kernel.shell._orig_sys_modules_main_mod if main is not None: sys.modules[ app.kernel.shell._orig_sys_modules_main_name] = main # IPython <= 3.2.x will send exception to sys.__stderr__ instead of # sys.stderr. IDA's console will not be able to display exceptions if we # don't send it to IDA's sys.stderr. To fix this, we call both the # ipython's and IDA's excepthook (IDA's excepthook is actually Python's # default). sys.excepthook = wrap_excepthook(sys.excepthook) print( "[8] inside IPythonKernel IPKernelApp after if-else for initializing" ) app.shell.set_completer_frame() app.kernel.start() app.kernel.do_one_iteration() self.connection_file = app.connection_file # Schedule the IPython kernel to run on the Qt main loop with a QTimer qtimer = QtCore.QTimer() # Use _poll_interval as docuementented here: # https://ipython.org/ipython-doc/dev/config/eventloops.html qtimer.setInterval(int(1000 * app.kernel._poll_interval)) qtimer.timeout.connect(app.kernel.do_one_iteration) qtimer.start() # We keep tht qtimer in a global variable to this module to allow to # manually stop the kernel later with stop_ipython_kernel. # There's a second purpose: If there is no more reference to the QTimer, # it will get garbage collected and the timer will stop calling # kernel.do_one_iteration. Keep this in mind before removing this line. self._cutter_ipython_qtimer = qtimer
def start(self): if self._timer is not None: raise Exception("IPython kernel is already running.") # The IPKernelApp initialization is based on the IPython source for # IPython.embed_kernel available here: # https://github.com/ipython/ipython/blob/rel-3.2.1/IPython/kernel/zmq/embed.py if IPKernelApp.initialized(): app = IPKernelApp.instance() else: app = IPKernelApp.instance(outstream_class="ipyida.kernel.IDATeeOutStream") app.initialize() main = app.kernel.shell._orig_sys_modules_main_mod if main is not None: sys.modules[app.kernel.shell._orig_sys_modules_main_name] = main # IPython <= 3.2.x will send exception to sys.__stderr__ instead of # sys.stderr. IDA's console will not be able to display exceptions if we # don't send it to IDA's sys.stderr. To fix this, we call both the # ipython's and IDA's excepthook (IDA's excepthook is actually Python's # default). sys.excepthook = wrap_excepthook(sys.excepthook) # Load the calling scope (ida_module, ida_locals) = IPython.utils.frame.extract_module_locals(1) if "idaapi" not in ida_locals: raise Exception( "{0:s} must be called from idapythonrc.py or " "IDA's prompt.".format("IPythonKernel.start") ) app.kernel.user_module = ida_module app.kernel.user_ns = ida_locals app.shell.set_completer_frame() app.kernel.start() app.kernel.do_one_iteration() self.connection_file = app.connection_file basename = os.path.basename(self.connection_file) print( "[IPyIDA] Connect with another client using --existing {}".format(basename) ) def ipython_kernel_iteration(): app.kernel.do_one_iteration() return int(1000 * app.kernel._poll_interval) self._timer = idaapi.register_timer( int(1000 * app.kernel._poll_interval), ipython_kernel_iteration )
def embed_kernel(module=None, local_ns=None, **kwargs): """Embed and start an IPython kernel in a given scope. Parameters ---------- module : ModuleType, optional The module to load into IPython globals (default: caller) local_ns : dict, optional The namespace to load into IPython user namespace (default: caller) kwargs : various, optional Further keyword args are relayed to the IPKernelApp constructor, allowing configuration of the Kernel. Will only have an effect on the first embed_kernel call for a given process. """ # get the app if it exists, or set it up if it doesn't if IPKernelApp.initialized(): app = IPKernelApp.instance() else: print "app" app = IPKernelApp.instance(**kwargs) #app.initialize([]) # Undo unnecessary sys module mangling from init_sys_modules. # This would not be necessary if we could prevent it # in the first place by using a different InteractiveShell # subclass, as in the regular embed case. # load the calling scope if not given (caller_module, caller_locals) = extract_module_locals(1) if module is None: module = caller_module if local_ns is None: local_ns = caller_locals print "eventloops.enable_gui" #ipykernel.eventloops.enable_gui('slicer', kernel=app.kernel) loop_slicer(app.kernel) print "initi" app.initialize(['python', '--gui=slicer']) app.kernel.user_module = module app.kernel.user_ns = local_ns app.shell.set_completer_frame() #loop_slicer(app.kernel) main = app.kernel.shell._orig_sys_modules_main_mod if main is not None: sys.modules[app.kernel.shell._orig_sys_modules_main_name] = main print "app.start" app.start() return app
def server_factory(network_manager_factory, verbosity, **kwargs): init_qt_app(verbose=verbosity >= 5) SplashKernel.network_manager_factory = network_manager_factory kernel = IPKernelApp.instance(kernel_class=SplashKernel) kernel.initialize() kernel.kernel.eventloop = loop_qt5 kernel.start()
def pylab_kernel(self, gui): '''Launch an IPython kernel with pylab support for the gui.''' trace = False # Forces Leo's --debug option. self.kernelApp = kernelApp = IPKernelApp.instance() # IPKernalApp is a singleton class. # Return the singleton instance, creating it if necessary. if kernelApp: # pylab is needed for Qt event loop integration. args = ['python', '--pylab=%s' % (gui)] if trace or g.app.debug: args.append('--log-level=20') # Higher is *quieter* # args.append('--debug') # Produces a verbose IPython log. #'--log-level=10' # '--pdb', # User-level debugging try: kernelApp.initialize(args) except Exception: sys.stdout = sys.__stdout__ print('kernelApp.initialize failed!') raise else: g.trace('IPKernelApp.instance failed!') return kernelApp
def main(): # Neptune.launch_instance() from ipykernel.kernelapp import IPKernelApp app = IPKernelApp.instance() app.init_pdb() app.init_blackhole() app.init_connection_file() app.init_poller() app.init_sockets() app.init_heartbeat() # writing/displaying connection info must be *after* init_sockets/heartbeat app.write_connection_file() # Log connection info after writing connection file, so that the connection # file is definitely available at the time someone reads the log. app.log_connection_info() app.init_io() try: app.init_signal() except: # Catch exception when initializing signal fails, eg when running the # kernel on a separate thread if app.log_level < logging.CRITICAL: app.log.error("Unable to initialize signal:", exc_info=True) app.init_kernel() print(app.kernel)
def nbmain(argv): """Notebook entry point used when FSLeyes is running as a frozen application. Used to start notebook server and kernel processes. """ if not fslplatform.frozen: raise RuntimeError('nbmain can only be used in ' 'frozen versions of FSLeyes') if argv[0] != 'notebook': raise RuntimeError('argv does not look like nbmain arguments ' '(first arg is not \'notebook\')') argv = argv[1:] # run the notebook server if argv[0] == 'server': from notebook.notebookapp import main # second argument is a path # to add to the PYTHONPATH. # See NotebookServer.run. sys.path.insert(0, argv[1]) return main(argv=[]) # run a kernel (in place of ipykernel_launcher} elif argv[0] == 'kernel': from ipykernel.kernelapp import IPKernelApp app = IPKernelApp.instance() app.initialize(argv[1:]) return app.start()
def MainLoopDebug(self): """Alternative mainloop to embed Jupyter kernel""" from ipykernel.kernelapp import IPKernelApp kernel = IPKernelApp.instance() kernel.initialize(['pythonw', '--matplotlib=wx']) # hack to ease debugging hack_wx() # expose vars ns = kernel.shell.user_ns ns['kernel'] = kernel ns['app'] = self ns['md'] = self.ctl.model ns['pp'] = self.top.pa_plot # invoke jupyter client # # FIXME: # - JupyterQtConsoleApp.launch_instance(argv) doesn't work for some reason. # - Need more clean/portable way to start client in background, esp. on Windows. # -- Windows has bizzare support of fork-exec multiprocessing on Python cfg = kernel.abs_connection_file.replace(os.path.sep, "/") cmd = "jupyter-qtconsole --existing %s" % cfg os.system("sh -c \"%s &\"" % cmd) # start kernel and GUI event loop kernel.start()
def setup_ipython(self): """Monkey patch shell's error handler. This method is to monkey-patch the showtraceback method of IPython's InteractiveShell to __IPYTHON__ is not detected when starting an IPython kernel, so this method is called from start_kernel in spyder-modelx. """ if self.is_ipysetup: return from ipykernel.kernelapp import IPKernelApp self.shell = IPKernelApp.instance().shell # None in PyCharm console if not self.shell and is_ipython(): self.shell = get_ipython() if self.shell: shell_class = type(self.shell) shell_class.default_showtraceback = shell_class.showtraceback shell_class.showtraceback = custom_showtraceback self.is_ipysetup = True else: raise RuntimeError("IPython shell not found.")
def pylab_kernel(self, gui): '''Launch an IPython kernel with pylab support for the gui.''' trace = False and not g.unitTesting # Increased logging. self.kernelApp = kernelApp = IPKernelApp.instance() # IPKernalApp is a singleton class. # Return the singleton instance, creating it if necessary. if kernelApp: # --pylab is no longer needed to create a qt console. # --pylab=qt now generates: # RuntimeError: Cannot activate multiple GUI eventloops # GUI event loop or pylab initialization failed args = ['python', '--pylab'] # Fails # args = ['python', '--pylab=%s' % (gui)] if trace: args.append('--log-level=20') # Higher is *quieter* # args.append('--debug') # Produces a verbose IPython log. #'--log-level=10' # '--pdb', # User-level debugging try: # self.pdb() kernelApp.initialize(args) except Exception: sys.stdout = sys.__stdout__ print('kernelApp.initialize failed!') raise else: g.trace('IPKernelApp.instance failed!') return kernelApp
def do_one_iteration(): """Perform an iteration on IPython kernel runloop""" if IPKernelApp.initialized(): app = IPKernelApp.instance() app.kernel.do_one_iteration() else: raise Exception("Kernel is not initialized")
def launch(app, MW=None): if MW is None: from manuskript.functions import mainWindow MW = mainWindow() MW.show() # Support for IPython Jupyter QT Console as a debugging aid. # Last argument must be --console to enable it # Code reference : # https://github.com/ipython/ipykernel/blob/master/examples/embedding/ipkernel_qtapp.py # https://github.com/ipython/ipykernel/blob/master/examples/embedding/internal_ipkernel.py if len(sys.argv) > 1 and sys.argv[-1] == "--console": try: from IPython.lib.kernel import connect_qtconsole from ipykernel.kernelapp import IPKernelApp # Only to ensure matplotlib QT mainloop integration is available import matplotlib # Create IPython kernel within our application kernel = IPKernelApp.instance() # Initialize it and use matplotlib for main event loop integration with QT kernel.initialize(['python', '--matplotlib=qt']) # Create the console in a new process and connect console = connect_qtconsole(kernel.abs_connection_file, profile=kernel.profile) # Export MW and app variable to the console's namespace kernel.shell.user_ns['MW'] = MW kernel.shell.user_ns['app'] = app kernel.shell.user_ns['kernel'] = kernel kernel.shell.user_ns['console'] = console # When we close manuskript, make sure we close the console process and stop the # IPython kernel's mainloop, otherwise the app will never finish. def console_cleanup(): app.quit() console.kill() kernel.io_loop.stop() app.lastWindowClosed.connect(console_cleanup) # Very important, IPython-specific step: this gets GUI event loop # integration going, and it replaces calling app.exec_() kernel.start() except Exception as e: print( "Console mode requested but error initializing IPython : %s" % str(e)) print( "To make use of the Interactive IPython QT Console, make sure you install : " ) print("$ pip3 install ipython qtconsole matplotlib") qApp.exec_() else: qApp.exec_() qApp.deleteLater()
def __init__(self, create_kernel=False, run_in_jupyter=0): self.run_in_jupyter = run_in_jupyter self.app = QtCore.QCoreApplication.instance() if self.app is None: self.app = QtWidgets.QApplication([]) self.app.setQuitOnLastWindowClosed(True) self.app.aboutToQuit.connect(self.about_to_quit) #QtCore.QObject.connect(self.app, Qt.SIGNAL("lastWindowClosed()"), # self.app, Qt.SLOT("quit()")) self.ipkernel = None if create_kernel: self.ipkernel = IPKernelApp.instance() self.ipkernel.initialize(['python', '--matplotlib=qt']) glog.info('stuff >> %s', self.ipkernel.connection_file) self.cleanups_cb = [] self.win = OpaMainWindow(self.close_main_window) self.win.show() #ui.graphicsView.useOpenGL() ## buggy, but you can try it if you need extra speed. #self.vb = ViewBox2() #self.vb.setAspectLocked() self.manager = PlotManager(self.win, self.win.ui.verticalLayout)
def start(self): if self._timer is not None: raise Exception("IPython kernel is already running.") # The IPKernelApp initialization is based on the IPython source for # IPython.embed_kernel available here: # https://github.com/ipython/ipython/blob/rel-3.2.1/IPython/kernel/zmq/embed.py if IPKernelApp.initialized(): app = IPKernelApp.instance() else: # Load IPyIDA's user init file into the user namespace if it exists. if os.path.exists(IPYIDARC_PATH): IPKernelApp.exec_files = [IPYIDARC_PATH] app = IPKernelApp.instance( outstream_class='ipyida.kernel.IDATeeOutStream') app.initialize() main = app.kernel.shell._orig_sys_modules_main_mod if main is not None: sys.modules[ app.kernel.shell._orig_sys_modules_main_name] = main # IPython <= 3.2.x will send exception to sys.__stderr__ instead of # sys.stderr. IDA's console will not be able to display exceptions if we # don't send it to IDA's sys.stderr. To fix this, we call both the # ipython's and IDA's excepthook (IDA's excepthook is actually Python's # default). sys.excepthook = wrap_excepthook(sys.excepthook) app.shell.set_completer_frame() app.kernel.start() self.connection_file = app.connection_file if not is_using_ipykernel_5(): app.kernel.do_one_iteration() def ipython_kernel_iteration(): app.kernel.do_one_iteration() return int(1000 * app.kernel._poll_interval) self._timer = idaapi.register_timer( int(1000 * app.kernel._poll_interval), ipython_kernel_iteration)
def start_ipython(ip=None, ns=None, log=None): """Start an IPython kernel in a thread Parameters ---------- ip : str The IP address to listen on (likely the parent object's ip). ns : dict Any names that should be injected into the IPython namespace. log : logger instance Hook up IPython's logging to an existing logger instead of the default. """ from IPython import get_ipython if get_ipython() is not None: raise RuntimeError("Cannot start IPython, it's already running.") from ipykernel.kernelapp import IPKernelApp # start IPython, disabling its signal handlers that won't work due to running in a thread: app = IPKernelApp.instance(log=log) # Don't connect to the history database app.config.HistoryManager.hist_file = ":memory:" # listen on all interfaces, so remote clients can connect: if ip: app.ip = ip # disable some signal handling, logging def noop(): return None app.init_signal = noop app.log_connection_info = noop # start IPython in a thread # initialization happens in the thread to avoid threading problems # with the sqlite history evt = Event() def _start(): app.initialize([]) app.kernel.pre_handler_hook = noop app.kernel.post_handler_hook = noop app.kernel.start() # save self in the IPython namespace as 'worker' # inject things into the IPython namespace if ns: app.kernel.shell.user_ns.update(ns) evt.set() # start the app's IOLoop in its thread IOLoop.current().start() zmq_loop_thread = Thread(target=_start) zmq_loop_thread.daemon = True zmq_loop_thread.start() assert evt.wait( timeout=5), "IPython didn't start in a reasonable amount of time." return app
def run(self): asyncio.set_event_loop(asyncio.new_event_loop()) app = IPKernelApp.instance() app.initialize() ip = get_ipython() load_ipython_extension(ip) self.connection_file = app.connection_file app.start()
def start(): with xvfb.autostart(): # FIXME: logs go to nowhere init_qt_app(verbose=False) kernel = IPKernelApp.instance(kernel_class=SplashKernel) kernel.initialize() kernel.kernel.eventloop = loop_qt5 kernel.start()
def embed_kernel(module=None, local_ns=None, **kwargs): """Embed and start an IPython kernel in a given scope. Parameters ---------- module : ModuleType, optional The module to load into IPython globals (default: caller) local_ns : dict, optional The namespace to load into IPython user namespace (default: caller) kwargs : various, optional Further keyword args are relayed to the IPKernelApp constructor, allowing configuration of the Kernel. Will only have an effect on the first embed_kernel call for a given process. """ # get the app if it exists, or set it up if it doesn't if IPKernelApp.initialized(): app = IPKernelApp.instance() else: app = IPKernelApp.instance(**kwargs) app.initialize(sys.argv) # Undo unnecessary sys module mangling from init_sys_modules. # This would not be necessary if we could prevent it # in the first place by using a different InteractiveShell # subclass, as in the regular embed case. main = app.kernel.shell._orig_sys_modules_main_mod if main is not None: sys.modules[app.kernel.shell._orig_sys_modules_main_name] = main # load the calling scope if not given (caller_module, caller_locals) = extract_module_locals(1) if module is None: module = caller_module if local_ns is None: local_ns = caller_locals app.kernel.user_module = None app.kernel.user_ns = None app.shell.set_completer_frame() if app.poller is not None: app.poller.start() app.kernel.start() return app
def start(): with xvfb.autostart(): # FIXME: logs go to nowhere init_qt_app(verbose=False) kernel = IPKernelApp.instance(kernel_class=SplashKernel) kernel.initialize() kernel.kernel.eventloop = loop_qt4 kernel.start()
def mpl_kernel(gui='qt'): """ Launch and return an IPython kernel with matplotlib support for the desired gui """ kernel = IPKernelApp.instance() kernel.initialize(['python', '--matplotlib=%s' % gui]) # kernel.initialize(['python']) return kernel
def _start_kernel(): """starts the ipython kernel and returns the ipython app""" global _ipython_app, _kernel_running if _ipython_app and _kernel_running: return _ipython_app import IPython from ipykernel.kernelapp import IPKernelApp from zmq.eventloop import ioloop # patch IPKernelApp.start so that it doesn't block def _IPKernelApp_start(self): if self.poller is not None: self.poller.start() self.kernel.start() # set up a timer to periodically poll the zmq ioloop loop = ioloop.IOLoop.instance() def poll_ioloop(timer_id, time): global _kernel_running # if the kernel has been closed then run the event loop until it gets to the # stop event added by IPKernelApp.shutdown_request if self.kernel.shell.exit_now: _log.debug("IPython kernel stopping (%s)" % self.connection_file) timer.kill_timer(timer_id) loop.start() _kernel_running = False return # otherwise call the event loop but stop immediately if there are no pending events loop.add_timeout(0, lambda: loop.add_callback(loop.stop)) loop.start() global _kernel_running _kernel_running = True timer.set_timer(100, poll_ioloop) IPKernelApp.start = _IPKernelApp_start # IPython expects sys.__stdout__ to be set sys.__stdout__ = sys.stdout sys.__stderr__ = sys.stderr # call the API embed function, which will use the monkey-patched method above IPython.embed_kernel() _ipython_app = IPKernelApp.instance() # patch ipapp so anything else trying to get a terminal app (e.g. ipdb) # gets our IPKernalApp. from IPython.terminal.ipapp import TerminalIPythonApp TerminalIPythonApp.instance = lambda: _ipython_app __builtins__["get_ipython"] = lambda: _ipython_app.shell return _ipython_app
def test(self): from ipykernel.connect import connect_qtconsole from ipykernel.kernelapp import IPKernelApp kernelApp = IPKernelApp.instance() args = ['python', '--pylab=qt', '--log-level=20'] kernelApp.initialize(args) connect_qtconsole()
def do_one_iteration(): """Perform an iteration on IPython kernel runloop""" if is_using_ipykernel_5(): raise Exception("Should not call this when ipykernel >= 5") if IPKernelApp.initialized(): app = IPKernelApp.instance() app.kernel.do_one_iteration() else: raise Exception("Kernel is not initialized")
def fork_kernel(self, config, pipe, resource_limits): """ A function to be set as the target for the new kernel processes forked in ForkingKernelManager.start_kernel. This method forks and initializes a new kernel, uses the update_function to update the kernel's namespace, sets resource limits for the kernel, and sends kernel connection information through the Pipe object. :arg traitlets.config.loader config: kernel configuration :arg multiprocessing.Pipe pipe: a multiprocessing connection object which will send kernel ip, session, and port information to the other side :arg dict resource_limits: a dict with keys resource.RLIMIT_* (see config_default documentation for explanation of valid options) and values of the limit for the given resource to be set in the kernel process """ os.setpgrp() logger = kernel_logger.getChild(str(uuid.uuid4())[:4]) logger.debug("kernel forked; now starting and configuring") try: ka = IPKernelApp.instance(config=config, ip=config["ip"]) ka.log.propagate = True ka.log_level = logger.level from namespace import InstrumentedNamespace ka.user_ns = InstrumentedNamespace() ka.initialize([]) except: logger.exception("Error initializing IPython kernel") # FIXME: What's the point in proceeding after?! try: if self.update_function is not None: self.update_function(ka) except: logger.exception("Error configuring up kernel") logger.debug("finished updating") for r, limit in resource_limits.iteritems(): resource.setrlimit(getattr(resource, r), (limit, limit)) pipe.send({"ip": ka.ip, "key": ka.session.key, "shell_port": ka.shell_port, "stdin_port": ka.stdin_port, "hb_port": ka.hb_port, "iopub_port": ka.iopub_port}) pipe.close() # The following line will erase JSON connection file with ports and # other numbers. Since we do not reuse the kernels, we don't really need # these files. And new kernels set atexit hook to delete the file, but # it does not get called, perhaps because kernels are stopped by system # signals. The result is accumulation of files leading to disk quota # issues AND attempts to use stale files to connect to non-existing # kernels that eventually crash the server. TODO: figure out a better # fix, perhaps kernels have to be stopped in a more gentle fashion? ka.cleanup_connection_file() # In order to show the correct code line when a (deprecation) warning # is triggered, we change the main module name and save user code to # a file with the same name. import sys sys.argv = ['sagemathcell.py'] old_execute = ka.kernel.do_execute import codecs def new_execute(code, *args, **kwds): with codecs.open('sagemathcell.py', 'w', encoding='utf-8') as f: f.write(code) return old_execute(code, *args, **kwds) ka.kernel.do_execute = new_execute ka.start()
def mpl_kernel(gui): """Launch and return an IPython kernel with matplotlib support for the desired gui""" kernel = IPKernelApp.instance() kernel.initialize( [ "python", "--matplotlib=%s" % gui, #'--log-level=10' # noqa ] ) return kernel
def _start_kernel(): """starts the ipython kernel and returns the ipython app""" if sys._ipython_app and sys._ipython_kernel_running: return sys._ipython_app import IPython from ipykernel.kernelapp import IPKernelApp from zmq.eventloop import ioloop # patch IPKernelApp.start so that it doesn't block def _IPKernelApp_start(self): if self.poller is not None: self.poller.start() self.kernel.start() # set up a timer to periodically poll the zmq ioloop loop = ioloop.IOLoop.instance() def poll_ioloop(timer_id, time): # if the kernel has been closed then run the event loop until it gets to the # stop event added by IPKernelApp.shutdown_request if self.kernel.shell.exit_now: _log.debug("IPython kernel stopping (%s)" % self.connection_file) timer.kill_timer(timer_id) loop.start() sys._ipython_kernel_running = False return # otherwise call the event loop but stop immediately if there are no pending events loop.add_timeout(0, lambda: loop.add_callback(loop.stop)) loop.start() sys._ipython_kernel_running = True timer.set_timer(100, poll_ioloop) IPKernelApp.start = _IPKernelApp_start # IPython expects sys.__stdout__ to be set sys.__stdout__ = sys.stdout sys.__stderr__ = sys.stderr # call the API embed function, which will use the monkey-patched method above IPython.embed_kernel() sys._ipython_app = IPKernelApp.instance() # patch ipapp so anything else trying to get a terminal app (e.g. ipdb) # gets our IPKernalApp. from IPython.terminal.ipapp import TerminalIPythonApp TerminalIPythonApp.instance = lambda: sys._ipython_app __builtins__["get_ipython"] = lambda: sys._ipython_app.shell return sys._ipython_app
def launch(app, MW = None): if MW is None: from manuskript.functions import mainWindow MW = mainWindow() MW.show() # Support for IPython Jupyter QT Console as a debugging aid. # Last argument must be --console to enable it # Code reference : # https://github.com/ipython/ipykernel/blob/master/examples/embedding/ipkernel_qtapp.py # https://github.com/ipython/ipykernel/blob/master/examples/embedding/internal_ipkernel.py if len(sys.argv) > 1 and sys.argv[-1] == "--console": try: from IPython.lib.kernel import connect_qtconsole from ipykernel.kernelapp import IPKernelApp # Only to ensure matplotlib QT mainloop integration is available import matplotlib # Create IPython kernel within our application kernel = IPKernelApp.instance() # Initialize it and use matplotlib for main event loop integration with QT kernel.initialize(['python', '--matplotlib=qt']) # Create the console in a new process and connect console = connect_qtconsole(kernel.abs_connection_file, profile=kernel.profile) # Export MW and app variable to the console's namespace kernel.shell.user_ns['MW'] = MW kernel.shell.user_ns['app'] = app kernel.shell.user_ns['kernel'] = kernel kernel.shell.user_ns['console'] = console # When we close manuskript, make sure we close the console process and stop the # IPython kernel's mainloop, otherwise the app will never finish. def console_cleanup(): app.quit() console.kill() kernel.io_loop.stop() app.lastWindowClosed.connect(console_cleanup) # Very important, IPython-specific step: this gets GUI event loop # integration going, and it replaces calling app.exec_() kernel.start() except Exception as e: print("Console mode requested but error initializing IPython : %s" % str(e)) print("To make use of the Interactive IPython QT Console, make sure you install : ") print("$ pip3 install ipython qtconsole matplotlib") qApp.exec_() else: qApp.exec_() qApp.deleteLater()
def execute(self, context): wm = context.window_manager self._timer = wm.event_timer_add(0.016, window=context.window) wm.modal_handler_add(self) if not JupyterKernelLoop.kernelApp: JupyterKernelLoop.kernelApp = IPKernelApp.instance() JupyterKernelLoop.kernelApp.initialize(['python'] + RUNTIME_CONFIG['args']) JupyterKernelLoop.kernelApp.kernel.start() # doesn't start event loop, kernelApp.start() does return {'RUNNING_MODAL'}
def get_session(): """Return the session id of the owner PyXLL client, or None.""" global _session if _session is not None: return _session app = IPKernelApp.instance() if IPKernelApp else None if app is None: return None user_ns = app.shell.user_ns _session = user_ns.get("__pyxll_notebook_session__") return _session
def run(self): global logger logger = log.kernel_logger.getChild(str(os.getpid())) logger.debug("forked kernel is running") log.std_redirect(logger) # Become a group leader for cleaner exit. os.setpgrp() dir = os.path.join(self.dir, self.id) try: os.mkdir(dir) except OSError as e: if e.errno != errno.EEXIST: raise os.chdir(dir) #config = traitlets.config.loader.Config({"ip": self.ip}) #config.HistoryManager.enabled = False app = IPKernelApp.instance(log=logger) from namespace import InstrumentedNamespace app.user_ns = InstrumentedNamespace() app.initialize([]) # Redirects stdout/stderr #log.std_redirect(logger) # Uncomment for debugging # This function should be called via atexit, but it isn't, perhaps due # to forking. Stale connection files do cause problems. app.cleanup_connection_file() kernel_init.initialize(app.kernel) for r, limit in self.rlimits.iteritems(): resource.setrlimit(getattr(resource, r), (limit, limit)) logger.debug("kernel ready") context = zmq.Context.instance() socket = context.socket(zmq.PUSH) socket.connect("tcp://localhost:{}".format(self.waiter_port)) socket.send_json({ "id": self.id, "connection": { "key": app.session.key, "ip": app.ip, "hb": app.hb_port, "iopub": app.iopub_port, "shell": app.shell_port, }, "rlimits": self.rlimits, }) def signal_handler(signum, frame): logger.info("received %s, shutting down", signum) # TODO: this may not be the best way to do it. app.kernel.do_shutdown(False) signal.signal(signal.SIGTERM, signal_handler) app.start() logger.debug("Kernel.run finished")
def start(self): if self._timer is not None: raise Exception("IPython kernel is already running.") # The IPKernelApp initialization is based on the IPython source for # IPython.embed_kernel available here: # https://github.com/ipython/ipython/blob/rel-3.2.1/IPython/kernel/zmq/embed.py if IPKernelApp.initialized(): app = IPKernelApp.instance() else: app = IPKernelApp.instance( outstream_class='ipyida.kernel.IDATeeOutStream' ) app.initialize() main = app.kernel.shell._orig_sys_modules_main_mod if main is not None: sys.modules[app.kernel.shell._orig_sys_modules_main_name] = main # IPython <= 3.2.x will send exception to sys.__stderr__ instead of # sys.stderr. IDA's console will not be able to display exceptions if we # don't send it to IDA's sys.stderr. To fix this, we call both the # ipython's and IDA's excepthook (IDA's excepthook is actually Python's # default). sys.excepthook = wrap_excepthook(sys.excepthook) app.shell.set_completer_frame() app.kernel.start() app.kernel.do_one_iteration() self.connection_file = app.connection_file def ipython_kernel_iteration(): app.kernel.do_one_iteration() return int(1000 * app.kernel._poll_interval) self._timer = idaapi.register_timer(int(1000 * app.kernel._poll_interval), ipython_kernel_iteration)
def get_connection_file(app=None): """Return the path to the connection file of an app Parameters ---------- app : IPKernelApp instance [optional] If unspecified, the currently running app will be used """ if app is None: from ipykernel.kernelapp import IPKernelApp if not IPKernelApp.initialized(): raise RuntimeError("app not specified, and not in a running Kernel") app = IPKernelApp.instance() return filefind(app.connection_file, ['.', app.connection_dir])
def send_message(session, msg_type, content): """Sends a message back to the client.""" app = IPKernelApp.instance() if IPKernelApp else None if app is None: raise AssertionError("No IPKernelApp found.") if session is None: raise AssertionError("No PyXLL client session found.") parent = {"header": {"session": session}} app.session.send(app.iopub_socket, msg_type, content=content, parent=parent)
def init_kernel(self, backend='qt', log_level='INFO'): _optslist = [ 'python', '--gui={0}'.format(backend), '--log-level={0}'.format(log_level) ] self.kernel = IPKernelApp.instance() self.kernel.initialize(_optslist) # To create and track active qt consoles self.consoles = [] # This application will also act on the shell user namespace self.namespace = self.kernel.shell.user_ns self.add_to_namespace('kernel', self.kernel)
def main(): # Remove this module's path from sys.path: try: sys.path.remove(osp.dirname(__file__)) except ValueError: pass try: locals().pop('__file__') except KeyError: pass __doc__ = '' __name__ = '__main__' # Add current directory to sys.path (like for any standard Python interpreter # executed in interactive mode): sys.path.insert(0, '') # Fire up the kernel instance. from ipykernel.kernelapp import IPKernelApp if not IS_EXT_INTERPRETER: from spyder.utils.ipython.spyder_kernel import SpyderKernel else: # We add "spyder" to sys.path for external interpreters, # so this works! # See create_kernel_spec of plugins/ipythonconsole from utils.ipython.spyder_kernel import SpyderKernel kernel = IPKernelApp.instance() kernel.kernel_class = SpyderKernel try: kernel.config = kernel_config() except: pass kernel.initialize() # Set our own magics kernel.shell.register_magic_function(varexp) # Set Pdb class to be used by %debug and %pdb. # This makes IPython consoles to use the class defined in our # sitecustomize instead of their default one. import pdb kernel.shell.InteractiveTB.debugger_cls = pdb.Pdb # Start the (infinite) kernel event loop. kernel.start()
def _get_default_pickle_protocol(): """Return the highest pickle prootocol supported by both server and client.""" global _default_pickle_protocol if _default_pickle_protocol is not None: return _default_pickle_protocol app = IPKernelApp.instance() if IPKernelApp else None if app is None: _default_pickle_protocol = pickle.HIGHEST_PROTOCOL return _default_pickle_protocol user_ns = app.shell.user_ns pickle_protocol = user_ns.get( "__pyxll_pickle_protocol__") or pickle.HIGHEST_PROTOCOL _default_pickle_protocol = min(pickle_protocol, pickle.HIGHEST_PROTOCOL) return _default_pickle_protocol
def main(): # Remove this module's path from sys.path: try: sys.path.remove(osp.dirname(__file__)) except ValueError: pass try: locals().pop('__file__') except KeyError: pass __doc__ = '' __name__ = '__main__' # Add current directory to sys.path (like for any standard Python interpreter # executed in interactive mode): sys.path.insert(0, '') # Fire up the kernel instance. from ipykernel.kernelapp import IPKernelApp if not IS_EXT_INTERPRETER: from spyder.utils.ipython.spyder_kernel import SpyderKernel else: # We add "spyder" to sys.path for external interpreters, # so this works! # See create_kernel_spec of plugins/ipythonconsole from utils.ipython.spyder_kernel import SpyderKernel kernel = IPKernelApp.instance() kernel.kernel_class = SpyderKernel try: kernel.config = kernel_config() except: pass kernel.initialize() # NOTE: Leave this and other magic modifications *after* setting # __ipythonkernel__ to not have problems while starting kernels kernel.shell.register_magic_function(varexp) # Start the (infinite) kernel event loop. kernel.start()
def gui_kernel(gui_backend): """ Launch and return an IPython kernel GUI with support. Parameters ---------- gui_backend -- string or None The GUI mode used to initialize the GUI mode. For options, see the `ipython --gui` help pages. If None, the kernel is initialized without GUI support. """ kernel = IPKernelApp.instance() argv = ['python'] if gui_backend is not None: argv.append('--gui={}'.format(gui_backend)) kernel.initialize(argv) return kernel
def setup_ipython(): try: ipy = IPKernelApp.instance() # Keep a reference to the kernel even if this module is reloaded sys._ipython_app = ipy sys._ipython_kernel_running = True # patch user_global_ns so that it always references the user_ns dict setattr(ipy.shell.__class__, 'user_global_ns', property(lambda self: self.user_ns)) # patch ipapp so anything else trying to get a terminal app (e.g. ipdb) gets our IPKernalApp. TerminalIPythonApp.instance = lambda: ipy __builtins__["get_ipython"] = lambda: ipy.shell.__class__ # Use the inline matplotlib backend mpl = ipy.shell.find_magic("matplotlib") if mpl: mpl("inline") finally: event.set()
def default_kernel_app(): """ Return a configured IPKernelApp """ def event_loop(kernel): """ Non-blocking qt event loop.""" kernel.timer = QtCore.QTimer() kernel.timer.timeout.connect(kernel.do_one_iteration) kernel.timer.start(1000 * kernel._poll_interval) app = IPKernelApp.instance() try: app.initialize(['python', '--pylab=qt']) except ZMQError: pass # already set up app.kernel.eventloop = event_loop try: app.start() except RuntimeError: # already started pass return app
def _start_ipython(self): from IPython import get_ipython if get_ipython() is not None: raise RuntimeError("Cannot start IPython, it's already running.") from zmq.eventloop.ioloop import ZMQIOLoop from ipykernel.kernelapp import IPKernelApp # save the global IOLoop instance # since IPython relies on it, but we are going to put it in a thread. save_inst = IOLoop.instance() IOLoop.clear_instance() zmq_loop = ZMQIOLoop() zmq_loop.install() # start IPython, disabling its signal handlers that won't work due to running in a thread: app = self._ipython_kernel = IPKernelApp.instance(log=logger) # Don't connect to the history database app.config.HistoryManager.hist_file = ':memory:' # listen on all interfaces, so remote clients can connect: app.ip = self.ip app.init_signal = lambda : None app.initialize([]) app.kernel.pre_handler_hook = lambda : None app.kernel.post_handler_hook = lambda : None app.kernel.start() # save self in the IPython namespace as 'worker' app.kernel.shell.user_ns['worker'] = self # start IPython's IOLoop in a thread zmq_loop_thread = Thread(target=zmq_loop.start) zmq_loop_thread.start() # put the global IOLoop instance back: IOLoop.clear_instance() save_inst.install() return app
try: sys.path.remove(osp.dirname(__file__)) except ValueError: pass locals().pop('__file__') __doc__ = '' __name__ = '__main__' # Add current directory to sys.path (like for any standard Python interpreter # executed in interactive mode): sys.path.insert(0, '') # Fire up the kernel instance. from ipykernel.kernelapp import IPKernelApp ipk_temp = IPKernelApp.instance() ipk_temp.config = kernel_config() ipk_temp.initialize() # Grabbing the kernel's shell to share its namespace with our # Variable Explorer __ipythonshell__ = ipk_temp.shell # Issue 977 : Since kernel.initialize() has completed execution, # we can now allow the monitor to communicate the availablility of # the kernel to accept front end connections. __ipythonkernel__ = ipk_temp del ipk_temp # Change %edit to open files inside Spyder # NOTE: Leave this and other magic modifications *after* setting
def _start_kernel(): """starts the ipython kernel and returns the ipython app""" if sys._ipython_app and sys._ipython_kernel_running: return sys._ipython_app import IPython from ipykernel.kernelapp import IPKernelApp from zmq.eventloop import ioloop # patch IPKernelApp.start so that it doesn't block def _IPKernelApp_start(self): if self.poller is not None: self.poller.start() self.kernel.start() # set up a timer to periodically poll the zmq ioloop loop = ioloop.IOLoop.instance() def poll_ioloop(timer_id, time): # if the kernel has been closed then run the event loop until it gets to the # stop event added by IPKernelApp.shutdown_request if self.kernel.shell.exit_now: _log.debug("IPython kernel stopping (%s)" % self.connection_file) timer.kill_timer(timer_id) loop.start() sys._ipython_kernel_running = False return # otherwise call the event loop but stop immediately if there are no pending events loop.add_timeout(0, lambda: loop.add_callback(loop.stop)) loop.start() sys._ipython_kernel_running = True timer.set_timer(100, poll_ioloop) IPKernelApp.start = _IPKernelApp_start # IPython expects sys.__stdout__ to be set sys.__stdout__ = sys.stdout sys.__stderr__ = sys.stderr # call the API embed function, which will use the monkey-patched method above IPython.embed_kernel() ipy = IPKernelApp.instance() # Keep a reference to the kernel even if this module is reloaded sys._ipython_app = ipy # patch user_global_ns so that it always references the user_ns dict setattr(ipy.shell.__class__, 'user_global_ns', property(lambda self: self.user_ns)) # patch ipapp so anything else trying to get a terminal app (e.g. ipdb) gets our IPKernalApp. from IPython.terminal.ipapp import TerminalIPythonApp TerminalIPythonApp.instance = lambda: ipy __builtins__["get_ipython"] = lambda: ipy.shell.__class__ # Use the inline matplotlib backend mpl = ipy.shell.find_magic("matplotlib") if mpl: mpl("inline") return ipy
def start_ipython(ip=None, ns=None, log=None): """Start an IPython kernel in a thread Parameters ---------- ip: str The IP address to listen on (likely the parent object's ip). ns: dict Any names that should be injected into the IPython namespace. log: logger instance Hook up IPython's logging to an existing logger instead of the default. """ from IPython import get_ipython if get_ipython() is not None: raise RuntimeError("Cannot start IPython, it's already running.") from zmq.eventloop.ioloop import ZMQIOLoop from ipykernel.kernelapp import IPKernelApp # save the global IOLoop instance # since IPython relies on it, but we are going to put it in a thread. save_inst = IOLoop.instance() IOLoop.clear_instance() zmq_loop = ZMQIOLoop() zmq_loop.install() # start IPython, disabling its signal handlers that won't work due to running in a thread: app = IPKernelApp.instance(log=log) # Don't connect to the history database app.config.HistoryManager.hist_file = ':memory:' # listen on all interfaces, so remote clients can connect: if ip: app.ip = ip # disable some signal handling, logging noop = lambda : None app.init_signal = noop app.log_connection_info = noop # start IPython in a thread # initialization happens in the thread to avoid threading problems # with the sqlite history evt = Event() def _start(): app.initialize([]) app.kernel.pre_handler_hook = noop app.kernel.post_handler_hook = noop app.kernel.start() app.kernel.loop = IOLoop.instance() # save self in the IPython namespace as 'worker' # inject things into the IPython namespace if ns: app.kernel.shell.user_ns.update(ns) evt.set() zmq_loop.start() zmq_loop_thread = Thread(target=_start) zmq_loop_thread.daemon = True zmq_loop_thread.start() assert evt.wait(timeout=5), "IPython didn't start in a reasonable amount of time." # put the global IOLoop instance back: IOLoop.clear_instance() save_inst.install() return app