Пример #1
0
    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)
Пример #2
0
def OpenJupyterNotebook(path=None):
    """
    Open a Jupyter notebook in a new task pane.

    :param path: Path to Jupyter notebook file or directory.
    :return: True on success
    """
    try:
        if path is not None:
            if not os.path.isabs(path):
                # Try and get the absolute path relative to the active workbook
                xl = xl_app(com_package="win32com")
                wb = xl.ActiveWorkbook
                if wb is not None and wb.FullName and os.path.exists(
                        wb.FullName):
                    abs_path = os.path.join(os.path.dirname(wb.FullName), path)
                    if os.path.exists(abs_path):
                        path = abs_path
            if not os.path.exists(path):
                raise RuntimeError(f"Path '{path}' not found.")

        initial_path = None
        notebook_path = None
        if path is not None:
            if os.path.isdir(path):
                initial_path = path
            elif os.path.isfile(path):
                notebook_path = path
            else:
                raise RuntimeError(f"Something is wrong with {path}")

        # Use schedule_call to actually open the notebook since if this was called
        # from a Workbook.Open macro Excel may not yet be ready to open a CTP.
        schedule_call(
            partial(open_jupyter_notebook,
                    initial_path=initial_path,
                    notebook_path=notebook_path))

        return True
    except Exception as e:
        xlcAlert(f"Error opening Jupyter notebook: {e}")
        raise