Beispiel #1
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)
Beispiel #2
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()
Beispiel #3
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
Beispiel #4
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
Beispiel #5
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