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 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 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 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 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 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 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 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 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(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)
from time import time try: from ipykernel.kernelapp import IPKernelApp from ipywidgets import widgets, IntProgress, HBox, HTML, VBox from IPython.display import clear_output, display from ipywidgets.widgets.interaction import show_inline_matplotlib_plots import matplotlib.pyplot as plt IN_NOTEBOOK = IPKernelApp.initialized() except: IN_NOTEBOOK = False __all__ = ['master_bar', 'progress_bar'] def format_time(t): t = int(t) h, m, s = t // 3600, (t // 60) % 60, t % 60 if h != 0: return f'{h}:{m:02d}:{s:02d}' else: return f'{m:02d}:{s:02d}' class ProgressBar(): update_every = 0.2 def __init__(self, gen, display=True, leave=True, parent=None): self._gen, self.total = gen, len(gen) if parent is None: self.leave, self.display = leave, display else: self.leave, self.display = False, False parent.add_child(self)
def in_notebook(): return IPKernelApp.initialized() import tqdm as tq
def in_notebook(): return IPKernelApp.initialized() def is_tuple(x) -> bool: return isinstance(x, tuple)
] return f'{self.name} ({self.id} {self.state}): {root_dev[0]["Ebs"]["VolumeSize"]}GB' else: identifiers = [ f'{ident}={repr(getattr(self, ident))}' for ident in self.meta.identifiers ] return f"{self.__class__.__name__}({', '.join(identifiers)})" boto3.resources.base.ServiceResource.__repr__ = _boto3_repr _in_notebook = False try: from ipykernel.kernelapp import IPKernelApp _in_notebook = IPKernelApp.initialized() except: pass def listify(p=None, q=None): "Make `p` listy and the same length as `q`." if p is None: p = [] elif isinstance(p, str): p = [p] elif not isinstance(p, Iterable): p = [p] n = q if type(q) == int else len(p) if q is None else len(q) if len(p) == 1: p = p * n assert len(p) == n, f'List len mismatch ({len(p)} vs {n})' return list(p)
def in_notebook(): return IPKernelApp.initialized() def printmd(string): display(Markdown(string))
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 # The stdout/stderrs used by IPython. These get set after the kernel has started. ipy_stdout = sys.stdout ipy_stderr = sys.stderr # patch IPKernelApp.start so that it doesn't block def _IPKernelApp_start(self): nonlocal ipy_stdout, ipy_stderr if self.poller is not None: self.poller.start() self.kernel.start() # set up a timer to periodically poll the zmq ioloop self.loop = ioloop.IOLoop.current() def poll_ioloop(): try: # Use the IPython stdout/stderr while running the kernel with PushStdout(ipy_stdout, ipy_stderr): # 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) self.loop.start() sys._ipython_kernel_running = False return # otherwise call the event loop but stop immediately if there are no pending events self.loop.add_timeout( 0, lambda: self.loop.add_callback(self.loop.stop)) self.loop.start() except: _log.error("Error polling Jupyter loop", exc_info=True) schedule_call(poll_ioloop, delay=0.1) sys._ipython_kernel_running = True schedule_call(poll_ioloop, delay=0.1) IPKernelApp.start = _IPKernelApp_start # IPython expects sys.__stdout__ to be set, and keep the original values to # be used after IPython has set its own. sys.__stdout__ = sys_stdout = sys.stdout sys.__stderr__ = sys_stderr = sys.stderr # Get or create the IPKernelApp instance and set the 'connection_dir' property if IPKernelApp.initialized(): ipy = IPKernelApp.instance() else: ipy = IPKernelApp.instance(local_ns={}) ipy.connection_dir = _get_connection_dir(ipy) ipy.initialize([]) # call the API embed function, which will use the monkey-patched method above embed_kernel(local_ns={}) # register the magic functions ipy.shell.register_magics(ExcelMagics) # Keep a reference to the kernel even if this module is reloaded sys._ipython_app = ipy # Restore sys stdout/stderr and keep track of the IPython versions ipy_stdout = sys.stdout ipy_stderr = sys.stderr sys.stdout = sys_stdout sys.stderr = sys_stderr # 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 # Use the inline matplotlib backend mpl = ipy.shell.find_magic("matplotlib") if mpl: try: mpl("inline") except ImportError: pass return ipy
def in_notebook(): """Check if this code is being executed in a notebook""" if not has_ipynb_shell(): return False from ipykernel.kernelapp import IPKernelApp return IPKernelApp.initialized()
def in_notebook(): return IPKernelApp.initialized()
def in_notebook(): return IPKernelApp.initialized() def in_ipynb():