Exemple #1
0
class KernelEngine:
    """Manages kernels for one document"""
    def __init__(self, settings, document_language):
        """Initializes available kernel names"""
        self.__settings = settings
        self.__doc_lang = document_language

        spec_manager = KernelSpecManager()
        kernel_names = spec_manager.find_kernel_specs()

        self.__available_kernel_names_mappings = {}

        for name in kernel_names:
            spec = spec_manager.get_kernel_spec(name)
            self.__available_kernel_names_mappings[spec.display_name] = name

        self.__manager = MultiKernelManager()

        self.__client_managers = {}

    def __del__(self):
        """Safe kernels shutdown"""
        self.__manager.shutdown_all()

    def execute(self,
                language,
                code,
                context,
                processing_manager,
                output_types=None,
                timeout=None,
                allow_errors=False):
        """Executes code in specified language within specified context"""
        client = self.__get_client(language, context)
        return client.execute(code, processing_manager, output_types, timeout,
                              allow_errors)

    def __kernel_name_by_language(self, language):
        """Gets kernel name by language"""
        try:
            return self.__available_kernel_names_mappings[language]
        except KeyError:
            raise InvalidLanguageNameError(
                language, self.__available_kernel_names_mappings.keys())

    def __get_client(self, language, context=None):
        """Returns client dor speciffic language and context"""
        kernel_name = self.__kernel_name_by_language(language)

        try:
            manager = self.__client_managers[kernel_name]
        except KeyError:
            manager = KernelClientManager(kernel_name, language,
                                          self.__doc_lang, self.__manager,
                                          self.__settings.timeout(language))
            self.__client_managers[kernel_name] = manager

        return manager.client(context)
    def _init_kernel_manager(self):
        mkm = MultiKernelManager()
        mkm.log_level = 'DEBUG'
        mkm.kernel_spec_manager = KernelSpecManager()
        mkm.kernel_spec_manager.kernel_dirs.append(
          self._kernels_source_dir + '/share/jupyter/kernels')
        mkm.kernel_spec_manager.install_kernel_spec(source_dir=self._py_executing_kernel_source_dir,
                                                    kernel_name=self.PYTHON_EXECUTING_KERNEL_NAME,
                                                    prefix=self._kernels_source_dir)

        mkm.kernel_spec_manager.install_kernel_spec(source_dir=self._r_executing_kernel_source_dir,
                                                    kernel_name=self.R_EXECUTING_KERNEL_NAME,
                                                    prefix=self._kernels_source_dir)
        return mkm
class KernelEngine:
    """Manages kernels for one document"""

    def __init__(self, settings, document_language):
        """Initializes available kernel names"""
        self.__settings = settings
        self.__doc_lang = document_language

        spec_manager = KernelSpecManager()
        kernel_names = spec_manager.find_kernel_specs()

        self.__available_kernel_names_mappings = {}

        for name in kernel_names:
            spec = spec_manager.get_kernel_spec(name)
            self.__available_kernel_names_mappings[spec.display_name] = name

        self.__manager = MultiKernelManager()

        self.__client_managers = {}

    def __del__(self):
        """Safe kernels shutdown"""
        self.__manager.shutdown_all()

    def execute(self, language, code, context, processing_manager, output_types=None, timeout=None, allow_errors=False):
        """Executes code in specified language within specified context"""
        client = self.__get_client(language, context)
        return client.execute(code, processing_manager, output_types, timeout, allow_errors)

    def __kernel_name_by_language(self, language):
        """Gets kernel name by language"""
        try:
            return self.__available_kernel_names_mappings[language]
        except KeyError:
            raise InvalidLanguageNameError(language, self.__available_kernel_names_mappings.keys())

    def __get_client(self, language, context=None):
        """Returns client dor speciffic language and context"""
        kernel_name = self.__kernel_name_by_language(language)

        try:
            manager = self.__client_managers[kernel_name]
        except KeyError:
            manager = KernelClientManager(kernel_name, language, self.__doc_lang, self.__manager,
                                          self.__settings.timeout(language))
            self.__client_managers[kernel_name] = manager

        return manager.client(context)
