def start_kernel(self): """ Create the expected context, start the kernel, obtain a client and emit a signal when both are started. """ logger.info(sys.path) os.chdir(self.cwd) # Ensure the kernel runs with the expected CWD. # Add user defined envars to os.environ so they can be picked up by # the child process running the kernel. logger.info('Starting iPython kernel with user defined envars: ' '{}'.format(self.envars)) for k, v in self.envars.items(): os.environ[k] = v if sys.platform == 'darwin': parent_dir = os.path.dirname(__file__) if '.app/Contents/Resources/app/mu' in parent_dir: # Mu is running as a macOS app bundle. Ensure the expected # paths are in PYTHONPATH of the subprocess so the kernel can # be found. os.environ['PYTHONPATH'] = ':'.join(sys.path) self.repl_kernel_manager = QtKernelManager() self.repl_kernel_manager.start_kernel() self.repl_kernel_client = self.repl_kernel_manager.client() self.kernel_started.emit(self.repl_kernel_manager, self.repl_kernel_client)
def create_kernel_manager_and_client(self, connection_file=None, hostname=None, sshkey=None, password=None): """Create kernel manager and client""" cf = find_connection_file(connection_file, profile='default') kernel_manager = QtKernelManager(connection_file=cf, config=None) kernel_client = kernel_manager.client() kernel_client.load_connection_file() if hostname is not None: try: newports = self.tunnel_to_kernel(dict(ip=kernel_client.ip, shell_port=kernel_client.shell_port, iopub_port=kernel_client.iopub_port, stdin_port=kernel_client.stdin_port, hb_port=kernel_client.hb_port), hostname, sshkey, password) (kernel_client.shell_port, kernel_client.iopub_port, kernel_client.stdin_port, kernel_client.hb_port) = newports except Exception as e: print(("Could not open ssh tunnel. The error was:\n\n" + e.message)) return None, None kernel_client.start_channels() # To rely on kernel's heartbeat to know when a kernel has died kernel_client.hb_channel.unpause() return kernel_manager, kernel_client
def _init_kernel_manager(self): connection_file = find_connection_file(self.app.connection_file) manager = QtKernelManager(connection_file=connection_file) manager.load_connection_file() manager.start_channels() atexit.register(manager.cleanup_connection_file) self.kernel_manager = manager
def launch_qtconsole(connection_file): wait_for_connection_file(connection_file) app = QtWidgets.QApplication([]) app._kernel_has_shutdown = False kernel_manager = QtKernelManager() kernel_manager.load_connection_file(connection_file) kernel_client = kernel_manager.client() kernel_client.start_channels() jupyter_widget = JupyterWidget() jupyter_widget.kernel_manager = kernel_manager jupyter_widget.kernel_client = kernel_client window = MainWindow(jupyter_widget) window.show() def shutdown_kernel(): if app._kernel_has_shutdown: return jupyter_widget.kernel_client.stop_channels() jupyter_widget.kernel_manager.shutdown_kernel() app._kernel_has_shutdown = True app.exit() jupyter_widget.exit_requested.connect(shutdown_kernel) app.aboutToQuit.connect(shutdown_kernel) return app.exec_()
def start_kernel(self): """ Create the expected context, start the kernel, obtain a client and emit a signal when both are started. """ logger.info(sys.path) os.chdir(self.cwd) # Ensure the kernel runs with the expected CWD. # Add user defined envars to os.environ so they can be picked up by # the child process running the kernel. logger.info("Starting iPython kernel with user defined envars: " "{}".format(self.envars)) for k, v in self.envars.items(): os.environ[k] = v # Ensure the expected paths are in PYTHONPATH of the subprocess so the # kernel and Mu-installed third party applications can be found. if "PYTHONPATH" not in os.environ: paths = sys.path + [MODULE_DIR] os.environ["PYTHONPATH"] = os.pathsep.join(paths) if MODULE_DIR not in os.environ["PYTHONPATH"]: # This is needed on Windows to ensure user installed third party # packages are available in the REPL. new_path = os.pathsep.join([os.environ["PYTHONPATH"], MODULE_DIR]) os.environ["PYTHONPATH"] = new_path logger.info("REPL PYTHONPATH: {}".format(os.environ["PYTHONPATH"])) self.repl_kernel_manager = QtKernelManager() self.repl_kernel_manager.start_kernel() self.repl_kernel_client = self.repl_kernel_manager.client() self.kernel_started.emit(self.repl_kernel_manager, self.repl_kernel_client)
def createConsoleTab(self): """ Initialize a python console and return its widget """ from qtconsole.rich_jupyter_widget import RichJupyterWidget from qtconsole.manager import QtKernelManager kernel_manager = QtKernelManager(kernel_name='python3') kernel_manager.start_kernel() kernel = kernel_manager.kernel kernel.gui = 'qt' kernel_client = kernel_manager.client() kernel_client.start_channels() kernel_client.namespace = self def stop(): kernel_client.stop_channels() kernel_manager.shutdown_kernel() widget = RichJupyterWidget(parent=self) widget.kernel_manager = kernel_manager widget.kernel_client = kernel_client widget.exit_requested.connect(stop) ipython_widget = widget ipython_widget.show() self.kernel_client = kernel_client #kernel_client.execute("import devices.demo.demo") return widget
def _createConsoleWidget(self): if is_using_pyqt5(): layout = QtWidgets.QVBoxLayout() else: layout = QtGui.QVBoxLayout() connection_file = find_connection_file(self.connection_file) self.kernel_manager = QtKernelManager(connection_file=connection_file) self.kernel_manager.load_connection_file() self.kernel_manager.client_factory = QtKernelClient self.kernel_client = self.kernel_manager.client() self.kernel_client.start_channels() widget_options = {} if sys.platform.startswith('linux'): # Some upstream bug crashes IDA when the ncurses completion is # used. I'm not sure where the bug is exactly (IDA's Qt5 bindings?) # but using the "droplist" instead works around the crash. The # problem is present only on Linux. # See: https://github.com/eset/ipyida/issues/8 widget_options["gui_completion"] = 'droplist' widget_options.update(_user_widget_options) if ipyida.kernel.is_using_ipykernel_5(): self.ipython_widget = RichJupyterWidget(self.parent, **widget_options) else: self.ipython_widget = IdaRichJupyterWidget(self.parent, **widget_options) self.ipython_widget.kernel_manager = self.kernel_manager self.ipython_widget.kernel_client = self.kernel_client layout.addWidget(self.ipython_widget) return layout
def start_python_kernel(self): """Starts kernel manager and client and attaches the client to the Python Console.""" self._kernel_starting = True km = QtKernelManager(kernel_name=self.kernel_name) try: blackhole = open(os.devnull, 'w') km.start_kernel(stdout=blackhole, stderr=blackhole) kc = km.client() kc.start_channels() self.kernel_manager = km self.kernel_client = kc self.connect_signals() return True except FileNotFoundError: self._toolbox.msg_error.emit( "\tCouldn't find the Python executable specified by the Jupyter kernel" ) self._kernel_starting = False return False except NoSuchKernel: # kernelspecs for the selected kernel_name not available self._toolbox.msg_error.emit( "\tCouldn't find the specified IPython kernel specs [{0}]". format(self.kernel_name)) self._kernel_starting = False return False
def __init__(self, kernel, *args, **kwargs): bg_color = get_emacs_var("eaf-emacs-theme-background-color") fg_color = get_emacs_var("eaf-emacs-theme-foreground-color") dark_mode = (get_emacs_var("eaf-jupyter-dark-mode") == "force" or \ get_emacs_var("eaf-jupyter-dark-mode") == True or \ (get_emacs_var("eaf-jupyter-dark-mode") == "follow" and get_emacs_var("eaf-emacs-theme-mode") == "dark")) self._init_style(bg_color, fg_color, dark_mode) self.scrollbar_visibility = False super(EafJupyterWidget, self).__init__(*args, **kwargs) kernel_manager = QtKernelManager(kernel_name=kernel) kernel_manager.start_kernel() kernel_client = kernel_manager.client() kernel_client.start_channels() self.kernel_manager = kernel_manager self.kernel_client = kernel_client self._control.setStyleSheet("border: none;") self._page_control.setStyleSheet("border: none;") self._kill_ring = EafKillRing(self._control)
class KernelRunner(QObject): """ Used to control the iPython kernel in a non-blocking manner so the UI remains responsive. """ kernel_started = pyqtSignal(QtKernelManager, QtKernelClient) kernel_finished = pyqtSignal() def __init__(self, cwd): """ Initialise the kernel runner with a target current working directory. """ super().__init__() self.cwd = cwd def start_kernel(self): """ Start the kernel, obtain a client and emit a signal when both are started. """ os.chdir(self.cwd) # Ensure the kernel runs with the expected CWD. self.repl_kernel_manager = QtKernelManager() self.repl_kernel_manager.start_kernel() self.repl_kernel_client = self.repl_kernel_manager.client() self.kernel_started.emit(self.repl_kernel_manager, self.repl_kernel_client) def stop_kernel(self): """ Stop the client connections to the kernel, affect an immediate shutdown of the kernel and emit a "finished" signal. """ self.repl_kernel_client.stop_channels() self.repl_kernel_manager.shutdown_kernel(now=True) self.kernel_finished.emit()
class KernelRunner(QObject): """ Used to control the iPython kernel in a non-blocking manner so the UI remains responsive. """ kernel_started = pyqtSignal(QtKernelManager, QtKernelClient) kernel_finished = pyqtSignal() # Used to build context with user defined envars when running the REPL. default_envars = os.environ.copy() def __init__(self, cwd, envars): """ Initialise the kernel runner with a target current working directory. """ super().__init__() self.cwd = cwd self.envars = dict(envars) def start_kernel(self): """ Create the expected context, start the kernel, obtain a client and emit a signal when both are started. """ logger.info(sys.path) os.chdir(self.cwd) # Ensure the kernel runs with the expected CWD. # Add user defined envars to os.environ so they can be picked up by # the child process running the kernel. logger.info("Starting iPython kernel with user defined envars: " "{}".format(self.envars)) for k, v in self.envars.items(): os.environ[k] = v # Ensure the expected paths are in PYTHONPATH of the subprocess so the # kernel and Mu-installed third party applications can be found. if "PYTHONPATH" not in os.environ: paths = sys.path + [MODULE_DIR] os.environ["PYTHONPATH"] = os.pathsep.join(paths) if MODULE_DIR not in os.environ["PYTHONPATH"]: # This is needed on Windows to ensure user installed third party # packages are available in the REPL. new_path = os.pathsep.join([os.environ["PYTHONPATH"], MODULE_DIR]) os.environ["PYTHONPATH"] = new_path logger.info("REPL PYTHONPATH: {}".format(os.environ["PYTHONPATH"])) self.repl_kernel_manager = QtKernelManager() self.repl_kernel_manager.start_kernel() self.repl_kernel_client = self.repl_kernel_manager.client() self.kernel_started.emit(self.repl_kernel_manager, self.repl_kernel_client) def stop_kernel(self): """ Clean up the context, stop the client connections to the kernel, affect an immediate shutdown of the kernel and emit a "finished" signal. """ os.environ.clear() for k, v in self.default_envars.items(): os.environ[k] = v self.repl_kernel_client.stop_channels() self.repl_kernel_manager.shutdown_kernel(now=True) self.kernel_finished.emit()
def _start_kernel(): km = QtKernelManager(autorestart=False) km.start_kernel() kc = km.client() kc.start_channels() kc.execute("%gui qt", silent=True) time.sleep(1.5) return km, kc
class IPythonConsole(idaapi.PluginForm): def __init__(self, connection_file, *args): super(IPythonConsole, self).__init__(*args) self.connection_file = connection_file def OnCreate(self, form): try: if is_using_pyqt5(): self.parent = self.FormToPyQtWidget(form, ctx=sys.modules[__name__]) else: self.parent = self.FormToPySideWidget( form, ctx=sys.modules[__name__]) layout = self._createConsoleWidget() self.parent.setLayout(layout) except: import traceback print(traceback.format_exc()) def _createConsoleWidget(self): if is_using_pyqt5(): layout = QtWidgets.QVBoxLayout() else: layout = QtGui.QVBoxLayout() connection_file = find_connection_file(self.connection_file) self.kernel_manager = QtKernelManager(connection_file=connection_file) self.kernel_manager.load_connection_file() self.kernel_manager.client_factory = QtKernelClient self.kernel_client = self.kernel_manager.client() self.kernel_client.start_channels() widget_options = {} if sys.platform.startswith('linux'): # Some upstream bug crashes IDA when the ncurses completion is # used. I'm not sure where the bug is exactly (IDA's Qt5 bindings?) # but using the "droplist" instead works around the crash. The # problem is present only on Linux. # See: https://github.com/eset/ipyida/issues/8 widget_options["gui_completion"] = 'droplist' self.ipython_widget = IdaRichJupyterWidget(self.parent, **widget_options) self.ipython_widget.kernel_manager = self.kernel_manager self.ipython_widget.kernel_client = self.kernel_client layout.addWidget(self.ipython_widget) return layout def Show(self, name="IPython Console"): return idaapi.PluginForm.Show(self, name) def OnClose(self, form): try: pass except: import traceback print(traceback.format_exc())
def restart_kernel(self): self.kernel_client.stop_channels() self.kernel_manager.shutdown_kernel() self.kernel_manager = QtKernelManager() self.kernel_manager.start_kernel() self.kernel_manager.kernel.gui = 'qt' self.kernel_client = self._kernel_manager.client() self.kernel_client.start_channels() self.kernel_client.iopub_channel.message_received.connect( self.update_kernel_status)
class KernelRunner(QObject): """ Used to control the iPython kernel in a non-blocking manner so the UI remains responsive. """ kernel_started = pyqtSignal(QtKernelManager, QtKernelClient) kernel_finished = pyqtSignal() # Used to build context with user defined envars when running the REPL. default_envars = os.environ.copy() def __init__(self, cwd, envars): """ Initialise the kernel runner with a target current working directory. """ super().__init__() self.cwd = cwd self.envars = dict(envars) def start_kernel(self): """ Create the expected context, start the kernel, obtain a client and emit a signal when both are started. """ logger.info(sys.path) os.chdir(self.cwd) # Ensure the kernel runs with the expected CWD. # Add user defined envars to os.environ so they can be picked up by # the child process running the kernel. logger.info('Starting iPython kernel with user defined envars: ' '{}'.format(self.envars)) for k, v in self.envars.items(): os.environ[k] = v if sys.platform == 'darwin': parent_dir = os.path.dirname(__file__) if '.app/Contents/Resources/app/mu' in parent_dir: # Mu is running as a macOS app bundle. Ensure the expected # paths are in PYTHONPATH of the subprocess so the kernel can # be found. os.environ['PYTHONPATH'] = ':'.join(sys.path) self.repl_kernel_manager = QtKernelManager() self.repl_kernel_manager.start_kernel() self.repl_kernel_client = self.repl_kernel_manager.client() self.kernel_started.emit(self.repl_kernel_manager, self.repl_kernel_client) def stop_kernel(self): """ Clean up the context, stop the client connections to the kernel, affect an immediate shutdown of the kernel and emit a "finished" signal. """ os.environ.clear() for k, v in self.default_envars.items(): os.environ[k] = v self.repl_kernel_client.stop_channels() self.repl_kernel_manager.shutdown_kernel(now=True) self.kernel_finished.emit()
def default_manager(kernel): """ Return a configured QtKernelManager :param kernel: An IPKernelApp instance """ connection_file = find_connection_file(kernel.connection_file) manager = QtKernelManager(connection_file=connection_file) manager.load_connection_file() manager.start_channels() atexit.register(manager.cleanup_connection_file) return manager
class IPythonConsole(idaapi.PluginForm): def __init__(self, connection_file, *args): super(IPythonConsole, self).__init__(*args) self.connection_file = connection_file def OnCreate(self, form): try: if is_using_pyqt5(): self.parent = self.FormToPyQtWidget(form, ctx=sys.modules[__name__]) else: self.parent = self.FormToPySideWidget(form, ctx=sys.modules[__name__]) layout = self._createConsoleWidget() self.parent.setLayout(layout) except: import traceback print(traceback.format_exc()) def _createConsoleWidget(self): if is_using_pyqt5(): layout = QtWidgets.QVBoxLayout() else: layout = QtGui.QVBoxLayout() connection_file = find_connection_file(self.connection_file) self.kernel_manager = QtKernelManager(connection_file=connection_file) self.kernel_manager.load_connection_file() self.kernel_manager.client_factory = QtKernelClient self.kernel_client = self.kernel_manager.client() self.kernel_client.start_channels() widget_options = {} if sys.platform.startswith('linux'): # Some upstream bug crashes IDA when the ncurses completion is # used. I'm not sure where the bug is exactly (IDA's Qt5 bindings?) # but using the "droplist" instead works around the crash. The # problem is present only on Linux. # See: https://github.com/eset/ipyida/issues/8 widget_options["gui_completion"] = 'droplist' self.ipython_widget = IdaRichJupyterWidget(self.parent, **widget_options) self.ipython_widget.kernel_manager = self.kernel_manager self.ipython_widget.kernel_client = self.kernel_client layout.addWidget(self.ipython_widget) return layout def Show(self, name="IPython Console"): return idaapi.PluginForm.Show(self, name) def OnClose(self, form): try: pass except: import traceback print(traceback.format_exc())
def start_kernel(self): """ Start the kernel, obtain a client and emit a signal when both are started. """ os.chdir(self.cwd) # Ensure the kernel runs with the expected CWD. self.repl_kernel_manager = QtKernelManager() self.repl_kernel_manager.start_kernel() self.repl_kernel_client = self.repl_kernel_manager.client() self.kernel_started.emit(self.repl_kernel_manager, self.repl_kernel_client)
def connect_to_kernel(self, kernel_name, connection_file): """Connects to an existing kernel. Used when Spine Engine is managing the kernel for project execution. Args: connection_file (str): Path to the connection file of the kernel """ self.kernel_manager = QtKernelManager(connection_file=connection_file) self.kernel_manager.load_connection_file() self.kernel_name = kernel_name self._setup_client() self.include_other_output = True self.other_output_prefix = ""
def make_jupyter_widget_with_kernel(): """Start a kernel, connect to it, and create a RichJupyterWidget to use it """ kernel_manager = QtKernelManager(kernel_name=USE_KERNEL) kernel_manager.start_kernel() kernel_client = kernel_manager.client() kernel_client.start_channels() jupyter_widget = RichJupyterWidget() jupyter_widget.kernel_manager = kernel_manager jupyter_widget.kernel_client = kernel_client return jupyter_widget
def make_jupyter_widget_with_kernel(self): """Start a kernel, connect to it, and create a RichJupyterWidget to use it """ USE_KERNEL = 'python3' kernel_manager = QtKernelManager(kernel_name=USE_KERNEL) kernel_manager.start_kernel() kernel_client = kernel_manager.client() kernel_client.start_channels() jupyter_widget = RichJupyterWidget() jupyter_widget.kernel_manager = kernel_manager jupyter_widget.kernel_client = kernel_client jupyter_widget._display_banner = False return jupyter_widget
def start_kernel(self, kernel: str): kernel_manager = QtKernelManager(kernel_name=kernel) kernel_manager.start_kernel() kernel_client = kernel_manager.client() kernel_client.start_channels() jupyter_widget = RichJupyterWidget() jupyter_widget.style_sheet = get_theme_contents( "dracula", "jupyter.css") jupyter_widget.syntax_style = "dracula" jupyter_widget.kernel_manager = kernel_manager jupyter_widget.kernel_client = kernel_client return jupyter_widget # Binding this to a variable won't work.
def main(): """Start kernel manager and client, create window, run app event loop, auto execute some code in user namespace. A minimalist example is shown in qt_ip_test.py. NOTE: Make sure that the Qt v2 API is being used by IPython by running `export QT_API=pyqt` at the command line before running neuropy, or by adding it to `.bashrc`""" app = guisupport.get_app_qt4() if INPROCESS: from qtconsole.inprocess import QtInProcessKernelManager km = QtInProcessKernelManager() else: from qtconsole.manager import QtKernelManager km = QtKernelManager() km.start_kernel() km.kernel.gui = 'qt4' kc = km.client() kc.start_channels() nw = NeuropyWindow() ipw = nw.ipw config_ipw(ipw) ipw.kernel_manager = km ipw.kernel_client = kc ipw.exit_requested.connect(nw.stop) nw.show() # execute some code through the frontend (once the event loop is running). # The output appears in the IPythonWidget (ipw). do_later(ipw.execute, 'run -i %s' % 'startup.py', hidden=True) do_later(ipw.execute, 'run -i %s' % 'globals.py', hidden=True) #guisupport.start_event_loop_qt4(app) # doesn't seem to work in IPy 5.3.0 app.exec_()
def _createConsoleWidget(self): if is_using_pyqt5(): layout = QtWidgets.QVBoxLayout() else: layout = QtGui.QVBoxLayout() connection_file = find_connection_file(self.connection_file) self.kernel_manager = QtKernelManager(connection_file=connection_file) self.kernel_manager.load_connection_file() self.kernel_manager.client_factory = QtKernelClient self.kernel_client = self.kernel_manager.client() self.kernel_client.start_channels() widget_options = {} if sys.platform.startswith('linux'): # Some upstream bug crashes IDA when the ncurses completion is # used. I'm not sure where the bug is exactly (IDA's Qt5 bindings?) # but using the "droplist" instead works around the crash. The # problem is present only on Linux. # See: https://github.com/eset/ipyida/issues/8 widget_options["gui_completion"] = 'droplist' self.ipython_widget = IdaRichJupyterWidget(self.parent, **widget_options) self.ipython_widget.kernel_manager = self.kernel_manager self.ipython_widget.kernel_client = self.kernel_client layout.addWidget(self.ipython_widget) return layout
def independent_qtconsole(): kernel_manager = QtKernelManager() kernel_manager.start_kernel(show_banner=False) kernel = kernel_manager.kernel kernel.gui = 'qt4' kernel_client = kernel_manager.client() kernel_client.start_channels() ipython_widget = RichJupyterWidget() ipython_widget.kernel_manager = kernel_manager ipython_widget.kernel_client = kernel_client ipython_widget.show() return ipython_widget
def main(): """Start kernel manager and client, create window, run app event loop""" app = guisupport.get_app_qt4() if INPROCESS: from qtconsole.inprocess import QtInProcessKernelManager km = QtInProcessKernelManager() else: from qtconsole.manager import QtKernelManager km = QtKernelManager() km.start_kernel() km.kernel.gui = 'qt4' kc = km.client() kc.start_channels() widget = RichJupyterWidget() widget.kernel_manager = km widget.kernel_client = kc if CLEANSHUTDOWN: # slow exit on CTRL+D def stop(): kc.stop_channels() km.shutdown_kernel() app.exit() widget.exit_requested.connect(stop) else: # fast exit on CTRL+D widget.exit_requested.connect(app.quit) widget.show() guisupport.start_event_loop_qt4(app)
def __init__(self): super().__init__() km = QtKernelManager(autorestart=False) km.start_kernel() kc = km.client() kc.start_channels() self.jupyter_widget = RichJupyterWidget() self.jupyter_widget.kernel_manager = km self.jupyter_widget.kernel_client = kc self.setCentralWidget(self.jupyter_widget) kc.execute("%gui qt", silent=True) time.sleep(3) # I need something better here, but I need to make sure # the QApplication in the kernel has started before # attempting to start the UI or it won't start kc.execute( "from pyomo.contrib.viewer.ui import get_mainwindow", silent=True) kc.execute("import pyomo.environ as pyo", silent=True) kc.execute("ui, model = get_mainwindow()", silent=True)
def _start_kernel(): km = QtKernelManager(autorestart=False) km.start_kernel() kc = km.client() kc.start_channels() kc.execute("%gui qt", silent=True) # make sure there is no possible way the user can start the model # viewer before the Qt Application in the kernel finishes starting time.sleep(1.0) # Now just do the standard imports of things you want to be available # and whatever we may want to do to set up the environment just create # an empty model, so you can start the model viewer right away. You # can add to the model if you want to use it, or create a new one. kc.execute(""" from pyomo.contrib.viewer.ui import get_mainwindow import pyomo.environ as pyo model = pyo.ConcreteModel("Default Model")""", silent=True) return km, kc
def __init__(self): super().__init__() km = QtKernelManager(autorestart=False) km.start_kernel() kc = km.client() kc.start_channels() self.jupyter_widget = RichJupyterWidget() self.jupyter_widget.kernel_manager = km self.jupyter_widget.kernel_client = kc self.setCentralWidget(self.jupyter_widget) kc.execute("%gui qt", silent=True) time.sleep(3) # I need something better here, but I need to make sure # the QApplication in the kernel has started before # attempting to start the UI or it won't start kc.execute("from pyomo.contrib.viewer.ui import get_mainwindow", silent=True) kc.execute("import pyomo.environ as pyo", silent=True) kc.execute("ui, model = get_mainwindow()", silent=True)
def start_kernel(self): """ Create the expected context, start the kernel, obtain a client and emit a signal when both are started. """ logger.info(sys.path) os.chdir(self.cwd) # Ensure the kernel runs with the expected CWD. # Add user defined envars to os.environ so they can be picked up by # the child process running the kernel. logger.info('Starting iPython kernel with user defined envars: ' '{}'.format(self.envars)) for k, v in self.envars.items(): os.environ[k] = v self.repl_kernel_manager = QtKernelManager() self.repl_kernel_manager.start_kernel() self.repl_kernel_client = self.repl_kernel_manager.client() self.kernel_started.emit(self.repl_kernel_manager, self.repl_kernel_client)
def _make_jupyter_widget_with_kernel(kernel_name): """ Start a kernel, connect to it, and create a RichJupyterWidget to use it. Parameters ---------- kernel_name : str Kernel name to use. """ kernel_manager = QtKernelManager(kernel_name=kernel_name) kernel_manager.start_kernel() kernel_client = kernel_manager.client() kernel_client.start_channels() jupyter_widget = RichJupyterWidget() jupyter_widget.kernel_manager = kernel_manager jupyter_widget.kernel_client = kernel_client return jupyter_widget
class KernelRunner(QObject): """ Used to control the iPython kernel in a non-blocking manner so the UI remains responsive. """ kernel_started = pyqtSignal(QtKernelManager, QtKernelClient) kernel_finished = pyqtSignal() def start_kernel(self): self.repl_kernel_manager = QtKernelManager() self.repl_kernel_manager.start_kernel() self.repl_kernel_client = self.repl_kernel_manager.client() self.kernel_started.emit(self.repl_kernel_manager, self.repl_kernel_client) def stop_kernel(self): self.repl_kernel_client.stop_channels() self.repl_kernel_manager.shutdown_kernel(now=True) self.kernel_finished.emit()
def _createConsoleWidget(self): print("[12] inside IPythonConsole _createConsoleWidget") layout = QtWidgets.QVBoxLayout() connection_file = find_connection_file(self.connection_file) self.kernel_manager = QtKernelManager(connection_file=connection_file) self.kernel_manager.load_connection_file() self.kernel_manager.client_factory = QtKernelClient self.kernel_client = self.kernel_manager.client() self.kernel_client.start_channels() widget_options = {} if sys.platform.startswith('linux'): # Some upstream bug crashes IDA when the ncurses completion is # used. I'm not sure where the bug is exactly (IDA's Qt5 bindings?) # but using the "droplist" instead works around the crash. The # problem is present only on Linux. # See: https://github.com/eset/ipyida/issues/8 widget_options["gui_completion"] = 'droplist' widget_options.update(_user_widget_options) print( "[13] inside IPythonConsole before creating CutterRichJupyterWidget" ) class fake_font(): def pointSize(self): return 16 QtWidgets.QApplication.instance().font = fake_font self.ipython_widget = CutterRichJupyterWidget(self.parent, **widget_options) print( "[15] inside IPythonConsole after creating CutterRichJupyterWidget" ) self.ipython_widget.kernel_manager = self.kernel_manager self.ipython_widget.kernel_client = self.kernel_client self.ipython_widget.setWindowTitle("iPython Widget") layout.addWidget(self.ipython_widget) return layout
def __init__(self, customBanner=None, *args, **kwargs): if customBanner != None: self.banner = customBanner super().__init__() self.font_size = 6 self.kernel_manager = QtKernelManager() self.kernel_manager.start_kernel() self.kernel_manager.kernel.gui = 'qt' self.kernel_client = self._kernel_manager.client() self.kernel_client.start_channels() self.kernel_client.iopub_channel.message_received.connect( self.update_kernel_status) def stop(): self.kernel_client.stop_channels() self.kernel_manager.shutdown_kernel() self.exit_requested.connect(stop) self.init_logging_level()
def __init__(self, parent=None, custom_banner=None, **kwargs): super(QIPythonWidget_embed, self).__init__(parent=parent, **kwargs) self.plugin = parent kernel_manager = QtKernelManager(kernel_name=USE_KERNEL) kernel_manager.start_kernel() kernel_client = kernel_manager.client() kernel_client.start_channels() self.jupyter_widget = RichJupyterWidget() self.jupyter_widget.kernel_manager = kernel_manager self.jupyter_widget.kernel_client = kernel_client if custom_banner is not None: self.jupyter_widget.banner = custom_banner layout = QHBoxLayout() layout.addWidget(self.jupyter_widget)
def setUp(self): """Open a kernel.""" self.kernel_manager = QtKernelManager() self.kernel_manager.start_kernel() self.kernel_client = self.kernel_manager.client() self.kernel_client.start_channels(shell=True, iopub=True) self.blocking_client = self.kernel_client.blocking_client() self.blocking_client.start_channels(shell=True, iopub=True) self.comm_manager = self.kernel_client.comm_manager # Check if client is working self.blocking_client.execute('print(0)') try: self._get_next_msg() self._get_next_msg() except TimeoutError: # Maybe it works now? self.blocking_client.execute('print(0)') self._get_next_msg() self._get_next_msg()
def start_kernel(self): """ Start the kernel, obtain a client and emit a signal when both are started. """ logger.info(sys.path) os.chdir(self.cwd) # Ensure the kernel runs with the expected CWD. self.repl_kernel_manager = QtKernelManager() self.repl_kernel_manager.start_kernel() self.repl_kernel_client = self.repl_kernel_manager.client() self.kernel_started.emit(self.repl_kernel_manager, self.repl_kernel_client)
def __init__(self, title, icon): DockWidget.__init__(self, core.mainWindow(), title, icon, "Alt+I") self.setObjectName(title) self.setAllowedAreas(Qt.BottomDockWidgetArea | Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea) # Copied from https://github.com/jupyter/qtconsole/blob/master/examples/inprocess_qtconsole.py, then modified based on https://github.com/jupyter/qtconsole/blob/master/qtconsole/qtconsoleapp.py -- the QtInProcessKernelManager is blocking, so infinite loops crash Enki! kernel_manager = QtKernelManager() kernel_manager.start_kernel() kernel_manager.client_factory = QtKernelClient kernel_manager.kernel.gui = 'qt' kernel_client = kernel_manager.client() kernel_client.start_channels() self.ipython_widget = RichJupyterWidget() self.ipython_widget.kernel_manager = kernel_manager self.ipython_widget.kernel_client = kernel_client # By default, iPython adds a blank line between inputs. Per Monika's request, this eliminates the extra line. See https://qtconsole.readthedocs.io/en/latest/config_options.html#options; this fix was based on info from https://stackoverflow.com/questions/38652671/ipython-5-0-remove-spaces-between-input-lines. self.ipython_widget.input_sep = '' self.ipython_widget.show() self.setWidget(self.ipython_widget) self.setFocusProxy(self.ipython_widget)
class KernelRunner(QObject): """ Used to control the iPython kernel in a non-blocking manner so the UI remains responsive. """ kernel_started = pyqtSignal(QtKernelManager, QtKernelClient) kernel_finished = pyqtSignal() def __init__(self, cwd): """ Initialise the kernel runner with a target current working directory. """ super().__init__() self.cwd = cwd def start_kernel(self): """ Start the kernel, obtain a client and emit a signal when both are started. """ logger.info(sys.path) os.chdir(self.cwd) # Ensure the kernel runs with the expected CWD. self.repl_kernel_manager = QtKernelManager() self.repl_kernel_manager.start_kernel() self.repl_kernel_client = self.repl_kernel_manager.client() self.kernel_started.emit(self.repl_kernel_manager, self.repl_kernel_client) def stop_kernel(self): """ Stop the client connections to the kernel, affect an immediate shutdown of the kernel and emit a "finished" signal. """ self.repl_kernel_client.stop_channels() self.repl_kernel_manager.shutdown_kernel(now=True) self.kernel_finished.emit()
def start_kernel(self): """ Create the expected context, start the kernel, obtain a client and emit a signal when both are started. """ logger.info(sys.path) os.chdir(self.cwd) # Ensure the kernel runs with the expected CWD. # Add user defined envars to os.environ so they can be picked up by # the child process running the kernel. logger.info('Starting iPython kernel with user defined envars: ' '{}'.format(self.envars)) for k, v in self.envars.items(): os.environ[k] = v # Ensure the expected paths are in PYTHONPATH of the subprocess so the # kernel and Mu-installed third party applications can be found. if 'PYTHONPATH' not in os.environ: os.environ['PYTHONPATH'] = os.pathsep.join(sys.path) self.repl_kernel_manager = QtKernelManager() self.repl_kernel_manager.start_kernel() self.repl_kernel_client = self.repl_kernel_manager.client() self.kernel_started.emit(self.repl_kernel_manager, self.repl_kernel_client)