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 main(): """launch a root kernel""" try: from ipykernel.kernelapp import IPKernelApp except ImportError: from IPython.kernel.zmq.kernelapp import IPKernelApp IPKernelApp.launch_instance(kernel_class=ROOTKernel)
def start_kernel(): """ Start Pytuga Jupyter kernel. """ from ipykernel.kernelapp import IPKernelApp IPKernelApp.launch_instance(kernel_class=PytugaKernel)
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 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 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 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 __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 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 my_embed_kernel(module=None, local_ns=None, **kwargs): # get the app if it exists, or set it up if it doesn't if IPKernelApp.initialized(): app = MyIPKernelApp.instance() else: app = MyIPKernelApp.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. 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 = module app.kernel.user_ns = local_ns app.shell.set_completer_frame() app.start()
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 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 bind_kernel(**kwargs): """Bind an Engine's Kernel to be used as a full IPython kernel. This allows a running Engine to be used simultaneously as a full IPython kernel with the QtConsole or other frontends. This function returns immediately. """ from ipykernel.kernelapp import IPKernelApp from ipyparallel.apps.ipengineapp import IPEngineApp # first check for IPKernelApp, in which case this should be a no-op # because there is already a bound kernel if IPKernelApp.initialized() and isinstance(IPKernelApp._instance, IPKernelApp): return if IPEngineApp.initialized(): try: app = IPEngineApp.instance() except MultipleInstanceError: pass else: return app.bind_kernel(**kwargs) raise RuntimeError("bind_kernel be called from an IPEngineApp instance")
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 embed_kernel(self, 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(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_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(): 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_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 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 _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 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 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 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 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 _launch_client(self, kernel_id): # we have to create the event-loop explicitly here since IOLoop.current() is prohibited outside of the main # thread AsyncIOLoop(make_current=True) # prepare parameters kernel_file = 'kernel-' + kernel_id + '.json' code_to_run = 'from jupyter_singleton.singletonapp import SingletonApp\n' + \ 'SingletonApp.client_started=True' kernel_class = 'jupyter_singleton.singletonipkernel.SingletonIPythonKernel' parameters = { 'connection_file': kernel_file, 'code_to_run': code_to_run, 'quiet': False, 'kernel_class': kernel_class } # start jupyter client self.kernel_app = IPKernelApp(**parameters) self.kernel_app.initialize([]) if hasattr(self.kernel_app.kernel, 'set_displaydatahook'): self.kernel_app.kernel.set_displaydatahook(self.display_data_hook) self.kernel_app.start()
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 bind_kernel(self, **kwargs): """Promote engine to listening kernel, accessible to frontends.""" if self.kernel_app is not None: return self.log.info( "Opening ports for direct connections as an IPython kernel") if self.curve_serverkey: self.log.warning("Bound kernel does not support CURVE security") kernel = self.kernel kwargs.setdefault('config', self.config) kwargs.setdefault('log', self.log) kwargs.setdefault('profile_dir', self.profile_dir) kwargs.setdefault('session', self.session) app = self.kernel_app = IPKernelApp(**kwargs) # allow IPKernelApp.instance(): IPKernelApp._instance = app app.init_connection_file() # relevant contents of init_sockets: app.shell_port = app._bind_socket(kernel.shell_streams[0], app.shell_port) app.log.debug("shell ROUTER Channel on port: %i", app.shell_port) iopub_socket = kernel.iopub_socket # ipykernel 4.3 iopub_socket is an IOThread wrapper: if hasattr(iopub_socket, 'socket'): iopub_socket = iopub_socket.socket app.iopub_port = app._bind_socket(iopub_socket, app.iopub_port) app.log.debug("iopub PUB Channel on port: %i", app.iopub_port) kernel.stdin_socket = self.context.socket(zmq.ROUTER) app.stdin_port = app._bind_socket(kernel.stdin_socket, app.stdin_port) app.log.debug("stdin ROUTER Channel on port: %i", app.stdin_port) # start the heartbeat, and log connection info: app.init_heartbeat() app.log_connection_info() app.connection_dir = self.profile_dir.security_dir app.write_connection_file()
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__' # Import our customizations into the kernel import_spydercustomize() # Remove current directory from sys.path to prevent kernel # crashes when people name Python files or modules with # the same name as standard library modules. # See spyder-ide/spyder#8007 while '' in sys.path: sys.path.remove('') # Fire up the kernel instance. from ipykernel.kernelapp import IPKernelApp from spyder_kernels.console.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 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
self.send_response(self.iopub_socket, 'stream', stream_content) if interrupted: return {'status': 'abort', 'execution_count': self.execution_count} return {'status': 'ok', 'execution_count': self.execution_count, 'payload': [], 'user_expressions': {}} def do_execute_command(self, cmd, silent): interrupted = False if cmd.startswith('%'): args = shlex.split(cmd) cmd = args.pop(0)[1:] if hasattr(self, '_cmd_' + cmd): return getattr(self, '_cmd_' + cmd)(args, silent), interrupted output = '' try: output = self.msf_wrapper.run_command(cmd, timeout=self.timeout) except KeyboardInterrupt: self.msf_wrapper.child.sendintr() interrupted = True self.msf_wrapper._expect_prompt() output = self.msf_wrapper.child.before except pexpect.EOF: output = self.msf_wrapper.child.before + 'Restarting Metasploit' self._start_msfconsole() return output, interrupted if __name__ == '__main__': IPKernelApp.launch_instance(kernel_class=MetasploitKernel)
backend = settings["backend"] width, height = settings["size"] resolution = settings["resolution"] for k, v in { "defaultfigurevisible": backends[backend], "defaultfigurepaperpositionmode": "manual", "defaultfigurepaperposition": matlab.double([0, 0, width / resolution, height / resolution]), "defaultfigurepaperunits": "inches"}.items(): self._matlab.set(0., k, v, nargout=0) def repr(self, obj): return obj def restart_kernel(self): self._matlab.exit() self._matlab = matlab.engine.start_matlab() self._first = True def do_shutdown(self, restart): self._matlab.exit() return super(MatlabKernel, self).do_shutdown(restart) if __name__ == '__main__': try: from ipykernel.kernelapp import IPKernelApp except ImportError: from IPython.kernel.zmq.kernelapp import IPKernelApp IPKernelApp.launch_instance(kernel_class=MatlabKernel)
response = {'name': 'stdout', 'text': hist.outs[-1]} self.send_response(self.iopub_socket, 'stream', response) if interrupted: return {'status': 'abort', 'execution_count': self.execution_count} rtn = 0 if len(hist) == 0 else hist.rtns[-1] if 0 < rtn: message = {'status': 'error', 'execution_count': self.execution_count, 'ename': '', 'evalue': str(rtn), 'traceback': []} else: message = {'status': 'ok', 'execution_count': self.execution_count, 'payload': [], 'user_expressions': {}} return message def do_complete(self, code, pos): """Get completions.""" shell = builtins.__xonsh_shell__ comps, beg, end = shell.completer.find_and_complete(code, pos, shell.ctx) message = {'matches': comps, 'cursor_start': beg, 'cursor_end': end+1, 'metadata': {}, 'status': 'ok'} return message if __name__ == '__main__': from ipykernel.kernelapp import IPKernelApp # must manually pass in args to avoid interfering w/ Jupyter arg parsing with main_context(argv=['--shell-type=readline']): IPKernelApp.launch_instance(kernel_class=XonshKernel)
# # Copyright SAS Institute # # Licensed under the Apache License, Version 2.0 (the License); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # from ipykernel.kernelapp import IPKernelApp from .kernel import SASKernel IPKernelApp.launch_instance(kernel_class=SASKernel)
from ipykernel.kernelapp import IPKernelApp from .kernel import polymakeKernel IPKernelApp.launch_instance(kernel_class=polymakeKernel)
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
from ipykernel.kernelapp import IPKernelApp from .kernel import CQLKernel IPKernelApp.launch_instance(kernel_class=CQLKernel)
except: self._child.kill(signal.SIGKILL) return {'status':'ok', 'restart':restart} def jyrepl(self,code,timeout=None): out="" #this if is needed for printing output if command entered is "variable" or fucntions like abc(var) and for code completion # if (len(re.split(r"\=",code.strip()))==1) and (len(re.split(r"[\ ]",code.strip()))==1): # code='eval('+repr(code.strip())+')' # self._child.sendline(code) # now_prompt=self._child.expect_exact([u">>> ",u"... "]) # if len(self._child.before.splitlines())>1: out+='\n'.join(self._child.before.splitlines()[1:])+'\n' # now_prompt=self._child.expect_exact([u">>> ",u"... "]) # else: # code='exec('+repr(code)+')' # for line in code.splitlines(): # self._child.sendline(line) # now_prompt=self._child.expect_exact([u">>> ",u"... "]) # if len(self._child.before.splitlines())>1: out+='\n'.join(self._child.before.splitlines()[1:])+'\n' # now_prompt=self._child.expect_exact([u">>> ",u"... "]) code='exec('+repr(code)+')' for line in code.splitlines(): self._child.sendline(line) now_prompt=self._child.expect_exact([u">>> ",u"... "]) if len(self._child.before.splitlines())>1: out+='\n'.join(self._child.before.splitlines()[1:])+'\n' now_prompt=self._child.expect_exact([u">>> ",u"... "]) return out if __name__ == '__main__': from ipykernel.kernelapp import IPKernelApp IPKernelApp.launch_instance(kernel_class=JythonKernel)