Exemple #4
0
class PyExecutor(pyexecutor_pb2_grpc.PyExecutorServicer):
    def __init__(self):
        self.manager = MultiKernelManager()
        kernel_id = self.manager.start_kernel()
        self.kernel = self.manager.get_kernel(kernel_id)
        self.client = BlockingKernelClient()
        self.client.load_connection_file(self.kernel.connection_file)

    def Execute(self, request, context):
        response = self.client.execute_interactive(
            code=request.command,
            user_expressions={'test': request.expression})
        expression = response['content']['user_expressions']['test']['data']
        result = expression[
            'text/html'] if 'text/html' in expression else expression[
                'text/plain']
        return pyexecutor_pb2.ExecuteResponse(result=result)
Exemple #5
0
    def __init__(self, settings, document_language):
        """Initializes available kernel names"""
        self.__settings = settings
        self.__doc_lang = document_language

        spec_manager = KernelSpecManager()
        kernel_names = spec_manager.find_kernel_specs()

        self.__available_kernel_names_mappings = {}

        for name in kernel_names:
            spec = spec_manager.get_kernel_spec(name)
            self.__available_kernel_names_mappings[spec.display_name] = name

        self.__manager = MultiKernelManager()

        self.__client_managers = {}
Exemple #6
0
 def do_ipython(self,code):
     global km
     global km
     global remote_id
     global remote
     global kernelmanager
     # python=True
     if km==None:
         kernelmanager = MultiKernelManager()
         remote_id = kernelmanager.start_kernel('python2')
         remote_kernel = kernelmanager.get_kernel(remote_id)
         remote = remote_kernel.client()
         km=remote.blocking_client()
         km.start_channels()
         if km.shell_channel.msg_ready():
             km.shell_channel.get_msg()
             km.iopub_channel.get_msg()
     #if km.shell_channel.msg_ready():
     #    km.shell_channel.get_msg()
     #if km.iopub_channel.msg_ready():
     #    km.iopub_channel.get_msg()
     km.execute(code)
     display_data=[]
     msgS=km.shell_channel.get_msg(block=True,timeout=-1)
     msg=km.iopub_channel.get_msg(block=True,timeout=-1)
     msgs=km.iopub_channel.get_msgs()
     for m in msgs:
         if m['msg_type']=='error':
             output=m['content']['text'] #.__repr__()#+msg+id
             display_data.append({'text/plain':output})
             break
         if m['msg_type']=='stream':
             output=m['content']['text'] #.__repr__()#+msg+id
             display_data.append({'text/plain':output})
         if m['msg_type']=='display_data':
             display_data.append(m['content']['data'])
     return display_data
Exemple #7
0
 def do_ipython(self, code):
     global km
     global km
     global remote_id
     global remote
     global kernelmanager
     # python=True
     if km == None:
         kernelmanager = MultiKernelManager()
         remote_id = kernelmanager.start_kernel('python2')
         remote_kernel = kernelmanager.get_kernel(remote_id)
         remote = remote_kernel.client()
         km = remote.blocking_client()
         km.start_channels()
         if km.shell_channel.msg_ready():
             km.shell_channel.get_msg()
             km.iopub_channel.get_msg()
     #if km.shell_channel.msg_ready():
     #    km.shell_channel.get_msg()
     #if km.iopub_channel.msg_ready():
     #    km.iopub_channel.get_msg()
     km.execute(code)
     display_data = []
     msgS = km.shell_channel.get_msg(block=True, timeout=-1)
     msg = km.iopub_channel.get_msg(block=True, timeout=-1)
     msgs = km.iopub_channel.get_msgs()
     for m in msgs:
         if m['msg_type'] == 'error':
             output = m['content']['text']  #.__repr__()#+msg+id
             display_data.append({'text/plain': output})
             break
         if m['msg_type'] == 'stream':
             output = m['content']['text']  #.__repr__()#+msg+id
             display_data.append({'text/plain': output})
         if m['msg_type'] == 'display_data':
             display_data.append(m['content']['data'])
     return display_data
    def __init__(self, settings, document_language):
        """Initializes available kernel names"""
        self.__settings = settings
        self.__doc_lang = document_language

        spec_manager = KernelSpecManager()
        kernel_names = spec_manager.find_kernel_specs()

        self.__available_kernel_names_mappings = {}

        for name in kernel_names:
            spec = spec_manager.get_kernel_spec(name)
            self.__available_kernel_names_mappings[spec.display_name] = name

        self.__manager = MultiKernelManager()

        self.__client_managers = {}
Exemple #9
0
def main():
    logging.basicConfig(level=logging.DEBUG)
    app = QtGui.QApplication(sys.argv)

    window = QtGui.QWidget()
    vbox = QtGui.QVBoxLayout()

    manager = MultiKernelManager()

    client1 = create_kernel_client(manager)
    widget1 = RichJupyterWidget(buffer_size=10000)
    widget1.kernel_client = client1

    client2 = create_kernel_client(manager)
    widget2 = RichJupyterWidget(buffer_size=10000)
    widget2.kernel_client = client2

    vbox.addWidget(widget1)
    vbox.addWidget(widget2)

    button1 = QtGui.QPushButton("Set up #1")
    button1.clicked.connect(lambda: setup_environment(widget1))
    button2 = QtGui.QPushButton("Set up #2")
    button2.clicked.connect(lambda: setup_environment(widget2))

    hbox = QtGui.QHBoxLayout()
    hbox.addWidget(button1)
    hbox.addWidget(button2)
    vbox.addLayout(hbox)

    window.setGeometry(QtCore.QRect(300, 300, 1000, 800))
    window.setWindowTitle('Two QT consoles connected to the same kernel')
    window.setLayout(vbox)
    window.show()
    window.activateWindow()
    window.raise_()

    sys.exit(app.exec_())
Exemple #10
0
def kernel(kernel_name):
    """Start a kernel and provide a client to the kernel.
    Clean up afterward."""
    # Connect to the kernel
    multimanager = MultiKernelManager()
    uid = multimanager.start_kernel(kernel_name)
    manager = multimanager.get_kernel(uid)
    client = manager.client()

    # Prepare the client; don't do anything until it's ready!
    client.start_channels()
    client.wait_for_ready()

    try:
        yield client
    finally:
        # Remove the connection file
        os.remove(client.connection_file)

        # Make sure all kernels turn off.
        multimanager.shutdown_all()
Exemple #11
0
from jupyter_client import MultiKernelManager
mkm = MultiKernelManager()
mkm.start_kernel(kernel_id=88472)
import notebook.notebookapp as nbapp

app = nbapp.NotebookApp(kernel_manager=mkm, kernel_id=88472)
app.launch_instance(
)  # this launches a notebook, but the kernel manager is not actually passed down in the constructor

app.kernel_manager = mkm
app.launch_instance()  # no effect. need to
Exemple #12
0
# The great "support IPython 2, 3, 4" strat begins
if not TEST:
    try:
        import jupyter
    except ImportError:
        jupyter_era = False
    else:
        jupyter_era = True

    if jupyter_era:
        # Jupyter / IPython 4.x
        from jupyter_client import KernelManager
        from jupyter_client.kernelspec import KernelSpecManager
        from jupyter_client import MultiKernelManager
        kernelSpecManager = KernelSpecManager()
        multiKernelManager = MultiKernelManager()
    else:
        from IPython.kernel import KernelManager
        from IPython.kernel.kernelspec import KernelSpecManager
        from IPython.kernel.multikernelmanager import MultiKernelManager
        kernelSpecManager = KernelSpecManager()
        multiKernelManager = MultiKernelManager()

# End of the great "support IPython 2, 3, 4" strat


def _debug_write(out):
    if DEBUG:
        sys.__stdout__.write(out)
        sys.__stdout__.write("\n")
        sys.__stdout__.flush()
Exemple #13
0
 def __init__(self):
     self.manager = MultiKernelManager()
     kernel_id = self.manager.start_kernel()
     self.kernel = self.manager.get_kernel(kernel_id)
     self.client = BlockingKernelClient()
     self.client.load_connection_file(self.kernel.connection_file)